-
Notifications
You must be signed in to change notification settings - Fork 8
/
resource.go
395 lines (331 loc) · 8.23 KB
/
resource.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package jsonapi
import (
"encoding/json"
"fmt"
"reflect"
"sort"
)
// A Resource is an element of a collection.
type Resource interface {
// Structure
Attrs() map[string]Attr
Rels() map[string]Rel
// Read
GetType() Type
Get(key string) any
// Update
Set(key string, val any)
}
// MarshalResource marshals a Resource into a JSON-encoded payload.
func MarshalResource(r Resource, prepath string, fields []string, relData map[string][]string) []byte {
mapPl := map[string]any{}
mapPl["id"] = r.Get("id").(string)
mapPl["type"] = r.GetType().Name
// Attributes
attrs := map[string]any{}
for _, attr := range r.Attrs() {
for _, field := range fields {
if field == attr.Name {
attrs[attr.Name] = r.Get(attr.Name)
break
}
}
}
if len(attrs) > 0 {
mapPl["attributes"] = attrs
}
// Relationships
rels := map[string]*json.RawMessage{}
for _, rel := range r.Rels() {
include := false
for _, field := range fields {
if field == rel.FromName {
include = true
break
}
}
if include {
var raw json.RawMessage
if rel.ToOne {
s := map[string]map[string]string{
"links": buildRelationshipLinks(r, prepath, rel.FromName),
}
for _, n := range relData[r.GetType().Name] {
if n == rel.FromName {
id := r.Get(rel.FromName).(string)
if id != "" {
s["data"] = map[string]string{
"id": r.Get(rel.FromName).(string),
"type": rel.ToType,
}
} else {
s["data"] = nil
}
break
}
}
raw, _ = json.Marshal(s)
rels[rel.FromName] = &raw
} else {
s := map[string]any{
"links": buildRelationshipLinks(r, prepath, rel.FromName),
}
for _, n := range relData[r.GetType().Name] {
if n == rel.FromName {
data := []map[string]string{}
ids := r.Get(rel.FromName).([]string)
sort.Strings(ids)
for _, id := range ids {
data = append(data, map[string]string{
"id": id,
"type": rel.ToType,
})
}
s["data"] = data
break
}
}
raw, _ = json.Marshal(s)
rels[rel.FromName] = &raw
}
}
}
if len(rels) > 0 {
mapPl["relationships"] = rels
}
// Links
mapPl["links"] = map[string]string{
"self": buildSelfLink(r, prepath),
}
// Meta
if m, ok := r.(MetaHolder); ok {
if len(m.Meta()) > 0 {
mapPl["meta"] = m.Meta()
}
}
// NOTE An error should not happen.
pl, _ := json.Marshal(mapPl)
return pl
}
// UnmarshalResource unmarshals a JSON-encoded payload into a Resource.
func UnmarshalResource(data []byte, schema *Schema) (Resource, error) {
var rske resourceSkeleton
err := json.Unmarshal(data, &rske)
if err != nil {
return nil, NewErrBadRequest(
"Invalid JSON",
"The provided JSON body could not be read.",
)
}
typ := schema.GetType(rske.Type)
res := typ.New()
res.Set("id", rske.ID)
for a, v := range rske.Attributes {
if attr, ok := typ.Attrs[a]; ok {
val, err := attr.UnmarshalToType(v)
if err != nil {
return nil, err
}
res.Set(attr.Name, val)
} else {
return nil, NewErrUnknownFieldInBody(typ.Name, a)
}
}
for r, v := range rske.Relationships {
if rel, ok := typ.Rels[r]; ok {
if len(v.Data) > 0 {
if rel.ToOne {
var iden Identifier
err = json.Unmarshal(v.Data, &iden)
res.Set(rel.FromName, iden.ID)
} else {
var idens Identifiers
err = json.Unmarshal(v.Data, &idens)
ids := make([]string, len(idens))
for i := range idens {
ids[i] = idens[i].ID
}
res.Set(rel.FromName, ids)
}
}
if err != nil {
return nil, NewErrInvalidFieldValueInBody(
rel.FromName,
string(v.Data),
typ.Name,
)
}
} else {
return nil, NewErrUnknownFieldInBody(typ.Name, r)
}
}
// Meta
if m, ok := res.(MetaHolder); ok {
m.SetMeta(rske.Meta)
}
return res, nil
}
// UnmarshalPartialResource unmarshals the given payload into a *SoftResource.
//
// The returned *SoftResource will only contain the information found in the
// payload. That means that fields not in the payload won't be part of the
// *SoftResource. Its type will be a new type whose fields will be a subset of
// the fields of the corresponding type from the schema.
//
// This is useful when handling a PATCH request where only some fields might be
// set to a value. UnmarshalResource returns a Resource where the missing fields
// are added and set to their zero value, but UnmarshalPartialResource does not
// do that. Therefore, the user is able to tell which fields have been set.
func UnmarshalPartialResource(data []byte, schema *Schema) (*SoftResource, error) {
var rske resourceSkeleton
err := json.Unmarshal(data, &rske)
if err != nil {
return nil, NewErrBadRequest(
"Invalid JSON",
"The provided JSON body could not be read.",
)
}
typ := schema.GetType(rske.Type)
newType := Type{
Name: typ.Name,
}
res := &SoftResource{
Type: &newType,
id: rske.ID,
}
for a, v := range rske.Attributes {
if attr, ok := typ.Attrs[a]; ok {
val, err := attr.UnmarshalToType(v)
if err != nil {
return nil, err
}
_ = newType.AddAttr(attr)
res.Set(attr.Name, val)
} else {
return nil, NewErrUnknownFieldInBody(typ.Name, a)
}
}
for r, v := range rske.Relationships {
if rel, ok := typ.Rels[r]; ok {
if len(v.Data) > 0 {
if rel.ToOne {
var iden Identifier
err = json.Unmarshal(v.Data, &iden)
_ = newType.AddRel(rel)
res.Set(rel.FromName, iden.ID)
} else {
var idens Identifiers
err = json.Unmarshal(v.Data, &idens)
ids := make([]string, len(idens))
for i := range idens {
ids[i] = idens[i].ID
}
_ = newType.AddRel(rel)
res.Set(rel.FromName, ids)
}
}
if err != nil {
return nil, NewErrInvalidFieldValueInBody(
rel.FromName,
string(v.Data),
typ.Name,
)
}
} else {
return nil, NewErrUnknownFieldInBody(typ.Name, r)
}
}
return res, nil
}
// Equal reports whether r1 and r2 are equal.
//
// Two resources are equal if their types are equal, all the attributes are
// equal (same type and same value), and all the relationstips are equal.
//
// IDs are ignored.
func Equal(r1, r2 Resource) bool {
// Type
if r1.GetType().Name != r2.GetType().Name {
return false
}
// Attributes
attrs := r1.Attrs()
r1Attrs := make([]Attr, 0, len(attrs))
for name := range attrs {
r1Attrs = append(r1Attrs, attrs[name])
}
sort.Slice(r1Attrs, func(i, j int) bool {
return r1Attrs[i].Name < r1Attrs[j].Name
})
attrs = r2.Attrs()
r2Attrs := make([]Attr, 0, len(attrs))
for name := range attrs {
r2Attrs = append(r2Attrs, attrs[name])
}
sort.Slice(r2Attrs, func(i, j int) bool {
return r2Attrs[i].Name < r2Attrs[j].Name
})
if len(r1Attrs) != len(r2Attrs) {
return false
}
for i, attr1 := range r1Attrs {
attr2 := r2Attrs[i]
if !reflect.DeepEqual(r1.Get(attr1.Name), r2.Get(attr2.Name)) {
// TODO Fix the following condition one day. Basically, all
// nils (nil pointer, nil slice, etc) should be considered
// equal to a nil empty interface.
if fmt.Sprintf("%v", r1.Get(attr1.Name)) == "<nil>" &&
fmt.Sprintf("%v", r2.Get(attr1.Name)) == "<nil>" {
continue
}
return false
}
}
// Relationships
rels := r1.Rels()
r1Rels := make([]Rel, 0, len(rels))
for name := range rels {
r1Rels = append(r1Rels, rels[name])
}
sort.Slice(r1Rels, func(i, j int) bool {
return r1Rels[i].FromName < r1Rels[j].FromName
})
rels = r2.Rels()
r2Rels := make([]Rel, 0, len(rels))
for name := range rels {
r2Rels = append(r2Rels, rels[name])
}
sort.Slice(r2Rels, func(i, j int) bool {
return r2Rels[i].FromName < r2Rels[j].FromName
})
if len(r1Rels) != len(r2Rels) {
return false
}
for i, rel1 := range r1Rels {
rel2 := r2Rels[i]
if rel1.ToOne != rel2.ToOne {
return false
}
if rel1.ToOne {
if r1.Get(rel1.FromName).(string) != r2.Get(rel2.FromName).(string) {
return false
}
} else {
v1 := r1.Get(rel1.FromName).([]string)
v2 := r2.Get(rel2.FromName).([]string)
if len(v1) != 0 || len(v2) != 0 {
if !reflect.DeepEqual(v1, v2) {
return false
}
}
}
}
return true
}
// EqualStrict is like Equal, but it also considers IDs.
func EqualStrict(r1, r2 Resource) bool {
if r1.Get("id").(string) != r2.Get("id").(string) {
return false
}
return Equal(r1, r2)
}