forked from Mirrors/jsonapi
move a method higher for godoc
This commit is contained in:
parent
9b8432d9d9
commit
68c4bcfbb0
56
response.go
56
response.go
|
@ -10,6 +10,34 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func MarshalOnePayload(w io.Writer, model interface{}) error {
|
||||||
|
rootNode, included, err := visitModelNode(model, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := &OnePayload{Data: rootNode}
|
||||||
|
|
||||||
|
uniqueIncluded := make(map[string]*Node)
|
||||||
|
|
||||||
|
for i, n := range included {
|
||||||
|
k := fmt.Sprintf("%s,%s", n.Type, n.Id)
|
||||||
|
if uniqueIncluded[k] == nil {
|
||||||
|
uniqueIncluded[k] = n
|
||||||
|
} else {
|
||||||
|
included = deleteNode(included, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
payload.Included = included
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func MarshalManyPayload(w io.Writer, models Models) error {
|
func MarshalManyPayload(w io.Writer, models Models) error {
|
||||||
d := models.GetData()
|
d := models.GetData()
|
||||||
data := make([]*Node, 0, len(d))
|
data := make([]*Node, 0, len(d))
|
||||||
|
@ -60,34 +88,6 @@ func MarshalOnePayloadEmbedded(model interface{}) (*OnePayload, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MarshalOnePayload(w io.Writer, model interface{}) error {
|
|
||||||
rootNode, included, err := visitModelNode(model, true)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
payload := &OnePayload{Data: rootNode}
|
|
||||||
|
|
||||||
uniqueIncluded := make(map[string]*Node)
|
|
||||||
|
|
||||||
for i, n := range included {
|
|
||||||
k := fmt.Sprintf("%s,%s", n.Type, n.Id)
|
|
||||||
if uniqueIncluded[k] == nil {
|
|
||||||
uniqueIncluded[k] = n
|
|
||||||
} else {
|
|
||||||
included = deleteNode(included, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
payload.Included = included
|
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func visitModelNode(model interface{}, sideload bool) (*Node, []*Node, error) {
|
func visitModelNode(model interface{}, sideload bool) (*Node, []*Node, error) {
|
||||||
node := new(Node)
|
node := new(Node)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue