forked from Mirrors/jsonapi
change interface of MarshalManyPayload to include reflect.Value for models
This commit is contained in:
parent
c7fa0d5728
commit
8f7c63b9c4
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue