@@ -29,22 +29,33 @@ var (
2929 ErrUnknownFieldNumberType = errors .New ("The struct field was not of a known number type" )
3030 // ErrInvalidType is returned when the given type is incompatible with the expected type.
3131 ErrInvalidType = errors .New ("Invalid type provided" ) // I wish we used punctuation.
32+
3233)
3334
3435// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
3536// the JSON value was of a different type
36- func ErrUnsupportedPtrType (rf reflect.Value , t reflect.Type , structField reflect.StructField ) error {
37- typeName := t .Elem ().Name ()
38- kind := t .Elem ().Kind ()
37+ type ErrUnsupportedPtrType struct {
38+ rf reflect.Value
39+ t reflect.Type
40+ structField reflect.StructField
41+ }
42+
43+ func (eupt ErrUnsupportedPtrType ) Error () string {
44+ typeName := eupt .t .Elem ().Name ()
45+ kind := eupt .t .Elem ().Kind ()
3946 if kind .String () != "" && kind .String () != typeName {
4047 typeName = fmt .Sprintf ("%s (%s)" , typeName , kind .String ())
4148 }
42- return fmt .Errorf (
49+ return fmt .Sprintf (
4350 "jsonapi: Can't unmarshal %+v (%s) to struct field `%s`, which is a pointer to `%s`" ,
44- rf , rf .Type ().Kind (), structField .Name , typeName ,
51+ eupt . rf , eupt . rf .Type ().Kind (), eupt . structField .Name , typeName ,
4552 )
4653}
4754
55+ func newErrUnsupportedPtrType (rf reflect.Value , t reflect.Type , structField reflect.StructField ) error {
56+ return ErrUnsupportedPtrType {rf , t , structField }
57+ }
58+
4859// UnmarshalPayload converts an io into a struct instance using jsonapi tags on
4960// struct fields. This method supports single request payloads only, at the
5061// moment. Bulk creates and updates are not supported yet.
@@ -559,15 +570,15 @@ func handlePointer(attribute interface{}, args []string, fieldType reflect.Type,
559570 var err error
560571 concreteVal , err = handleStruct (attribute , args , fieldType , fieldValue )
561572 if err != nil {
562- return reflect.Value {}, ErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
573+ return reflect.Value {}, newErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
563574 }
564575 return concreteVal .Elem (), err
565576 default :
566- return reflect.Value {}, ErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
577+ return reflect.Value {}, newErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
567578 }
568579
569580 if t != concreteVal .Type () {
570- return reflect.Value {}, ErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
581+ return reflect.Value {}, newErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
571582 }
572583
573584 return concreteVal , nil
0 commit comments