From 8f7c63b9c4f1a36418df96f9d49db58e835954fd Mon Sep 17 00:00:00 2001 From: Sam Woodard Date: Sun, 12 Jul 2015 11:37:31 -0700 Subject: [PATCH] change interface of MarshalManyPayload to include reflect.Value for models --- examples/app.go | 17 ++++------------- response.go | 9 +++++---- response_test.go | 15 +++------------ 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/examples/app.go b/examples/app.go index fbdf03f..4d9e042 100644 --- a/examples/app.go +++ b/examples/app.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/http/httptest" + "reflect" "regexp" "time" @@ -38,7 +39,7 @@ func listBlogs(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Header().Set("Content-Type", "application/vnd.api+json") - if err := jsonapi.MarshalManyPayload(w, blogs); err != nil { + if err := jsonapi.MarshalManyPayload(w, reflect.ValueOf(blogs)); err != nil { http.Error(w, err.Error(), 500) } } @@ -115,8 +116,8 @@ func testBlogForCreate(i int) *Blog { } } -func testBlogsForList() Blogs { - blogs := make(Blogs, 0, 10) +func testBlogsForList() []interface{} { + blogs := make([]interface{}, 0, 10) for i := 0; i < 10; i += 1 { blogs = append(blogs, testBlogForCreate(i)) @@ -195,13 +196,3 @@ type Comment struct { PostId int `jsonapi:"attr,post_id"` Body string `jsonapi:"attr,body"` } - -type Blogs []*Blog - -func (b Blogs) GetData() []interface{} { - d := make([]interface{}, len(b)) - for i, blog := range b { - d[i] = blog - } - return d -} diff --git a/response.go b/response.go index 823cb8b..dbe7849 100644 --- a/response.go +++ b/response.go @@ -26,13 +26,14 @@ func MarshalOnePayload(w io.Writer, model interface{}) error { return nil } -func MarshalManyPayload(w io.Writer, models Models) error { - d := models.GetData() - data := make([]*Node, 0, len(d)) +func MarshalManyPayload(w io.Writer, models reflect.Value) error { + data := make([]*Node, 0, models.Len()) incl := make([]*Node, 0) - for _, model := range d { + for i := 0; i < models.Len(); i += 1 { + model := models.Index(i).Interface() + node, included, err := visitModelNode(model, true) if err != nil { return err diff --git a/response_test.go b/response_test.go index d8610b8..10a085b 100644 --- a/response_test.go +++ b/response_test.go @@ -3,6 +3,7 @@ package jsonapi import ( "bytes" "encoding/json" + "reflect" "regexp" "testing" "time" @@ -33,16 +34,6 @@ type Comment struct { Body string `jsonapi:"attr,body"` } -type Blogs []*Blog - -func (b Blogs) GetData() []interface{} { - d := make([]interface{}, len(b)) - for i, blog := range b { - d[i] = blog - } - return d -} - func TestMalformedTagResposne(t *testing.T) { testModel := &BadModel{} out := bytes.NewBuffer(nil) @@ -195,7 +186,7 @@ func TestNoRelations(t *testing.T) { } func TestMarshalMany(t *testing.T) { - data := Blogs{ + data := []interface{}{ &Blog{ Id: 5, Title: "Title 1", @@ -243,7 +234,7 @@ func TestMarshalMany(t *testing.T) { } out := bytes.NewBuffer(nil) - if err := MarshalManyPayload(out, data); err != nil { + if err := MarshalManyPayload(out, reflect.ValueOf(data)); err != nil { t.Fatal(err) }