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"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -39,7 +38,7 @@ func listBlogs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
w.Header().Set("Content-Type", "application/vnd.api+json")
|
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)
|
http.Error(w, err.Error(), 500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,13 +26,14 @@ func MarshalOnePayload(w io.Writer, model interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MarshalManyPayload(w io.Writer, models reflect.Value) error {
|
func MarshalManyPayload(w io.Writer, models []interface{}) error {
|
||||||
data := make([]*Node, 0, models.Len())
|
modelsValues := reflect.ValueOf(models)
|
||||||
|
data := make([]*Node, 0, modelsValues.Len())
|
||||||
|
|
||||||
incl := make([]*Node, 0)
|
incl := make([]*Node, 0)
|
||||||
|
|
||||||
for i := 0; i < models.Len(); i += 1 {
|
for i := 0; i < modelsValues.Len(); i += 1 {
|
||||||
model := models.Index(i).Interface()
|
model := modelsValues.Index(i).Interface()
|
||||||
|
|
||||||
node, included, err := visitModelNode(model, true)
|
node, included, err := visitModelNode(model, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -3,7 +3,6 @@ package jsonapi
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -234,7 +233,7 @@ func TestMarshalMany(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
out := bytes.NewBuffer(nil)
|
out := bytes.NewBuffer(nil)
|
||||||
if err := MarshalManyPayload(out, reflect.ValueOf(data)); err != nil {
|
if err := MarshalManyPayload(out, data); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue