Skip to content

Commit

Permalink
add UnmarshalErrors(), solves google#140
Browse files Browse the repository at this point in the history
  • Loading branch information
thilonel committed Jul 3, 2019
1 parent d0428f6 commit e8ceb12
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
11 changes: 11 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
95 changes: 95 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit e8ceb12

Please sign in to comment.