Skip to content

Commit bdac9a4

Browse files
committed
Add even more info
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 ```
1 parent 8496f1a commit bdac9a4

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

request.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ 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(t interface{}) error {
37-
return fmt.Errorf("Pointer (%s) in struct is not supported", t)
36+
func ErrUnsupportedPtrType(rf reflect.Value, f reflect.StructField) error {
37+
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(), f.Name, f.Type.Elem().Name(), f.Type.Elem().Kind(),
40+
)
3841
}
3942

4043
// UnmarshalPayload converts an io into a struct instance using jsonapi tags on
@@ -424,7 +427,6 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
424427
// Field was a Pointer type
425428
if fieldValue.Kind() == reflect.Ptr {
426429
var concreteVal reflect.Value
427-
428430
switch cVal := val.(type) {
429431
case string:
430432
concreteVal = reflect.ValueOf(&cVal)
@@ -437,11 +439,11 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
437439
case uintptr:
438440
concreteVal = reflect.ValueOf(&cVal)
439441
default:
440-
return ErrUnsupportedPtrType(cVal)
442+
return ErrUnsupportedPtrType(reflect.ValueOf(val), fieldType)
441443
}
442444

443445
if fieldValue.Type() != concreteVal.Type() {
444-
return ErrUnsupportedPtrType(fieldValue.Type())
446+
return ErrUnsupportedPtrType(reflect.ValueOf(val), fieldType)
445447
}
446448

447449
fieldValue.Set(concreteVal)

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 := ErrUnsupportedPtrType("*string").Error()
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"
130130

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

0 commit comments

Comments
 (0)