From e8ceb1251adfbdf32f304d590dd6ac5177430fd0 Mon Sep 17 00:00:00 2001 From: thilonel Date: Wed, 3 Jul 2019 11:28:51 +0700 Subject: [PATCH] add UnmarshalErrors(), solves #140 --- errors.go | 11 ++++++ errors_test.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/errors.go b/errors.go index 798fed0..0d9d220 100644 --- a/errors.go +++ b/errors.go @@ -15,6 +15,17 @@ func MarshalErrors(w io.Writer, errorObjects []*ErrorObject) error { return json.NewEncoder(w).Encode(&ErrorsPayload{Errors: errorObjects}) } +// UnmarshalErrors is just a wrapper for convenience +func UnmarshalErrors(in io.Reader) (*ErrorsPayload, error) { + payload := new(ErrorsPayload) + + if err := json.NewDecoder(in).Decode(payload); err != nil { + return nil, err + } + + return payload, nil +} + // ErrorsPayload is a serializer struct for representing a valid JSON API errors payload. type ErrorsPayload struct { Errors []*ErrorObject `json:"errors"` diff --git a/errors_test.go b/errors_test.go index 683a1d1..697338a 100644 --- a/errors_test.go +++ b/errors_test.go @@ -55,3 +55,98 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) { }) } } + +func TestMarshalErrors(t *testing.T) { + firstMeta := map[string]interface{}{ + "custom": "info", + "custom two": "more info", + } + secondMeta := map[string]interface{}{ + "foo": "foo info", + "bar": "bar info", + } + sample := map[string]interface{}{ + "errors": []interface{}{ + map[string]interface{}{ + "id": "1", + "title": "first title", + "detail": "first detail", + "status": "400", + "code": "first code", + "meta": firstMeta, + }, + map[string]interface{}{ + "id": "2", + "title": "second title", + "detail": "second detail", + "status": "404", + "code": "second code", + "meta": secondMeta, + }, + }, + } + + data, err := json.Marshal(sample) + if err != nil { + t.Fatal(err) + } + in := bytes.NewReader(data) + + errorsPayload, err := UnmarshalErrors(in) + if err != nil { + t.Fatal(err) + } + + expectedPayload := ErrorsPayload{ + Errors: []*ErrorObject{ + { + ID: "1", + Title: "first title", + Detail: "first detail", + Status: "400", + Code: "first code", + Meta: &firstMeta, + }, { + ID: "2", + Title: "second title", + Detail: "second detail", + Status: "404", + Code: "second code", + Meta: &secondMeta, + }, + }, + } + if !reflect.DeepEqual(*errorsPayload, expectedPayload) { + t.Fatalf("Expected: \n%#v \nto equal: \n%#v", errorsPayload, expectedPayload) + } +} + +func TestMarshalErrorsPartialData(t *testing.T) { + sample := map[string]interface{}{ + "errors": []interface{}{ + map[string]interface{}{ + "status": "400", + }, + map[string]interface{}{ + "status": "404", + }, + }, + } + + data, err := json.Marshal(sample) + if err != nil { + t.Fatal(err) + } + in := bytes.NewReader(data) + + errorsPayload, err := UnmarshalErrors(in) + if err != nil { + t.Fatal(err) + } + + expectedPayload := ErrorsPayload{Errors: []*ErrorObject{{Status: "400"}, {Status: "404"}}} + + if !reflect.DeepEqual(*errorsPayload, expectedPayload) { + t.Fatalf("Expected: \n%#v \nto equal: \n%#v", errorsPayload, expectedPayload) + } +}