diff --git a/models_test.go b/models_test.go index 2d4aae4..5aa3e58 100644 --- a/models_test.go +++ b/models_test.go @@ -5,6 +5,8 @@ import ( "time" ) +type StringType string + type BadModel struct { ID int `jsonapi:"primary"` } @@ -18,11 +20,12 @@ type ModelBadTypes struct { } type WithPointer struct { - ID *uint64 `jsonapi:"primary,with-pointers"` - Name *string `jsonapi:"attr,name"` - IsActive *bool `jsonapi:"attr,is-active"` - IntVal *int `jsonapi:"attr,int-val"` - FloatVal *float32 `jsonapi:"attr,float-val"` + ID *uint64 `jsonapi:"primary,with-pointers"` + Name *string `jsonapi:"attr,name"` + IsActive *bool `jsonapi:"attr,is-active"` + IntVal *int `jsonapi:"attr,int-val"` + FloatVal *float32 `jsonapi:"attr,float-val"` + String *StringType `jsonapi:"attr,string"` } type Timestamp struct { diff --git a/request.go b/request.go index a7bb0b1..929af34 100644 --- a/request.go +++ b/request.go @@ -580,7 +580,7 @@ func handlePointer( reflect.ValueOf(attribute), fieldType, structField) } - if t != concreteVal.Type() { + if !t.ConvertibleTo(concreteVal.Type()) { return reflect.Value{}, newErrUnsupportedPtrType( reflect.ValueOf(attribute), fieldType, structField) } diff --git a/request_test.go b/request_test.go index 3326598..7847010 100644 --- a/request_test.go +++ b/request_test.go @@ -45,6 +45,32 @@ func TestUnmarshall_attrStringSlice(t *testing.T) { } } +func TestUnmarshalToStructWithPointerToNamedType(t *testing.T) { + out := new(WithPointer) + in := map[string]interface{}{ + "string": "foo", + } + if err := UnmarshalPayload(sampleWithPointerPayload(in), out); err != nil { + t.Fatal(err) + } + if *out.String != "foo" { + t.Fatalf("Error unmarshalling to string alias ptr") + } +} + +func TestUnmarshalToStructWithPointerToNamedTypeNull(t *testing.T) { + out := new(WithPointer) + in := map[string]interface{}{ + "string": nil, + } + if err := UnmarshalPayload(sampleWithPointerPayload(in), out); err != nil { + t.Fatal(err) + } + if out.String != nil { + t.Fatalf("Error unmarshalling to string alias ptr") + } +} + func TestUnmarshalToStructWithPointerAttr(t *testing.T) { out := new(WithPointer) in := map[string]interface{}{ @@ -68,6 +94,9 @@ func TestUnmarshalToStructWithPointerAttr(t *testing.T) { if *out.FloatVal != 1.1 { t.Fatalf("Error unmarshalling to float ptr") } + if out.String != nil { + t.Fatalf("Error unmarshalling to string alias ptr") + } } func TestUnmarshalPayload_ptrsAllNil(t *testing.T) {