1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
package jsonapi
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type Node struct {
ID string `json:"-"`
Content string `json:"content"`
MotherID string `json:"-"`
ChildIDs []string `json:"-"`
AbandonedChildIDs []string `json:"-"`
}
func (n *Node) GetID() string {
return n.ID
}
func (n *Node) GetReferences() []Reference {
return []Reference{
{
Type: "nodes",
Name: "mother-node",
},
{
Type: "nodes",
Name: "child-nodes",
},
{
Type: "nodes",
Name: "abandoned-child-nodes",
},
}
}
func (n *Node) GetReferencedIDs() []ReferenceID {
result := []ReferenceID{}
if n.MotherID != "" {
result = append(result, ReferenceID{Type: "nodes", Name: "mother-node", ID: n.MotherID})
}
for _, referenceID := range n.ChildIDs {
result = append(result, ReferenceID{Type: "nodes", Name: "child-nodes", ID: referenceID})
}
for _, referenceID := range n.AbandonedChildIDs {
result = append(result, ReferenceID{Type: "nodes", Name: "abandoned-child-nodes", ID: referenceID})
}
return result
}
var _ = Describe("Marshalling with the same reference type", func() {
var theNode Node
BeforeEach(func() {
theNode = Node{
ID: "super",
Content: "I am the Super Node",
MotherID: "1337",
ChildIDs: []string{"666", "42"},
AbandonedChildIDs: []string{"2", "1"},
}
})
It("marshals all the relationships of the same type", func() {
i, err := Marshal(&theNode)
Expect(err).To(BeNil())
Expect(i).To(MatchJSON(`{
"data": {
"type": "nodes",
"id": "super",
"attributes": {
"content": "I am the Super Node"
},
"relationships": {
"abandoned-child-nodes": {
"data": [
{
"type": "nodes",
"id": "2"
},
{
"type": "nodes",
"id": "1"
}
]
},
"child-nodes": {
"data": [
{
"type": "nodes",
"id": "666"
},
{
"type": "nodes",
"id": "42"
}
]
},
"mother-node": {
"data": {
"type": "nodes",
"id": "1337"
}
}
}
}
}`))
})
})
|