Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pointers to alias types #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"time"
)

type StringType string

type BadModel struct {
ID int `jsonapi:"primary"`
}
Expand All @@ -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"`
StringVal *StringType `jsonapi:"attr,string-val"`
}

type Timestamp struct {
Expand Down
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
29 changes: 29 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ func TestUnmarshall_attrStringSlice(t *testing.T) {
}
}

func TestUnmarshalToStructWithPointerToNamedType(t *testing.T) {
out := new(WithPointer)
in := map[string]interface{}{
"string-val": "foo",
}
if err := UnmarshalPayload(sampleWithPointerPayload(in), out); err != nil {
t.Fatal(err)
}
if *out.StringVal != "foo" {
t.Fatalf("Error unmarshalling to string alias ptr")
}
}

func TestUnmarshalToStructWithPointerToNamedTypeNull(t *testing.T) {
out := new(WithPointer)
in := map[string]interface{}{
"string-val": nil,
}
if err := UnmarshalPayload(sampleWithPointerPayload(in), out); err != nil {
t.Fatal(err)
}
if out.StringVal != nil {
t.Fatalf("Error unmarshalling to string alias ptr")
}
}

func TestUnmarshalToStructWithPointerAttr(t *testing.T) {
out := new(WithPointer)
in := map[string]interface{}{
Expand All @@ -68,6 +94,9 @@ func TestUnmarshalToStructWithPointerAttr(t *testing.T) {
if *out.FloatVal != 1.1 {
t.Fatalf("Error unmarshalling to float ptr")
}
if out.StringVal != nil {
t.Fatalf("Error unmarshalling to string alias ptr")
}
}

func TestUnmarshalPayload_ptrsAllNil(t *testing.T) {
Expand Down