forked from Mirrors/jsonapi
yay! finally figured out how to accept []interface{}
This commit is contained in:
parent
8f7c63b9c4
commit
4216780137
|
@ -7,7 +7,6 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
|
@ -39,7 +38,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, reflect.ValueOf(blogs)); err != nil {
|
||||
if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,13 +26,14 @@ func MarshalOnePayload(w io.Writer, model interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func MarshalManyPayload(w io.Writer, models reflect.Value) error {
|
||||
data := make([]*Node, 0, models.Len())
|
||||
func MarshalManyPayload(w io.Writer, models []interface{}) error {
|
||||
modelsValues := reflect.ValueOf(models)
|
||||
data := make([]*Node, 0, modelsValues.Len())
|
||||
|
||||
incl := make([]*Node, 0)
|
||||
|
||||
for i := 0; i < models.Len(); i += 1 {
|
||||
model := models.Index(i).Interface()
|
||||
for i := 0; i < modelsValues.Len(); i += 1 {
|
||||
model := modelsValues.Index(i).Interface()
|
||||
|
||||
node, included, err := visitModelNode(model, true)
|
||||
if err != nil {
|
||||
|
|
|
@ -3,7 +3,6 @@ package jsonapi
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -234,7 +233,7 @@ func TestMarshalMany(t *testing.T) {
|
|||
}
|
||||
|
||||
out := bytes.NewBuffer(nil)
|
||||
if err := MarshalManyPayload(out, reflect.ValueOf(data)); err != nil {
|
||||
if err := MarshalManyPayload(out, data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue