-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrom_resource_example_test.go
124 lines (110 loc) · 2.6 KB
/
from_resource_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
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
114
115
116
117
118
119
120
121
122
123
124
package jsonapi_test
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/smotes/jsonapi"
)
// satisfy identityWriteAdapter on *Person
func (p *Person) SetID(id string) error {
temp, err := strconv.Atoi(id)
if err != nil {
return err
}
p.ID = temp
return nil
}
// satisfy identityWriteAdapter on *Person
func (p *Person) SetType(typ string) error {
if typ != "people" {
return errors.New("type should equal people")
}
return nil
}
// satisfy identityWriteAdapter on *Article
func (a *Article) SetID(id string) error {
temp, err := strconv.Atoi(id)
if err != nil {
return err
}
a.ID = temp
return nil
}
// satisfy identityWriteAdapter on *Article
func (a *Article) SetType(typ string) error {
if typ != "articles" {
return errors.New("type should equal articles")
}
return nil
}
// satisfy attributesWriteAdapter on *Article
func (a *Article) SetAttributes(as map[string]interface{}) error {
if v, ok := as["title"]; ok {
if v, ok := v.(string); ok {
a.Title = v
}
}
if v, ok := as["body"]; ok {
if v, ok := v.(string); ok {
a.Body = v
}
}
return nil
}
// This example includes an integration test with jsonapi.FromResource function and
// the json.Unmarshal function from the "encoding/json" package.
func ExampleFromResource() {
testArticleJSON = `
{
"id": "1",
"type": "articles",
"attributes": {
"title": "JSON API paints my bikeshed!",
"body": "The shortest article. Ever."
},
"relationships": {
"author": {
"data": {
"id": "42",
"type": "people"
},
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
}
}
},
"links": {
"self": "http://example.com/articles/1"
},
"meta": {
"total": 42,
"type": "foo"
}
}
`
p := Person{}
article := Article{}
articleResource := jsonapi.Resource{}
personResource := jsonapi.Resource{}
// first, unmarshal and convert article resource to article
if err := json.Unmarshal([]byte(testArticleJSON), &articleResource); err != nil {
panic(err)
}
if err := jsonapi.FromResource(&article, &articleResource, true); err != nil {
panic(err)
}
// person resource in author relationship must be unmarshaled separately afterwards
if rel, ok := articleResource.Relationships.Get("author"); ok {
if err := json.Unmarshal(rel.Data, &personResource); err != nil {
panic(err)
}
if err := jsonapi.FromResource(&p, &personResource, true); err != nil {
panic(err)
}
}
article.Author = &p
fmt.Println(article.ID, article.Title, article.Author.ID)
// Output: 1 JSON API paints my bikeshed! 42
}