Skip to content

Commit f9316df

Browse files
committed
Don't print type & kind if redundant
1 parent cf7abfd commit f9316df

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

request.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ var (
3333

3434
// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
3535
// the JSON value was of a different type
36-
func ErrUnsupportedPtrType(rf reflect.Value, f reflect.Type, structField reflect.StructField) error {
36+
func ErrUnsupportedPtrType(rf reflect.Value, t reflect.Type, structField reflect.StructField) error {
37+
typeName := t.Elem().Name()
38+
kind := t.Elem().Kind()
39+
if kind.String() != "" && kind.String() != typeName {
40+
typeName = fmt.Sprintf("%s (%s)", typeName, kind.String())
41+
}
3742
return fmt.Errorf(
38-
"[jsonapi unmarshalNode]: Can't unmarshal %+v (%s) to struct field `%s`, which is a pointer to `%s` (%s), which is not a supported type",
39-
rf, rf.Type().Name(), structField.Name, f.Elem().Name(), f.Elem().Kind(),
43+
"[jsonapi]: Can't unmarshal %+v (%s) to struct field `%s`, which is a pointer to `%s`, which is not a supported type",
44+
rf, rf.Type().Name(), structField.Name, typeName,
4045
)
4146
}
4247

request_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestUnmarshalToStructWithPointerAttr_BadType(t *testing.T) {
126126
in := map[string]interface{}{
127127
"name": true, // This is the wrong type.
128128
}
129-
expectedErrorMessage := "[jsonapi unmarshalNode]: Can't unmarshal true (bool) to struct field `Name`, which is a pointer to `string` (string), which is not a supported type"
129+
expectedErrorMessage := "[jsonapi]: Can't unmarshal true (bool) to struct field `Name`, which is a pointer to `string`, which is not a supported type"
130130

131131
err := UnmarshalPayload(sampleWithPointerPayload(in), out)
132132

0 commit comments

Comments
 (0)