Skip to content

Commit 8496f1a

Browse files
committed
Show type for ErrUnsupportedPtrType
This may aid in figuring out where the problematic JSON is Before: ``` Pointer type in struct is not supported ``` After: ``` Pointer (*int) in struct is not supported ```
1 parent af3dab1 commit 8496f1a

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

request.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ var (
2727
// (numeric) but the Struct field was a non numeric type (i.e. not int, uint,
2828
// float, etc)
2929
ErrUnknownFieldNumberType = errors.New("The struct field was not of a known number type")
30-
// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
31-
// the JSON value was of a different type
32-
ErrUnsupportedPtrType = errors.New("Pointer type in struct is not supported")
3330
// ErrInvalidType is returned when the given type is incompatible with the expected type.
3431
ErrInvalidType = errors.New("Invalid type provided") // I wish we used punctuation.
3532
)
3633

34+
// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
35+
// the JSON value was of a different type
36+
func ErrUnsupportedPtrType(t interface{}) error {
37+
return fmt.Errorf("Pointer (%s) in struct is not supported", t)
38+
}
39+
3740
// UnmarshalPayload converts an io into a struct instance using jsonapi tags on
3841
// struct fields. This method supports single request payloads only, at the
3942
// moment. Bulk creates and updates are not supported yet.
@@ -434,11 +437,11 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
434437
case uintptr:
435438
concreteVal = reflect.ValueOf(&cVal)
436439
default:
437-
return ErrUnsupportedPtrType
440+
return ErrUnsupportedPtrType(cVal)
438441
}
439442

440443
if fieldValue.Type() != concreteVal.Type() {
441-
return ErrUnsupportedPtrType
444+
return ErrUnsupportedPtrType(fieldValue.Type())
442445
}
443446

444447
fieldValue.Set(concreteVal)

request_test.go

Lines changed: 1 addition & 1 deletion
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 := ErrUnsupportedPtrType.Error()
129+
expectedErrorMessage := ErrUnsupportedPtrType("*string").Error()
130130

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

0 commit comments

Comments
 (0)