2015-07-06 16:40:43 -04:00
|
|
|
package jsonapi
|
|
|
|
|
2015-07-07 18:46:20 -04:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-04-05 14:13:02 -04:00
|
|
|
"errors"
|
2017-02-17 18:11:39 -05:00
|
|
|
"fmt"
|
2015-07-08 12:34:37 -04:00
|
|
|
"io"
|
2017-01-26 18:25:13 -05:00
|
|
|
"reflect"
|
2017-02-03 18:18:58 -05:00
|
|
|
"sort"
|
2015-08-29 13:29:58 -04:00
|
|
|
"strings"
|
2015-07-07 18:46:20 -04:00
|
|
|
"testing"
|
2015-07-10 14:41:54 -04:00
|
|
|
"time"
|
2015-07-07 18:46:20 -04:00
|
|
|
)
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-02-03 18:18:58 -05:00
|
|
|
func TestUnmarshall_attrStringSlice(t *testing.T) {
|
|
|
|
out := &Book{}
|
|
|
|
tags := []string{"fiction", "sale"}
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "books",
|
|
|
|
"id": "1",
|
|
|
|
"attributes": map[string]interface{}{"tags": tags},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(bytes.NewReader(b), out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e, a := len(tags), len(out.Tags); e != a {
|
|
|
|
t.Fatalf("Was expecting %d tags, got %d", e, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(tags)
|
|
|
|
sort.Strings(out.Tags)
|
|
|
|
|
|
|
|
for i, tag := range tags {
|
|
|
|
if e, a := tag, out.Tags[i]; e != a {
|
|
|
|
t.Fatalf("At index %d, was expecting %s got %s", i, e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 21:34:38 -04:00
|
|
|
func TestUnmarshalToStructWithPointerAttr(t *testing.T) {
|
|
|
|
out := new(WithPointer)
|
|
|
|
in := map[string]interface{}{
|
|
|
|
"name": "The name",
|
|
|
|
"is-active": true,
|
|
|
|
"int-val": 8,
|
|
|
|
"float-val": 1.1,
|
|
|
|
}
|
|
|
|
if err := UnmarshalPayload(sampleWithPointerPayload(in), out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if *out.Name != "The name" {
|
|
|
|
t.Fatalf("Error unmarshalling to string ptr")
|
|
|
|
}
|
2016-07-18 17:48:35 -04:00
|
|
|
if !*out.IsActive {
|
2016-07-22 21:34:38 -04:00
|
|
|
t.Fatalf("Error unmarshalling to bool ptr")
|
|
|
|
}
|
|
|
|
if *out.IntVal != 8 {
|
|
|
|
t.Fatalf("Error unmarshalling to int ptr")
|
|
|
|
}
|
|
|
|
if *out.FloatVal != 1.1 {
|
|
|
|
t.Fatalf("Error unmarshalling to float ptr")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 15:33:18 -05:00
|
|
|
func TestUnmarshalPayload_missingTypeFieldShouldError(t *testing.T) {
|
|
|
|
if err := UnmarshalPayload(
|
|
|
|
strings.NewReader(`{"data":{"body":"hello world"}}`),
|
|
|
|
&Post{},
|
|
|
|
); err == nil {
|
|
|
|
t.Fatalf("Expected an error but did not get one")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
func TestUnmarshalPayload_ptrsAllNil(t *testing.T) {
|
|
|
|
out := new(WithPointer)
|
|
|
|
if err := UnmarshalPayload(
|
2021-03-06 15:33:18 -05:00
|
|
|
strings.NewReader(`{"data":{"type":"with-pointers"}}`), out); err != nil {
|
|
|
|
t.Fatalf("Error unmarshalling to Foo: %v", err)
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if out.ID != nil {
|
|
|
|
t.Fatalf("Error unmarshalling; expected ID ptr to be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalPayloadWithPointerID(t *testing.T) {
|
|
|
|
out := new(WithPointer)
|
|
|
|
attrs := map[string]interface{}{}
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(sampleWithPointerPayload(attrs), out); err != nil {
|
|
|
|
t.Fatalf("Error unmarshalling to Foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
// these were present in the payload -- expect val to be not nil
|
|
|
|
if out.ID == nil {
|
|
|
|
t.Fatalf("Error unmarshalling; expected ID ptr to be not nil")
|
|
|
|
}
|
|
|
|
if e, a := uint64(2), *out.ID; e != a {
|
|
|
|
t.Fatalf("Was expecting the ID to have a value of %d, got %d", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalPayloadWithPointerAttr_AbsentVal(t *testing.T) {
|
2016-07-22 21:34:38 -04:00
|
|
|
out := new(WithPointer)
|
|
|
|
in := map[string]interface{}{
|
|
|
|
"name": "The name",
|
|
|
|
"is-active": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(sampleWithPointerPayload(in), out); err != nil {
|
|
|
|
t.Fatalf("Error unmarshalling to Foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
// these were present in the payload -- expect val to be not nil
|
|
|
|
if out.Name == nil || out.IsActive == nil {
|
|
|
|
t.Fatalf("Error unmarshalling; expected ptr to be not nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// these were absent in the payload -- expect val to be nil
|
|
|
|
if out.IntVal != nil || out.FloatVal != nil {
|
|
|
|
t.Fatalf("Error unmarshalling; expected ptr to be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 13:11:20 -05:00
|
|
|
func TestUnmarshalToStructWithPointerAttr_BadType_bool(t *testing.T) {
|
2016-07-18 17:48:35 -04:00
|
|
|
out := new(WithPointer)
|
|
|
|
in := map[string]interface{}{
|
|
|
|
"name": true, // This is the wrong type.
|
|
|
|
}
|
2018-01-17 13:11:20 -05:00
|
|
|
expectedErrorMessage := "jsonapi: Can't unmarshal true (bool) to struct field `Name`, which is a pointer to `string`"
|
|
|
|
|
|
|
|
err := UnmarshalPayload(sampleWithPointerPayload(in), out)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error due to invalid type.")
|
|
|
|
}
|
|
|
|
if err.Error() != expectedErrorMessage {
|
|
|
|
t.Fatalf("Unexpected error message: %s", err.Error())
|
|
|
|
}
|
2018-03-14 16:43:24 -04:00
|
|
|
if _, ok := err.(ErrUnsupportedPtrType); !ok {
|
|
|
|
t.Fatalf("Unexpected error type: %s", reflect.TypeOf(err))
|
|
|
|
}
|
2018-01-17 13:11:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalToStructWithPointerAttr_BadType_MapPtr(t *testing.T) {
|
|
|
|
out := new(WithPointer)
|
|
|
|
in := map[string]interface{}{
|
|
|
|
"name": &map[string]interface{}{"a": 5}, // This is the wrong type.
|
|
|
|
}
|
|
|
|
expectedErrorMessage := "jsonapi: Can't unmarshal map[a:5] (map) to struct field `Name`, which is a pointer to `string`"
|
|
|
|
|
|
|
|
err := UnmarshalPayload(sampleWithPointerPayload(in), out)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error due to invalid type.")
|
|
|
|
}
|
|
|
|
if err.Error() != expectedErrorMessage {
|
|
|
|
t.Fatalf("Unexpected error message: %s", err.Error())
|
|
|
|
}
|
2018-03-14 16:43:24 -04:00
|
|
|
if _, ok := err.(ErrUnsupportedPtrType); !ok {
|
|
|
|
t.Fatalf("Unexpected error type: %s", reflect.TypeOf(err))
|
|
|
|
}
|
2018-01-17 13:11:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalToStructWithPointerAttr_BadType_Struct(t *testing.T) {
|
|
|
|
out := new(WithPointer)
|
|
|
|
type FooStruct struct{ A int }
|
|
|
|
in := map[string]interface{}{
|
|
|
|
"name": FooStruct{A: 5}, // This is the wrong type.
|
|
|
|
}
|
|
|
|
expectedErrorMessage := "jsonapi: Can't unmarshal map[A:5] (map) to struct field `Name`, which is a pointer to `string`"
|
|
|
|
|
|
|
|
err := UnmarshalPayload(sampleWithPointerPayload(in), out)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error due to invalid type.")
|
|
|
|
}
|
|
|
|
if err.Error() != expectedErrorMessage {
|
|
|
|
t.Fatalf("Unexpected error message: %s", err.Error())
|
|
|
|
}
|
2018-03-14 16:43:24 -04:00
|
|
|
if _, ok := err.(ErrUnsupportedPtrType); !ok {
|
|
|
|
t.Fatalf("Unexpected error type: %s", reflect.TypeOf(err))
|
|
|
|
}
|
2018-01-17 13:11:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalToStructWithPointerAttr_BadType_IntSlice(t *testing.T) {
|
|
|
|
out := new(WithPointer)
|
|
|
|
type FooStruct struct{ A, B int }
|
|
|
|
in := map[string]interface{}{
|
|
|
|
"name": []int{4, 5}, // This is the wrong type.
|
|
|
|
}
|
|
|
|
expectedErrorMessage := "jsonapi: Can't unmarshal [4 5] (slice) to struct field `Name`, which is a pointer to `string`"
|
2016-07-18 17:48:35 -04:00
|
|
|
|
|
|
|
err := UnmarshalPayload(sampleWithPointerPayload(in), out)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error due to invalid type.")
|
|
|
|
}
|
|
|
|
if err.Error() != expectedErrorMessage {
|
|
|
|
t.Fatalf("Unexpected error message: %s", err.Error())
|
|
|
|
}
|
2018-03-14 16:43:24 -04:00
|
|
|
if _, ok := err.(ErrUnsupportedPtrType); !ok {
|
|
|
|
t.Fatalf("Unexpected error type: %s", reflect.TypeOf(err))
|
|
|
|
}
|
2016-07-18 17:48:35 -04:00
|
|
|
}
|
|
|
|
|
2016-07-22 21:34:38 -04:00
|
|
|
func TestStringPointerField(t *testing.T) {
|
|
|
|
// Build Book payload
|
|
|
|
description := "Hello World!"
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "books",
|
|
|
|
"id": "5",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"author": "aren55555",
|
|
|
|
"description": description,
|
|
|
|
"isbn": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
payload, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse JSON API payload
|
|
|
|
book := new(Book)
|
|
|
|
if err := UnmarshalPayload(bytes.NewReader(payload), book); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if book.Description == nil {
|
|
|
|
t.Fatal("Was not expecting a nil pointer for book.Description")
|
|
|
|
}
|
|
|
|
if expected, actual := description, *book.Description; expected != actual {
|
|
|
|
t.Fatalf("Was expecting descript to be `%s`, got `%s`", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 14:49:36 -04:00
|
|
|
func TestMalformedTag(t *testing.T) {
|
|
|
|
out := new(BadModel)
|
2015-07-10 12:07:12 -04:00
|
|
|
err := UnmarshalPayload(samplePayload(), out)
|
2016-01-05 16:13:24 -05:00
|
|
|
if err == nil || err != ErrBadJSONAPIStructTag {
|
2015-07-08 14:49:36 -04:00
|
|
|
t.Fatalf("Did not error out with wrong number of arguments in tag")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 13:29:58 -04:00
|
|
|
func TestUnmarshalInvalidJSON(t *testing.T) {
|
|
|
|
in := strings.NewReader("{}")
|
|
|
|
out := new(Blog)
|
|
|
|
|
|
|
|
err := UnmarshalPayload(in, out)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Did not error out the invalid JSON.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 17:48:35 -04:00
|
|
|
func TestUnmarshalInvalidJSON_BadType(t *testing.T) {
|
|
|
|
var badTypeTests = []struct {
|
|
|
|
Field string
|
|
|
|
BadValue interface{}
|
2017-01-31 14:18:51 -05:00
|
|
|
Error error
|
2016-07-18 17:48:35 -04:00
|
|
|
}{ // The `Field` values here correspond to the `ModelBadTypes` jsonapi fields.
|
2017-01-31 14:18:51 -05:00
|
|
|
{Field: "string_field", BadValue: 0, Error: ErrUnknownFieldNumberType}, // Expected string.
|
|
|
|
{Field: "float_field", BadValue: "A string.", Error: ErrInvalidType}, // Expected float64.
|
|
|
|
{Field: "time_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected int64.
|
|
|
|
{Field: "time_ptr_field", BadValue: "A string.", Error: ErrInvalidTime}, // Expected *time / int64.
|
2016-07-18 17:48:35 -04:00
|
|
|
}
|
2017-02-03 17:45:34 -05:00
|
|
|
for _, test := range badTypeTests {
|
2017-02-17 18:11:39 -05:00
|
|
|
t.Run(fmt.Sprintf("Test_%s", test.Field), func(t *testing.T) {
|
|
|
|
out := new(ModelBadTypes)
|
|
|
|
in := map[string]interface{}{}
|
|
|
|
in[test.Field] = test.BadValue
|
|
|
|
expectedErrorMessage := test.Error.Error()
|
|
|
|
|
|
|
|
err := UnmarshalPayload(samplePayloadWithBadTypes(in), out)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error due to invalid type.")
|
|
|
|
}
|
|
|
|
if err.Error() != expectedErrorMessage {
|
|
|
|
t.Fatalf("Unexpected error message: %s", err.Error())
|
|
|
|
}
|
|
|
|
})
|
2016-07-18 17:48:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 21:32:15 -04:00
|
|
|
func TestUnmarshalSetsID(t *testing.T) {
|
|
|
|
in := samplePayloadWithID()
|
2015-07-08 16:11:03 -04:00
|
|
|
out := new(Blog)
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-07-10 12:07:12 -04:00
|
|
|
if err := UnmarshalPayload(in, out); err != nil {
|
2015-07-08 16:11:03 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2016-07-05 21:32:15 -04:00
|
|
|
if out.ID != 2 {
|
|
|
|
t.Fatalf("Did not set ID on dst interface")
|
2015-07-08 16:11:03 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-06 17:41:53 -04:00
|
|
|
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
func TestUnmarshal_nonNumericID(t *testing.T) {
|
|
|
|
data := samplePayloadWithoutIncluded()
|
|
|
|
data["data"].(map[string]interface{})["id"] = "non-numeric-id"
|
2018-10-05 11:56:32 -04:00
|
|
|
payload, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
in := bytes.NewReader(payload)
|
|
|
|
out := new(Post)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(in, out); err != ErrBadJSONAPIID {
|
|
|
|
t.Fatalf(
|
|
|
|
"Was expecting a `%s` error, got `%s`",
|
|
|
|
ErrBadJSONAPIID,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:41:53 -04:00
|
|
|
func TestUnmarshalSetsAttrs(t *testing.T) {
|
2015-07-08 14:49:36 -04:00
|
|
|
out, err := unmarshalSamplePayload()
|
|
|
|
if err != nil {
|
2015-07-06 17:41:53 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
|
|
|
if out.CreatedAt.IsZero() {
|
|
|
|
t.Fatalf("Did not parse time")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.ViewCount != 1000 {
|
|
|
|
t.Fatalf("View count not properly serialized")
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 17:41:53 -04:00
|
|
|
|
2021-04-05 14:13:02 -04:00
|
|
|
func TestUnmarshal_Times(t *testing.T) {
|
|
|
|
aTime := time.Date(2016, 8, 17, 8, 27, 12, 0, time.UTC)
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
desc string
|
|
|
|
inputPayload *OnePayload
|
|
|
|
wantErr bool
|
|
|
|
verification func(tm *TimestampModel) error
|
|
|
|
}{
|
|
|
|
// Default:
|
|
|
|
{
|
|
|
|
desc: "default_byValue",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"defaultv": aTime.Unix(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verification: func(tm *TimestampModel) error {
|
|
|
|
if !tm.DefaultV.Equal(aTime) {
|
|
|
|
return errors.New("times not equal!")
|
|
|
|
}
|
|
|
|
return nil
|
2016-09-22 16:58:07 -04:00
|
|
|
},
|
|
|
|
},
|
2021-04-05 14:13:02 -04:00
|
|
|
{
|
|
|
|
desc: "default_byPointer",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"defaultp": aTime.Unix(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verification: func(tm *TimestampModel) error {
|
|
|
|
if !tm.DefaultP.Equal(aTime) {
|
|
|
|
return errors.New("times not equal!")
|
|
|
|
}
|
|
|
|
return nil
|
2016-09-22 16:58:07 -04:00
|
|
|
},
|
|
|
|
},
|
2021-04-05 14:13:02 -04:00
|
|
|
{
|
|
|
|
desc: "default_invalid",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"defaultv": "not a timestamp!",
|
|
|
|
},
|
|
|
|
},
|
2016-09-22 16:58:07 -04:00
|
|
|
},
|
2021-04-05 14:13:02 -04:00
|
|
|
wantErr: true,
|
2016-09-22 16:58:07 -04:00
|
|
|
},
|
2021-04-05 14:13:02 -04:00
|
|
|
// ISO 8601:
|
|
|
|
{
|
|
|
|
desc: "iso8601_byValue",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"iso8601v": "2016-08-17T08:27:12Z",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verification: func(tm *TimestampModel) error {
|
|
|
|
if !tm.ISO8601V.Equal(aTime) {
|
|
|
|
return errors.New("times not equal!")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "iso8601_byPointer",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"iso8601p": "2016-08-17T08:27:12Z",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verification: func(tm *TimestampModel) error {
|
|
|
|
if !tm.ISO8601P.Equal(aTime) {
|
|
|
|
return errors.New("times not equal!")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "iso8601_invalid",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"iso8601v": "not a timestamp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
// RFC 3339
|
|
|
|
{
|
|
|
|
desc: "rfc3339_byValue",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"rfc3339v": "2016-08-17T08:27:12Z",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verification: func(tm *TimestampModel) error {
|
|
|
|
if got, want := tm.RFC3339V, aTime; got != want {
|
|
|
|
return fmt.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "rfc3339_byPointer",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"rfc3339p": "2016-08-17T08:27:12Z",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verification: func(tm *TimestampModel) error {
|
|
|
|
if got, want := *tm.RFC3339P, aTime; got != want {
|
|
|
|
return fmt.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "rfc3339_invalid",
|
|
|
|
inputPayload: &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
Type: "timestamps",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"rfc3339v": "not a timestamp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
// Serialize the OnePayload using the standard JSON library.
|
|
|
|
in := bytes.NewBuffer(nil)
|
|
|
|
if err := json.NewEncoder(in).Encode(tc.inputPayload); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2021-04-05 14:13:02 -04:00
|
|
|
out := &TimestampModel{}
|
|
|
|
err := UnmarshalPayload(in, out)
|
|
|
|
if got, want := (err != nil), tc.wantErr; got != want {
|
|
|
|
t.Fatalf("UnmarshalPayload error: got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
if tc.verification != nil {
|
|
|
|
if err := tc.verification(out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2016-09-22 16:58:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 21:32:04 -04:00
|
|
|
func TestUnmarshalRelationshipsWithoutIncluded(t *testing.T) {
|
2018-10-05 11:56:32 -04:00
|
|
|
data, err := json.Marshal(samplePayloadWithoutIncluded())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-13 21:32:04 -04:00
|
|
|
in := bytes.NewReader(data)
|
|
|
|
out := new(Post)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify each comment has at least an ID
|
|
|
|
for _, comment := range out.Comments {
|
2016-07-15 16:40:23 -04:00
|
|
|
if comment.ID == 0 {
|
2016-07-13 21:32:04 -04:00
|
|
|
t.Fatalf("The comment did not have an ID")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 12:34:37 -04:00
|
|
|
func TestUnmarshalRelationships(t *testing.T) {
|
2015-07-08 14:49:36 -04:00
|
|
|
out, err := unmarshalSamplePayload()
|
|
|
|
if err != nil {
|
2015-07-08 12:34:37 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost == nil {
|
|
|
|
t.Fatalf("Current post was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost.Title != "Bas" || out.CurrentPost.Body != "Fuubar" {
|
2015-07-10 14:41:54 -04:00
|
|
|
t.Fatalf("Attributes were not set")
|
2015-07-08 12:34:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Posts) != 2 {
|
|
|
|
t.Fatalf("There should have been 2 posts")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:13:04 -05:00
|
|
|
func TestUnmarshalNullRelationship(t *testing.T) {
|
|
|
|
sample := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "posts",
|
|
|
|
"id": "1",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"body": "Hello",
|
|
|
|
"title": "World",
|
|
|
|
},
|
|
|
|
"relationships": map[string]interface{}{
|
|
|
|
"latest_comment": map[string]interface{}{
|
|
|
|
"data": nil, // empty to-one relationship
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(sample)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
in := bytes.NewReader(data)
|
|
|
|
out := new(Post)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.LatestComment != nil {
|
|
|
|
t.Fatalf("Latest Comment was not set to nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNullRelationshipInSlice(t *testing.T) {
|
|
|
|
sample := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "posts",
|
|
|
|
"id": "1",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"body": "Hello",
|
|
|
|
"title": "World",
|
|
|
|
},
|
|
|
|
"relationships": map[string]interface{}{
|
|
|
|
"comments": map[string]interface{}{
|
|
|
|
"data": []interface{}{}, // empty to-many relationships
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(sample)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
in := bytes.NewReader(data)
|
|
|
|
out := new(Post)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Comments) != 0 {
|
|
|
|
t.Fatalf("Wrong number of comments; Comments should be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 12:34:37 -04:00
|
|
|
func TestUnmarshalNestedRelationships(t *testing.T) {
|
2015-07-08 14:49:36 -04:00
|
|
|
out, err := unmarshalSamplePayload()
|
|
|
|
if err != nil {
|
2015-07-08 12:34:37 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost == nil {
|
|
|
|
t.Fatalf("Current post was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost.Comments == nil {
|
|
|
|
t.Fatalf("Did not materialize nested records, comments")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.CurrentPost.Comments) != 2 {
|
|
|
|
t.Fatalf("Wrong number of comments")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 10:42:52 -04:00
|
|
|
func TestUnmarshalRelationshipsSerializedEmbedded(t *testing.T) {
|
|
|
|
out := sampleSerializedEmbeddedTestModel()
|
|
|
|
|
|
|
|
if out.CurrentPost == nil {
|
|
|
|
t.Fatalf("Current post was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost.Title != "Foo" || out.CurrentPost.Body != "Bar" {
|
|
|
|
t.Fatalf("Attributes were not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Posts) != 2 {
|
|
|
|
t.Fatalf("There should have been 2 posts")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Posts[0].LatestComment.Body != "foo" {
|
|
|
|
t.Fatalf("The comment body was not set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:34:04 -04:00
|
|
|
func TestUnmarshalNestedRelationshipsEmbedded(t *testing.T) {
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
if err := MarshalOnePayloadEmbedded(out, testModel()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
model := new(Blog)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(out, model); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if model.CurrentPost == nil {
|
|
|
|
t.Fatalf("Current post was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if model.CurrentPost.Comments == nil {
|
|
|
|
t.Fatalf("Did not materialize nested records, comments")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(model.CurrentPost.Comments) != 2 {
|
|
|
|
t.Fatalf("Wrong number of comments")
|
|
|
|
}
|
|
|
|
|
|
|
|
if model.CurrentPost.Comments[0].Body != "foo" {
|
|
|
|
t.Fatalf("Comment body not set")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 14:41:54 -04:00
|
|
|
func TestUnmarshalRelationshipsSideloaded(t *testing.T) {
|
|
|
|
payload := samplePayloadWithSideloaded()
|
|
|
|
out := new(Blog)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(payload, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost == nil {
|
|
|
|
t.Fatalf("Current post was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost.Title != "Foo" || out.CurrentPost.Body != "Bar" {
|
|
|
|
t.Fatalf("Attributes were not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Posts) != 2 {
|
|
|
|
t.Fatalf("There should have been 2 posts")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNestedRelationshipsSideloaded(t *testing.T) {
|
|
|
|
payload := samplePayloadWithSideloaded()
|
|
|
|
out := new(Blog)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(payload, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost == nil {
|
|
|
|
t.Fatalf("Current post was not materialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.CurrentPost.Comments == nil {
|
|
|
|
t.Fatalf("Did not materialize nested records, comments")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.CurrentPost.Comments) != 2 {
|
|
|
|
t.Fatalf("Wrong number of comments")
|
|
|
|
}
|
2015-07-10 17:31:26 -04:00
|
|
|
|
|
|
|
if out.CurrentPost.Comments[0].Body != "foo" {
|
|
|
|
t.Fatalf("Comment body not set")
|
|
|
|
}
|
2015-07-10 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
func TestUnmarshalNestedRelationshipsEmbedded_withClientIDs(t *testing.T) {
|
|
|
|
model := new(Blog)
|
|
|
|
|
2015-09-17 18:55:53 -04:00
|
|
|
if err := UnmarshalPayload(samplePayload(), model); err != nil {
|
2015-09-10 18:55:51 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-05 21:32:15 -04:00
|
|
|
if model.Posts[0].ClientID == "" {
|
|
|
|
t.Fatalf("ClientID not set from request on related record")
|
2015-09-10 18:55:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 14:49:36 -04:00
|
|
|
func unmarshalSamplePayload() (*Blog, error) {
|
|
|
|
in := samplePayload()
|
|
|
|
out := new(Blog)
|
|
|
|
|
2015-07-10 12:07:12 -04:00
|
|
|
if err := UnmarshalPayload(in, out); err != nil {
|
2015-07-08 14:49:36 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 18:25:13 -05:00
|
|
|
func TestUnmarshalManyPayload(t *testing.T) {
|
|
|
|
sample := map[string]interface{}{
|
|
|
|
"data": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "posts",
|
|
|
|
"id": "1",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"body": "First",
|
|
|
|
"title": "Post",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "posts",
|
|
|
|
"id": "2",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"body": "Second",
|
|
|
|
"title": "Post",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(sample)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
in := bytes.NewReader(data)
|
|
|
|
|
|
|
|
posts, err := UnmarshalManyPayload(in, reflect.TypeOf(new(Post)))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(posts) != 2 {
|
|
|
|
t.Fatal("Wrong number of posts")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range posts {
|
|
|
|
_, ok := p.(*Post)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Was expecting a Post")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManyPayload_withLinks(t *testing.T) {
|
|
|
|
firstPageURL := "http://somesite.com/movies?page[limit]=50&page[offset]=50"
|
|
|
|
prevPageURL := "http://somesite.com/movies?page[limit]=50&page[offset]=0"
|
|
|
|
nextPageURL := "http://somesite.com/movies?page[limit]=50&page[offset]=100"
|
|
|
|
lastPageURL := "http://somesite.com/movies?page[limit]=50&page[offset]=500"
|
|
|
|
|
|
|
|
sample := map[string]interface{}{
|
|
|
|
"data": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "posts",
|
|
|
|
"id": "1",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"body": "First",
|
|
|
|
"title": "Post",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "posts",
|
|
|
|
"id": "2",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"body": "Second",
|
|
|
|
"title": "Post",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"links": map[string]interface{}{
|
|
|
|
KeyFirstPage: firstPageURL,
|
|
|
|
KeyPreviousPage: prevPageURL,
|
|
|
|
KeyNextPage: nextPageURL,
|
|
|
|
KeyLastPage: lastPageURL,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(sample)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
in := bytes.NewReader(data)
|
|
|
|
|
|
|
|
payload := new(ManyPayload)
|
|
|
|
if err = json.NewDecoder(in).Decode(payload); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Links == nil {
|
|
|
|
t.Fatal("Was expecting a non nil ptr Link field")
|
|
|
|
}
|
|
|
|
|
|
|
|
links := *payload.Links
|
|
|
|
|
|
|
|
first, ok := links[KeyFirstPage]
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Was expecting a non nil ptr Link field")
|
|
|
|
}
|
|
|
|
if e, a := firstPageURL, first; e != a {
|
|
|
|
t.Fatalf("Was expecting links.%s to have a value of %s, got %s", KeyFirstPage, e, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
prev, ok := links[KeyPreviousPage]
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Was expecting a non nil ptr Link field")
|
|
|
|
}
|
|
|
|
if e, a := prevPageURL, prev; e != a {
|
|
|
|
t.Fatalf("Was expecting links.%s to have a value of %s, got %s", KeyPreviousPage, e, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
next, ok := links[KeyNextPage]
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Was expecting a non nil ptr Link field")
|
|
|
|
}
|
|
|
|
if e, a := nextPageURL, next; e != a {
|
|
|
|
t.Fatalf("Was expecting links.%s to have a value of %s, got %s", KeyNextPage, e, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
last, ok := links[KeyLastPage]
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Was expecting a non nil ptr Link field")
|
|
|
|
}
|
|
|
|
if e, a := lastPageURL, last; e != a {
|
|
|
|
t.Fatalf("Was expecting links.%s to have a value of %s, got %s", KeyLastPage, e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 09:04:52 -04:00
|
|
|
func TestUnmarshalCustomTypeAttributes(t *testing.T) {
|
|
|
|
customInt := CustomIntType(5)
|
|
|
|
customFloat := CustomFloatType(1.5)
|
|
|
|
customString := CustomStringType("Test")
|
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "customtypes",
|
|
|
|
"id": "1",
|
|
|
|
"attributes": map[string]interface{}{
|
2018-10-05 10:27:18 -04:00
|
|
|
"int": 5,
|
|
|
|
"intptr": 5,
|
2018-03-28 09:04:52 -04:00
|
|
|
"intptrnull": nil,
|
|
|
|
|
2018-10-05 10:27:18 -04:00
|
|
|
"float": 1.5,
|
|
|
|
"string": "Test",
|
2018-03-28 09:04:52 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-10-05 11:56:32 -04:00
|
|
|
payload, err := json.Marshal(data)
|
2018-03-28 09:04:52 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse JSON API payload
|
|
|
|
customAttributeTypes := new(CustomAttributeTypes)
|
|
|
|
if err := UnmarshalPayload(bytes.NewReader(payload), customAttributeTypes); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected, actual := customInt, customAttributeTypes.Int; expected != actual {
|
2018-03-28 09:14:32 -04:00
|
|
|
t.Fatalf("Was expecting custom int to be `%d`, got `%d`", expected, actual)
|
2018-03-28 09:04:52 -04:00
|
|
|
}
|
|
|
|
if expected, actual := customInt, *customAttributeTypes.IntPtr; expected != actual {
|
2018-03-28 09:14:32 -04:00
|
|
|
t.Fatalf("Was expecting custom int pointer to be `%d`, got `%d`", expected, actual)
|
2018-03-28 09:04:52 -04:00
|
|
|
}
|
|
|
|
if customAttributeTypes.IntPtrNull != nil {
|
2018-03-28 09:14:32 -04:00
|
|
|
t.Fatalf("Was expecting custom int pointer to be <nil>, got `%d`", customAttributeTypes.IntPtrNull)
|
2018-03-28 09:04:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if expected, actual := customFloat, customAttributeTypes.Float; expected != actual {
|
2018-03-28 09:14:32 -04:00
|
|
|
t.Fatalf("Was expecting custom float to be `%f`, got `%f`", expected, actual)
|
2018-03-28 09:04:52 -04:00
|
|
|
}
|
|
|
|
if expected, actual := customString, customAttributeTypes.String; expected != actual {
|
|
|
|
t.Fatalf("Was expecting custom string to be `%s`, got `%s`", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:57:33 -04:00
|
|
|
func TestUnmarshalCustomTypeAttributes_ErrInvalidType(t *testing.T) {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "customtypes",
|
|
|
|
"id": "1",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"int": "bad",
|
|
|
|
"intptr": 5,
|
|
|
|
"intptrnull": nil,
|
|
|
|
|
|
|
|
"float": 1.5,
|
|
|
|
"string": "Test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
payload, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse JSON API payload
|
|
|
|
customAttributeTypes := new(CustomAttributeTypes)
|
|
|
|
err = UnmarshalPayload(bytes.NewReader(payload), customAttributeTypes)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected an error unmarshalling the payload due to type mismatch, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != ErrInvalidType {
|
|
|
|
t.Fatalf("Expected error to be %v, was %v", ErrInvalidType, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
func samplePayloadWithoutIncluded() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2016-07-13 21:32:04 -04:00
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "posts",
|
|
|
|
"id": "1",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"body": "Hello",
|
|
|
|
"title": "World",
|
|
|
|
},
|
|
|
|
"relationships": map[string]interface{}{
|
|
|
|
"comments": map[string]interface{}{
|
|
|
|
"data": []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "comments",
|
|
|
|
"id": "123",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"type": "comments",
|
|
|
|
"id": "456",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"latest_comment": map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "comments",
|
|
|
|
"id": "55555",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
}
|
2016-07-22 21:34:38 -04:00
|
|
|
|
2015-07-08 12:34:37 -04:00
|
|
|
func samplePayload() io.Reader {
|
2015-07-10 12:07:12 -04:00
|
|
|
payload := &OnePayload{
|
|
|
|
Data: &Node{
|
2015-07-06 17:41:53 -04:00
|
|
|
Type: "blogs",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "New blog",
|
|
|
|
"created_at": 1436216820,
|
|
|
|
"view_count": 1000,
|
|
|
|
},
|
2015-07-07 18:46:20 -04:00
|
|
|
Relationships: map[string]interface{}{
|
2015-07-10 12:07:12 -04:00
|
|
|
"posts": &RelationshipManyNode{
|
|
|
|
Data: []*Node{
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2015-07-07 18:46:20 -04:00
|
|
|
Type: "posts",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "Foo",
|
|
|
|
"body": "Bar",
|
|
|
|
},
|
2016-07-05 21:32:15 -04:00
|
|
|
ClientID: "1",
|
2015-07-07 18:46:20 -04:00
|
|
|
},
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2015-07-08 12:34:37 -04:00
|
|
|
Type: "posts",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "X",
|
|
|
|
"body": "Y",
|
|
|
|
},
|
2016-07-05 21:32:15 -04:00
|
|
|
ClientID: "2",
|
2015-07-08 12:34:37 -04:00
|
|
|
},
|
2015-07-07 18:46:20 -04:00
|
|
|
},
|
|
|
|
},
|
2015-07-10 12:07:12 -04:00
|
|
|
"current_post": &RelationshipOneNode{
|
|
|
|
Data: &Node{
|
2015-07-07 18:46:20 -04:00
|
|
|
Type: "posts",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "Bas",
|
|
|
|
"body": "Fuubar",
|
|
|
|
},
|
2016-07-05 21:32:15 -04:00
|
|
|
ClientID: "3",
|
2015-07-08 12:34:37 -04:00
|
|
|
Relationships: map[string]interface{}{
|
2015-07-10 12:07:12 -04:00
|
|
|
"comments": &RelationshipManyNode{
|
|
|
|
Data: []*Node{
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2015-07-08 12:34:37 -04:00
|
|
|
Type: "comments",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"body": "Great post!",
|
|
|
|
},
|
2016-07-05 21:32:15 -04:00
|
|
|
ClientID: "4",
|
2015-07-08 12:34:37 -04:00
|
|
|
},
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2015-07-08 12:34:37 -04:00
|
|
|
Type: "comments",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"body": "Needs some work!",
|
|
|
|
},
|
2016-07-05 21:32:15 -04:00
|
|
|
ClientID: "5",
|
2015-07-08 12:34:37 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-07-07 18:46:20 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-07-06 17:41:53 -04:00
|
|
|
},
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
json.NewEncoder(out).Encode(payload)
|
|
|
|
|
2015-07-08 12:34:37 -04:00
|
|
|
return out
|
2015-07-06 17:41:53 -04:00
|
|
|
}
|
2015-07-08 16:11:03 -04:00
|
|
|
|
2016-07-05 21:32:15 -04:00
|
|
|
func samplePayloadWithID() io.Reader {
|
2015-07-10 12:07:12 -04:00
|
|
|
payload := &OnePayload{
|
|
|
|
Data: &Node{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: "2",
|
2015-07-08 16:11:03 -04:00
|
|
|
Type: "blogs",
|
|
|
|
Attributes: map[string]interface{}{
|
|
|
|
"title": "New blog",
|
|
|
|
"view_count": 1000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
2016-07-22 21:34:38 -04:00
|
|
|
json.NewEncoder(out).Encode(payload)
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2016-07-18 17:48:35 -04:00
|
|
|
func samplePayloadWithBadTypes(m map[string]interface{}) io.Reader {
|
|
|
|
payload := &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
ID: "2",
|
|
|
|
Type: "badtypes",
|
|
|
|
Attributes: m,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
json.NewEncoder(out).Encode(payload)
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2016-07-22 21:34:38 -04:00
|
|
|
func sampleWithPointerPayload(m map[string]interface{}) io.Reader {
|
|
|
|
payload := &OnePayload{
|
|
|
|
Data: &Node{
|
|
|
|
ID: "2",
|
|
|
|
Type: "with-pointers",
|
|
|
|
Attributes: m,
|
|
|
|
},
|
|
|
|
}
|
2015-07-08 16:11:03 -04:00
|
|
|
|
2016-07-22 21:34:38 -04:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-07-08 16:11:03 -04:00
|
|
|
json.NewEncoder(out).Encode(payload)
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
2015-07-10 14:41:54 -04:00
|
|
|
|
2015-07-10 20:34:04 -04:00
|
|
|
func testModel() *Blog {
|
|
|
|
return &Blog{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 5,
|
|
|
|
ClientID: "1",
|
2015-07-10 14:41:54 -04:00
|
|
|
Title: "Title 1",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
Posts: []*Post{
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 1,
|
2015-07-10 14:41:54 -04:00
|
|
|
Title: "Foo",
|
|
|
|
Body: "Bar",
|
|
|
|
Comments: []*Comment{
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 1,
|
2015-07-10 14:41:54 -04:00
|
|
|
Body: "foo",
|
|
|
|
},
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 2,
|
2015-07-10 14:41:54 -04:00
|
|
|
Body: "bar",
|
|
|
|
},
|
|
|
|
},
|
2015-07-12 10:42:52 -04:00
|
|
|
LatestComment: &Comment{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 1,
|
2015-07-12 10:42:52 -04:00
|
|
|
Body: "foo",
|
|
|
|
},
|
2015-07-10 14:41:54 -04:00
|
|
|
},
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 2,
|
2015-07-10 14:41:54 -04:00
|
|
|
Title: "Fuubar",
|
|
|
|
Body: "Bas",
|
|
|
|
Comments: []*Comment{
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 1,
|
2015-07-10 14:41:54 -04:00
|
|
|
Body: "foo",
|
|
|
|
},
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 3,
|
2015-07-10 14:41:54 -04:00
|
|
|
Body: "bas",
|
|
|
|
},
|
|
|
|
},
|
2015-07-12 10:42:52 -04:00
|
|
|
LatestComment: &Comment{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 1,
|
2015-07-12 10:42:52 -04:00
|
|
|
Body: "foo",
|
|
|
|
},
|
2015-07-10 14:41:54 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
CurrentPost: &Post{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 1,
|
2015-07-10 14:41:54 -04:00
|
|
|
Title: "Foo",
|
|
|
|
Body: "Bar",
|
|
|
|
Comments: []*Comment{
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 1,
|
2015-07-10 14:41:54 -04:00
|
|
|
Body: "foo",
|
|
|
|
},
|
2017-09-13 15:59:59 -04:00
|
|
|
{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 2,
|
2015-07-10 14:41:54 -04:00
|
|
|
Body: "bar",
|
|
|
|
},
|
|
|
|
},
|
2015-07-12 10:42:52 -04:00
|
|
|
LatestComment: &Comment{
|
2016-07-05 21:32:15 -04:00
|
|
|
ID: 1,
|
2015-07-12 10:42:52 -04:00
|
|
|
Body: "foo",
|
|
|
|
},
|
2015-07-10 14:41:54 -04:00
|
|
|
},
|
|
|
|
}
|
2015-07-10 20:34:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func samplePayloadWithSideloaded() io.Reader {
|
|
|
|
testModel := testModel()
|
2015-07-10 14:41:54 -04:00
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
2017-06-28 20:30:09 -04:00
|
|
|
MarshalPayload(out, testModel)
|
2015-07-10 14:41:54 -04:00
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
2015-07-12 10:42:52 -04:00
|
|
|
|
|
|
|
func sampleSerializedEmbeddedTestModel() *Blog {
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
MarshalOnePayloadEmbedded(out, testModel())
|
|
|
|
|
|
|
|
blog := new(Blog)
|
|
|
|
UnmarshalPayload(out, blog)
|
|
|
|
|
|
|
|
return blog
|
|
|
|
}
|
2017-07-15 09:22:03 -04:00
|
|
|
|
2018-01-17 16:02:09 -05:00
|
|
|
func TestUnmarshalNestedStructPtr(t *testing.T) {
|
|
|
|
type Director struct {
|
2018-09-28 17:57:05 -04:00
|
|
|
Firstname string `jsonapi:"attr,firstname"`
|
|
|
|
Surname string `jsonapi:"attr,surname"`
|
2018-01-17 16:02:09 -05:00
|
|
|
}
|
|
|
|
type Movie struct {
|
|
|
|
ID string `jsonapi:"primary,movies"`
|
|
|
|
Name string `jsonapi:"attr,name"`
|
|
|
|
Director *Director `jsonapi:"attr,director"`
|
|
|
|
}
|
|
|
|
sample := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "movies",
|
|
|
|
"id": "123",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"name": "The Shawshank Redemption",
|
|
|
|
"director": map[string]interface{}{
|
|
|
|
"firstname": "Frank",
|
|
|
|
"surname": "Darabont",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(sample)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
in := bytes.NewReader(data)
|
|
|
|
out := new(Movie)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Name != "The Shawshank Redemption" {
|
|
|
|
t.Fatalf("expected out.Name to be `The Shawshank Redemption`, but got `%s`", out.Name)
|
|
|
|
}
|
|
|
|
if out.Director.Firstname != "Frank" {
|
|
|
|
t.Fatalf("expected out.Director.Firstname to be `Frank`, but got `%s`", out.Director.Firstname)
|
|
|
|
}
|
|
|
|
if out.Director.Surname != "Darabont" {
|
|
|
|
t.Fatalf("expected out.Director.Surname to be `Darabont`, but got `%s`", out.Director.Surname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 09:22:03 -04:00
|
|
|
func TestUnmarshalNestedStruct(t *testing.T) {
|
|
|
|
boss := map[string]interface{}{
|
|
|
|
"firstname": "Hubert",
|
|
|
|
"surname": "Farnsworth",
|
|
|
|
"age": 176,
|
|
|
|
"hired-at": "2016-08-17T08:27:12Z",
|
|
|
|
}
|
|
|
|
|
|
|
|
sample := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "companies",
|
|
|
|
"id": "123",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"name": "Planet Express",
|
|
|
|
"boss": boss,
|
|
|
|
"founded-at": "2016-08-17T08:27:12Z",
|
2018-09-28 17:57:05 -04:00
|
|
|
"teams": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "Dev",
|
|
|
|
"members": []map[string]interface{}{
|
|
|
|
map[string]interface{}{"firstname": "Sean"},
|
|
|
|
map[string]interface{}{"firstname": "Iz"},
|
2018-01-17 11:03:13 -05:00
|
|
|
},
|
2018-09-28 17:57:05 -04:00
|
|
|
"leader": map[string]interface{}{"firstname": "Iz"},
|
2018-01-17 11:03:13 -05:00
|
|
|
},
|
2018-09-28 17:57:05 -04:00
|
|
|
map[string]interface{}{
|
|
|
|
"name": "DxE",
|
|
|
|
"members": []map[string]interface{}{
|
|
|
|
map[string]interface{}{"firstname": "Akshay"},
|
|
|
|
map[string]interface{}{"firstname": "Peri"},
|
2018-01-17 11:03:13 -05:00
|
|
|
},
|
2018-09-28 17:57:05 -04:00
|
|
|
"leader": map[string]interface{}{"firstname": "Peri"},
|
2018-01-17 11:03:13 -05:00
|
|
|
},
|
|
|
|
},
|
2017-07-15 09:22:03 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(sample)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
in := bytes.NewReader(data)
|
|
|
|
out := new(Company)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Boss.Firstname != "Hubert" {
|
2017-11-21 06:05:50 -05:00
|
|
|
t.Fatalf("expected `Hubert` at out.Boss.Firstname, but got `%s`", out.Boss.Firstname)
|
2017-07-15 09:22:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if out.Boss.Age != 176 {
|
2017-11-21 06:05:50 -05:00
|
|
|
t.Fatalf("expected `176` at out.Boss.Age, but got `%d`", out.Boss.Age)
|
2017-07-15 09:22:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if out.Boss.HiredAt.IsZero() {
|
2017-11-21 06:15:24 -05:00
|
|
|
t.Fatalf("expected out.Boss.HiredAt to be zero, but got `%t`", out.Boss.HiredAt.IsZero())
|
2017-07-15 09:22:03 -04:00
|
|
|
}
|
2018-01-17 11:03:13 -05:00
|
|
|
|
|
|
|
if len(out.Teams) != 2 {
|
|
|
|
t.Fatalf("expected len(out.Teams) to be 2, but got `%d`", len(out.Teams))
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[0].Name != "Dev" {
|
|
|
|
t.Fatalf("expected out.Teams[0].Name to be `Dev`, but got `%s`", out.Teams[0].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[1].Name != "DxE" {
|
|
|
|
t.Fatalf("expected out.Teams[1].Name to be `DxE`, but got `%s`", out.Teams[1].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Teams[0].Members) != 2 {
|
|
|
|
t.Fatalf("expected len(out.Teams[0].Members) to be 2, but got `%d`", len(out.Teams[0].Members))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Teams[1].Members) != 2 {
|
|
|
|
t.Fatalf("expected len(out.Teams[1].Members) to be 2, but got `%d`", len(out.Teams[1].Members))
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[0].Members[0].Firstname != "Sean" {
|
|
|
|
t.Fatalf("expected out.Teams[0].Members[0].Firstname to be `Sean`, but got `%s`", out.Teams[0].Members[0].Firstname)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[0].Members[1].Firstname != "Iz" {
|
|
|
|
t.Fatalf("expected out.Teams[0].Members[1].Firstname to be `Iz`, but got `%s`", out.Teams[0].Members[1].Firstname)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[1].Members[0].Firstname != "Akshay" {
|
|
|
|
t.Fatalf("expected out.Teams[1].Members[0].Firstname to be `Akshay`, but got `%s`", out.Teams[1].Members[0].Firstname)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[1].Members[1].Firstname != "Peri" {
|
|
|
|
t.Fatalf("expected out.Teams[1].Members[1].Firstname to be `Peri`, but got `%s`", out.Teams[1].Members[1].Firstname)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[0].Leader.Firstname != "Iz" {
|
|
|
|
t.Fatalf("expected out.Teams[0].Leader.Firstname to be `Iz`, but got `%s`", out.Teams[0].Leader.Firstname)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[1].Leader.Firstname != "Peri" {
|
|
|
|
t.Fatalf("expected out.Teams[1].Leader.Firstname to be `Peri`, but got `%s`", out.Teams[1].Leader.Firstname)
|
|
|
|
}
|
2017-07-15 09:22:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNestedStructSlice(t *testing.T) {
|
|
|
|
|
|
|
|
fry := map[string]interface{}{
|
|
|
|
"firstname": "Philip J.",
|
|
|
|
"surname": "Fry",
|
|
|
|
"age": 25,
|
|
|
|
"hired-at": "2016-08-17T08:27:12Z",
|
|
|
|
}
|
|
|
|
|
|
|
|
bender := map[string]interface{}{
|
|
|
|
"firstname": "Bender Bending",
|
|
|
|
"surname": "Rodriguez",
|
|
|
|
"age": 19,
|
|
|
|
"hired-at": "2016-08-17T08:27:12Z",
|
|
|
|
}
|
|
|
|
|
|
|
|
deliveryCrew := map[string]interface{}{
|
|
|
|
"name": "Delivery Crew",
|
|
|
|
"members": []interface{}{
|
|
|
|
fry,
|
|
|
|
bender,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sample := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
"type": "companies",
|
|
|
|
"id": "123",
|
|
|
|
"attributes": map[string]interface{}{
|
|
|
|
"name": "Planet Express",
|
|
|
|
"teams": []interface{}{
|
|
|
|
deliveryCrew,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(sample)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
in := bytes.NewReader(data)
|
|
|
|
out := new(Company)
|
|
|
|
|
|
|
|
if err := UnmarshalPayload(in, out); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[0].Name != "Delivery Crew" {
|
2017-11-21 06:05:50 -05:00
|
|
|
t.Fatalf("Nested struct not unmarshalled: Expected `Delivery Crew` but got `%s`", out.Teams[0].Name)
|
2017-07-15 09:22:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Teams[0].Members) != 2 {
|
2017-11-21 06:05:50 -05:00
|
|
|
t.Fatalf("Nested struct not unmarshalled: Expected to have `2` Members but got `%d`",
|
|
|
|
len(out.Teams[0].Members))
|
2017-07-15 09:22:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if out.Teams[0].Members[0].Firstname != "Philip J." {
|
2017-11-21 06:05:50 -05:00
|
|
|
t.Fatalf("Nested struct not unmarshalled: Expected `Philip J.` but got `%s`",
|
|
|
|
out.Teams[0].Members[0].Firstname)
|
2017-07-15 09:22:03 -04:00
|
|
|
}
|
|
|
|
}
|