99
1010const errMessage = "gojsonq: wrong method call for %v"
1111
12+ // Available named error values
1213var (
1314 ErrExpectsPointer = fmt .Errorf ("gojsonq: failed to unmarshal, expects pointer" )
1415 ErrImmutable = fmt .Errorf ("gojsonq: failed to unmarshal, target is not mutable" )
@@ -30,7 +31,7 @@ func (r *Result) Nil() bool {
3031 return r .value == nil
3132}
3233
33- // As sets the value of Result to v
34+ // As sets the value of Result to v; It does not support methods with argument available in Result
3435func (r * Result ) As (v interface {}) error {
3536 if r .value != nil {
3637 rv := reflect .ValueOf (v )
@@ -43,11 +44,26 @@ func (r *Result) As(v interface{}) error {
4344 return ErrImmutable
4445 }
4546
46- value := reflect .ValueOf (r .value )
47- if ! reflect .TypeOf (r .value ).AssignableTo (reflect .TypeOf (v ).Elem ()) {
48- return ErrTypeMismatch
47+ method := rv .Type ().String ()
48+ methodMap := map [string ]string {
49+ "*string" : "String" , "*bool" : "Bool" , "*time.Duration" : "Duration" ,
50+ "*int" : "Int" , "*int8" : "Int8" , "*int16" : "Int16" , "*int32" : "Int32" ,
51+ "*uint" : "Uint" , "*uint8" : "Uint8" , "*uint16" : "Uint16" , "*uint32" : "Uint32" ,
52+ "*float32" : "Float32" , "*float64" : "Float64" ,
53+
54+ "*[]string" : "StringSlice" , "*[]bool" : "BoolSlice" , "*[]time.Duration" : "DurationSlice" ,
55+ "*[]int" : "IntSlice" , "*[]int8" : "Int8Slice" , "*[]int16" : "Int16Slice" , "*[]int32" : "Int32Slice" ,
56+ "*[]uint" : "UintSlice" , "*[]uint8" : "Uint8Slice" , "*[]uint16" : "Uint16Slice" , "*[]uint32" : "Uint32Slice" ,
57+ "*[]float32" : "Float32Slice" , "*[]float64" : "Float64Slice" ,
58+ }
59+
60+ vv := reflect .ValueOf (r ).MethodByName (methodMap [method ]).Call (nil )
61+ if vv != nil {
62+ if vv [1 ].Interface () != nil {
63+ return ErrTypeMismatch
64+ }
65+ rv .Elem ().Set (vv [0 ])
4966 }
50- rv .Elem ().Set (value )
5167 }
5268 return nil
5369}
0 commit comments