diff --git a/request.go b/request.go index fe29706..29361b3 100644 --- a/request.go +++ b/request.go @@ -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. @@ -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) diff --git a/request_test.go b/request_test.go index 2206449..efaf4fb 100644 --- a/request_test.go +++ b/request_test.go @@ -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)