-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayloader.go
More file actions
120 lines (100 loc) · 3.47 KB
/
payloader.go
File metadata and controls
120 lines (100 loc) · 3.47 KB
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
package jsonapi
import (
codec "github.com/neuronlabs/neuron/codec"
)
// Payloader is used to encapsulate the One and Many payload types
type Payloader interface {
GetNodes() []*Node
GetIncluded() []*Node
SetLinks(links *TopLinks)
SetMeta(meta codec.Meta)
SetIncluded(included []*Node)
}
// compile time check for the interface implementation.
var _ Payloader = &SinglePayload{}
// SinglePayload is used to represent a generic JSON API payload where a single
// resource (Node) was included as an {} in the "data" key
type SinglePayload struct {
Links *TopLinks `json:"links,omitempty"`
Meta codec.Meta `json:"meta,omitempty"`
Data *Node `json:"data"`
Included []*Node `json:"included,omitempty"`
}
func (p *SinglePayload) GetNodes() []*Node {
return []*Node{p.Data}
}
// GetIncluded implements Payloader interface.
func (p *SinglePayload) GetIncluded() []*Node {
return p.Included
}
// SetIncluded sets the included data for the provided payload.
// Implements Payloader interface.
func (p *SinglePayload) SetIncluded(included []*Node) {
p.Included = included
}
// SetLinks sets the links for the single payload.
// Implements Payloader interface.
func (p *SinglePayload) SetLinks(links *TopLinks) {
p.Links = links
}
// SetMeta sets the meta for the single payload.
// Implements Payloader interface.
func (p *SinglePayload) SetMeta(meta codec.Meta) {
p.Meta = meta
}
// compile time check for the interface implementation.
var _ Payloader = &ManyPayload{}
// ManyPayload is used to represent a generic JSON API payload where many
// resources (Nodes) were included in an [] in the "data" key
type ManyPayload struct {
Links *TopLinks `json:"links,omitempty"`
Meta codec.Meta `json:"meta,omitempty"`
Data []*Node `json:"data"`
Included []*Node `json:"included,omitempty"`
}
func (p *ManyPayload) GetNodes() []*Node {
return p.Data
}
// GetIncluded implements Payloader.
func (p *ManyPayload) GetIncluded() []*Node {
return p.Included
}
// SetIncluded sets the included data for the provided payload.
// Implements Payloader interface.
func (p *ManyPayload) SetIncluded(included []*Node) {
p.Included = included
}
// SetLinks sets the links for the ManyPaload.
// Implements Payloader interface.
func (p *ManyPayload) SetLinks(links *TopLinks) {
p.Links = links
}
// SetMeta sets the meta for the single payload.
// Implements Payloader interface.
func (p *ManyPayload) SetMeta(meta codec.Meta) {
p.Meta = meta
}
// Meta is used to represent a `meta` object.
// http://jsonapi.org/format/#document-meta
type Meta map[string]interface{}
// Node is used to represent a generic JSON API Resource.
type Node struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
Relationships map[string]interface{} `json:"relationships,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *codec.Meta `json:"meta,omitempty"`
}
// RelationshipOneNode is used to represent a generic single JSON API relation.
type RelationshipOneNode struct {
Data *Node `json:"data"`
Links *Links `json:"links,omitempty"`
Meta *codec.Meta `json:"meta,omitempty"`
}
// RelationshipManyNode is used to represent a generic has many JSON API relation.
type RelationshipManyNode struct {
Data []*Node `json:"data"`
Links *Links `json:"links,omitempty"`
Meta *codec.Meta `json:"meta,omitempty"`
}