Skip to content

Commit

Permalink
Add even more info
Browse files Browse the repository at this point in the history
E.g.:

```
[jsonapi unmarshalNode]: Can't unmarshal map[min_containers:2
max_containers:5] () to struct field `Scaling`, which is a pointer to
`Scaling` (struct), which is not a supported type
```
  • Loading branch information
msabramo committed Jan 17, 2018
1 parent e7f4e85 commit c0336d5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ var (

// 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)
func ErrUnsupportedPtrType(rf reflect.Value, f reflect.StructField) error {
return fmt.Errorf(
"[jsonapi unmarshalNode]: Can't unmarshal %+v (%s) to struct field `%s`, which is a pointer to `%s` (%s), which is not a supported type",
rf, rf.Type().Name(), f.Name, f.Type.Elem().Name(), f.Type.Elem().Kind(),
)
}

// UnmarshalPayload converts an io into a struct instance using jsonapi tags on
Expand Down Expand Up @@ -424,7 +427,6 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
// Field was a Pointer type
if fieldValue.Kind() == reflect.Ptr {
var concreteVal reflect.Value

switch cVal := val.(type) {
case string:
concreteVal = reflect.ValueOf(&cVal)
Expand All @@ -437,11 +439,11 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
case uintptr:
concreteVal = reflect.ValueOf(&cVal)
default:
return ErrUnsupportedPtrType(cVal)
return ErrUnsupportedPtrType(reflect.ValueOf(val), fieldType)
}

if fieldValue.Type() != concreteVal.Type() {
return ErrUnsupportedPtrType(fieldValue.Type())
return ErrUnsupportedPtrType(reflect.ValueOf(val), fieldType)
}

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("*string").Error()
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"

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

Expand Down

0 comments on commit c0336d5

Please sign in to comment.