Skip to content

Commit

Permalink
Show type for ErrUnsupportedPtrType
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
msabramo committed Jan 17, 2018
1 parent e0fc4ee commit e7f4e85
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ var (
// (numeric) but the Struct field was a non numeric type (i.e. not int, uint,
// float, etc)
ErrUnknownFieldNumberType = errors.New("The struct field was not of a known number type")
// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
// the JSON value was of a different type
ErrUnsupportedPtrType = errors.New("Pointer type in struct is not supported")
// ErrInvalidType is returned when the given type is incompatible with the expected type.
ErrInvalidType = errors.New("Invalid type provided") // I wish we used punctuation.
)

// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
// the JSON value was of a different type
func ErrUnsupportedPtrType(t interface{}) error {
return fmt.Errorf("Pointer (%s) in struct is not supported", t)
}

// UnmarshalPayload converts an io into a struct instance using jsonapi tags on
// struct fields. This method supports single request payloads only, at the
// moment. Bulk creates and updates are not supported yet.
Expand Down Expand Up @@ -434,11 +437,11 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
case uintptr:
concreteVal = reflect.ValueOf(&cVal)
default:
return ErrUnsupportedPtrType
return ErrUnsupportedPtrType(cVal)
}

if fieldValue.Type() != concreteVal.Type() {
return ErrUnsupportedPtrType
return ErrUnsupportedPtrType(fieldValue.Type())
}

fieldValue.Set(concreteVal)
Expand Down
2 changes: 1 addition & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestUnmarshalToStructWithPointerAttr_BadType(t *testing.T) {
in := map[string]interface{}{
"name": true, // This is the wrong type.
}
expectedErrorMessage := ErrUnsupportedPtrType.Error()
expectedErrorMessage := ErrUnsupportedPtrType("*string").Error()

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

Expand Down

0 comments on commit e7f4e85

Please sign in to comment.