forked from Mirrors/jsonapi
Add MarshalManyPayloadWithoutIncluded (#56)
* fix spelling in documentation * add MarshalManyPayloadWithoutIncluded does the same as `MarshalOnePayloadWithoutIncluded` but for MarshalManyPayload * Refactored MarshalManyPayloadWithoutIncluded to use the MarshalMany func
This commit is contained in:
parent
baa7beb3f7
commit
9d919b42a6
27
response.go
27
response.go
|
@ -48,7 +48,7 @@ func MarshalOnePayload(w io.Writer, model interface{}) error {
|
|||
|
||||
// MarshalOnePayloadWithoutIncluded writes a jsonapi response with one object,
|
||||
// without the related records sideloaded into "included" array. If you want to
|
||||
// serialzie the realtions into the "included" array see MarshalOnePayload.
|
||||
// serialize the relations into the "included" array see MarshalOnePayload.
|
||||
//
|
||||
// model interface{} should be a pointer to a struct.
|
||||
func MarshalOnePayloadWithoutIncluded(w io.Writer, model interface{}) error {
|
||||
|
@ -83,6 +83,31 @@ func MarshalOne(model interface{}) (*OnePayload, error) {
|
|||
return payload, nil
|
||||
}
|
||||
|
||||
// MarshalManyPayloadWithoutIncluded writes a jsonapi response with many records,
|
||||
// without the related records sideloaded into "included" array. If you want to
|
||||
// serialize the relations into the "included" array see MarshalManyPayload.
|
||||
//
|
||||
// models interface{} should be a slice of struct pointers.
|
||||
func MarshalManyPayloadWithoutIncluded(w io.Writer, models interface{}) error {
|
||||
m, err := convertToSliceInterface(&models)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payload, err := MarshalMany(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Empty the included
|
||||
payload.Included = []*Node{}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalManyPayload writes a jsonapi response with many records, with related
|
||||
// records sideloaded, into "included" array. This method encodes a response for
|
||||
// a slice of records, hence data will be an array of records rather than a
|
||||
|
|
|
@ -715,6 +715,33 @@ func TestMarshalMany_WithSliceOfStructPointers(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMarshalManyWithoutIncluded(t *testing.T) {
|
||||
var data []*Blog
|
||||
for len(data) < 2 {
|
||||
data = append(data, testBlog())
|
||||
}
|
||||
|
||||
out := bytes.NewBuffer(nil)
|
||||
if err := MarshalManyPayloadWithoutIncluded(out, data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resp := new(ManyPayload)
|
||||
if err := json.NewDecoder(out).Decode(resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
d := resp.Data
|
||||
|
||||
if len(d) != 2 {
|
||||
t.Fatalf("data should have two elements")
|
||||
}
|
||||
|
||||
if resp.Included != nil {
|
||||
t.Fatalf("Encoding json response did not omit included")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalMany_SliceOfInterfaceAndSliceOfStructsSameJSON(t *testing.T) {
|
||||
structs := []*Book{
|
||||
&Book{ID: 1, Author: "aren55555", ISBN: "abc"},
|
||||
|
|
Loading…
Reference in New Issue