forked from mfcochauxlaberge/jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
143 lines (127 loc) · 3.02 KB
/
document.go
File metadata and controls
143 lines (127 loc) · 3.02 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package jsonapi
// Document ...
type Document struct {
// Data
Data interface{}
// Included
Included map[string]Resource
// References
Resources map[string]map[string]struct{}
Links map[string]Link
// Relationships where data has to be included in payload
RelData map[string][]string
// Top-level members
Meta map[string]interface{}
// Errors
Errors []Error
// Internal
PrePath string
}
// NewDocument ...
func NewDocument() *Document {
return &Document{
Included: map[string]Resource{},
Resources: map[string]map[string]struct{}{},
Links: map[string]Link{},
RelData: map[string][]string{},
Meta: map[string]interface{}{},
}
}
// Include ...
func (d *Document) Include(res Resource) {
key := res.GetType() + " " + res.GetID()
if len(d.Included) == 0 {
d.Included = map[string]Resource{}
}
if dres, ok := d.Data.(Resource); ok {
// Check resource
rkey := dres.GetID() + " " + dres.GetType()
if rkey == key {
return
}
} else if col, ok := d.Data.(Collection); ok {
// Check Collection
ctyp := col.Sample().GetType()
if ctyp == res.GetType() {
for i := 0; i < col.Len(); i++ {
rkey := col.Elem(i).GetID() + " " + col.Elem(i).GetType()
if rkey == key {
return
}
}
}
}
// Check already included resources
if _, ok := d.Included[key]; ok {
return
}
d.Included[key] = res
}
// MarshalJSON ...
// func (d *Document) Marshal() ([]byte, error) {
// // Data
// var data json.RawMessage
// var errors json.RawMessage
// var err error
// if d.Errors != nil {
// errors, err = json.Marshal(d.Errors)
// } else if res, ok := d.Data.(Resource); ok {
// _, typ := res.IDAndType()
// data, err = marshalResource(res, d.URL.Host, d.URL.Params.Fields[typ], d.RelData)
// } else if col, ok := d.Data.(Collection); ok {
// data, err = marshalCollection(col, d.URL.Host, d.URL.Params.Fields[col.Type()], d.RelData)
// } else if id, ok := d.Data.(Identifier); ok {
// data, err = json.Marshal(id)
// } else if ids, ok := d.Data.(Identifiers); ok {
// data, err = json.Marshal(ids)
// } else {
// data = []byte("null")
// }
//
// if err != nil {
// return []byte{}, err
// }
//
// // Included
// inclusions := []*json.RawMessage{}
// if len(data) > 0 {
// for key := range d.Included {
// _, typ := d.Included[key].IDAndType()
// raw, err := marshalResource(d.Included[key], d.URL.Host, d.URL.Params.Fields[typ], d.RelData)
// if err != nil {
// return []byte{}, err
// }
// rawm := json.RawMessage(raw)
// inclusions = append(inclusions, &rawm)
// }
// }
//
// // Marshaling
// plMap := map[string]interface{}{}
//
// if len(data) > 0 {
// plMap["data"] = data
// }
//
// if len(d.Links) > 0 {
// plMap["links"] = d.Links
// }
//
// if len(errors) > 0 {
// plMap["errors"] = errors
// }
//
// if len(inclusions) > 0 {
// plMap["included"] = inclusions
// }
//
// if len(d.Meta) > 0 {
// plMap["meta"] = d.Meta
// }
//
// if len(d.JSONAPI) > 0 {
// plMap["jsonapi"] = d.JSONAPI
// }
//
// return json.Marshal(plMap)
// }