-
Notifications
You must be signed in to change notification settings - Fork 9
/
marshal_relationships_example_test.go
61 lines (51 loc) · 2.14 KB
/
marshal_relationships_example_test.go
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
package jsonapi_test
import (
"fmt"
"github.com/DataDog/jsonapi"
)
type Author struct {
ID string `jsonapi:"primary,author"`
Name string `jsonapi:"attribute" json:"name"`
}
type Comment struct {
ID string `jsonapi:"primary,comments"`
Body string `jsonapi:"attribute" json:"comment"`
Author *Author `jsonapi:"relationship"`
}
func (c *Comment) LinkRelation(relation string) *jsonapi.Link {
return &jsonapi.Link{
Self: fmt.Sprintf("http://example.com/comments/%s/relationships/%s", c.ID, relation),
Related: fmt.Sprintf("http://example.com/comments/%s/%s", c.ID, relation),
}
}
type Article struct {
ID string `jsonapi:"primary,articles"`
Title string `jsonapi:"attribute" json:"title"`
Author *Author `jsonapi:"relationship" json:"author,omitempty"`
Comments []*Comment `jsonapi:"relationship" json:"comments,omitempty"`
}
func (a *Article) LinkRelation(relation string) *jsonapi.Link {
return &jsonapi.Link{
Self: fmt.Sprintf("http://example.com/articles/%s/relationships/%s", a.ID, relation),
Related: fmt.Sprintf("http://example.com/articles/%s/%s", a.ID, relation),
}
}
func ExampleMarshal_relationships() {
authorA := &Author{ID: "AA", Name: "Cool Author"}
authorB := &Author{ID: "AB", Name: "Cool Commenter"}
authorC := &Author{ID: "AC", Name: "Neat Commenter"}
commentA := &Comment{ID: "CA", Body: "Very cool", Author: authorB}
commentB := &Comment{ID: "CB", Body: "Super neat", Author: authorC}
article := Article{
ID: "1",
Title: "Hello World",
Author: authorA,
Comments: []*Comment{commentA, commentB},
}
b, err := jsonapi.Marshal(&article)
if err != nil {
panic(err)
}
fmt.Printf("%s", string(b))
// Output: {"data":{"id":"1","type":"articles","attributes":{"title":"Hello World"},"relationships":{"author":{"data":{"id":"AA","type":"author"},"links":{"self":"http://example.com/articles/1/relationships/author","related":"http://example.com/articles/1/author"}},"comments":{"data":[{"id":"CA","type":"comments"},{"id":"CB","type":"comments"}],"links":{"self":"http://example.com/articles/1/relationships/comments","related":"http://example.com/articles/1/comments"}}}}}
}