@@ -29,22 +29,33 @@ var (
29
29
ErrUnknownFieldNumberType = errors .New ("The struct field was not of a known number type" )
30
30
// ErrInvalidType is returned when the given type is incompatible with the expected type.
31
31
ErrInvalidType = errors .New ("Invalid type provided" ) // I wish we used punctuation.
32
+
32
33
)
33
34
34
35
// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
35
36
// 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 ()
39
46
if kind .String () != "" && kind .String () != typeName {
40
47
typeName = fmt .Sprintf ("%s (%s)" , typeName , kind .String ())
41
48
}
42
- return fmt .Errorf (
49
+ return fmt .Sprintf (
43
50
"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 ,
45
52
)
46
53
}
47
54
55
+ func newErrUnsupportedPtrType (rf reflect.Value , t reflect.Type , structField reflect.StructField ) error {
56
+ return ErrUnsupportedPtrType {rf , t , structField }
57
+ }
58
+
48
59
// UnmarshalPayload converts an io into a struct instance using jsonapi tags on
49
60
// struct fields. This method supports single request payloads only, at the
50
61
// moment. Bulk creates and updates are not supported yet.
@@ -559,15 +570,15 @@ func handlePointer(attribute interface{}, args []string, fieldType reflect.Type,
559
570
var err error
560
571
concreteVal , err = handleStruct (attribute , args , fieldType , fieldValue )
561
572
if err != nil {
562
- return reflect.Value {}, ErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
573
+ return reflect.Value {}, newErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
563
574
}
564
575
return concreteVal .Elem (), err
565
576
default :
566
- return reflect.Value {}, ErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
577
+ return reflect.Value {}, newErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
567
578
}
568
579
569
580
if t != concreteVal .Type () {
570
- return reflect.Value {}, ErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
581
+ return reflect.Value {}, newErrUnsupportedPtrType (reflect .ValueOf (attribute ), fieldType , structField )
571
582
}
572
583
573
584
return concreteVal , nil
0 commit comments