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

Improve handleStruct to use struct's own Unmarshaler if it is available #169

Open
wants to merge 2 commits 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
27 changes: 26 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ func unmarshalAttribute(
return
}

// Handle field of type json.RawMessage
if fieldValue.Type() == reflect.TypeOf(json.RawMessage{}) {
value, err = handleJSONRawMessage(attribute)
}

// Handle field of type time.Time
if fieldValue.Type() == reflect.TypeOf(time.Time{}) ||
fieldValue.Type() == reflect.TypeOf(new(time.Time)) {
Expand Down Expand Up @@ -444,6 +449,14 @@ func handleStringSlice(attribute interface{}) (reflect.Value, error) {
return reflect.ValueOf(values), nil
}

func handleJSONRawMessage(attribute interface{}) (reflect.Value, error) {
tmp, err := json.Marshal(attribute)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(json.RawMessage(tmp)), nil
}

func handleTime(attribute interface{}, args []string, fieldValue reflect.Value) (reflect.Value, error) {
var isIso8601 bool
v := reflect.ValueOf(attribute)
Expand Down Expand Up @@ -591,7 +604,19 @@ func handlePointer(
func handleStruct(
attribute interface{},
fieldValue reflect.Value) (reflect.Value, error) {

if fieldValue.CanAddr() {
interf := fieldValue.Addr().Interface()
if _, ok := interf.(json.Unmarshaler); ok {
var tmp []byte
tmp, err := json.Marshal(attribute)
if err == nil {
err = json.Unmarshal(tmp, interf)
if err == nil {
return reflect.ValueOf(interf), nil
}
}
}
}
data, err := json.Marshal(attribute)
if err != nil {
return reflect.Value{}, err
Expand Down
72 changes: 72 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,3 +1306,75 @@ func TestUnmarshalNestedStructSlice(t *testing.T) {
out.Teams[0].Members[0].Firstname)
}
}

type MyCustomAttribute struct {
Field string
}

func (mca MyCustomAttribute) MarshalJSON() ([]byte, error) {
return json.Marshal(mca.Field)
}
func (mca *MyCustomAttribute) UnmarshalJSON(bts []byte) error {
return json.Unmarshal(bts, &mca.Field)
}

type StructForTest struct {
ID string `jsonapi:"primary,tests"`
Custom MyCustomAttribute `jsonapi:"attr,custom,omitempty"`
Raw json.RawMessage `jsonapi:"attr,raw,omitempty"`
}

func TestUnmarshalWithCustomType(t *testing.T) {
sft := &StructForTest{
ID: "my-id",
Custom: MyCustomAttribute{
Field: "a-string",
},
}
buf := new(bytes.Buffer)
err := MarshalPayload(buf, sft)
if err != nil {
t.Fatal(err)
}
newSft := &StructForTest{}
err = UnmarshalPayload(buf, newSft)

if err != nil {
t.Fatal(err)
}

if sft.Custom.Field != newSft.Custom.Field {
t.Fatalf("Custom type wasn't properly unmarshalled: Expected to have `%s` but got `%s`",
sft.Custom.Field, newSft.Custom.Field)
}
}

func TestUnmarshalWithJSONRawMessage(t *testing.T) {
tests := [][]byte{
[]byte(`{"really":{"deep":true},"test":"toast"}`),
[]byte(`"just a string"`),
[]byte(`123`),
}
for _, v := range tests {
sft := &StructForTest{
ID: "my-id",
Raw: v,
}
buf := new(bytes.Buffer)
err := MarshalPayload(buf, sft)
if err != nil {
t.Fatal(err)
}
newSft := &StructForTest{}
err = UnmarshalPayload(buf, newSft)

if err != nil {
t.Fatal(err)
}

if bytes.Compare(sft.Raw, newSft.Raw) != 0 {
t.Fatalf("json.RawMessage wasn't properly unmarshalled: Expected to have `%s` but got `%s`",
string(sft.Raw), string(newSft.Raw))
}
}
}