forked from google/jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
535 lines (459 loc) · 14.3 KB
/
request.go
File metadata and controls
535 lines (459 loc) · 14.3 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package jsonapi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"strconv"
"strings"
"time"
)
const (
unsuportedStructTagMsg = "Unsupported jsonapi tag annotation, %s"
)
var (
// ErrInvalidTime is returned when a struct has a time.Time type field, but
// the JSON value was not a unix timestamp integer.
ErrInvalidTime = errors.New("Only numbers can be parsed as dates, unix timestamps")
// ErrInvalidISO8601 is returned when a struct has a time.Time type field and includes
// "iso8601" in the tag spec, but the JSON value was not an ISO8601 timestamp string.
ErrInvalidISO8601 = errors.New("Only strings can be parsed as dates, ISO8601 timestamps")
// ErrUnknownFieldNumberType is returned when the JSON value was a float
// (numeric) but the Struct field was a non numeric type (i.e. not int, uint,
// float, etc)
ErrUnknownFieldNumberType = errors.New("The struct field was not of a known number type")
// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
// the JSON value was of a different type
ErrUnsupportedPtrType = errors.New("Pointer type in struct is not supported")
// ErrInvalidType is returned when the given type is incompatible with the expected type.
ErrInvalidType = errors.New("Invalid type provided") // I wish we used punctuation.
ptrTimeType = reflect.TypeOf(new(time.Time))
timeType = ptrTimeType.Elem()
)
// UnmarshalPayload converts an io into a struct instance using jsonapi tags on
// struct fields. This method supports single request payloads only, at the
// moment. Bulk creates and updates are not supported yet.
//
// Will Unmarshal embedded and sideloaded payloads. The latter is only possible if the
// object graph is complete. That is, in the "relationships" data there are type and id,
// keys that correspond to records in the "included" array.
//
// For example you could pass it, in, req.Body and, model, a BlogPost
// struct instance to populate in an http handler,
//
// func CreateBlog(w http.ResponseWriter, r *http.Request) {
// blog := new(Blog)
//
// if err := jsonapi.UnmarshalPayload(r.Body, blog); err != nil {
// http.Error(w, err.Error(), 500)
// return
// }
//
// // ...do stuff with your blog...
//
// w.Header().Set("Content-Type", jsonapi.MediaType)
// w.WriteHeader(201)
//
// if err := jsonapi.MarshalPayload(w, blog); err != nil {
// http.Error(w, err.Error(), 500)
// }
// }
//
//
// Visit https://github.com/google/jsonapi#create for more info.
//
// model interface{} should be a pointer to a struct.
func UnmarshalPayload(in io.Reader, model interface{}) error {
payload := new(OnePayload)
if err := json.NewDecoder(in).Decode(payload); err != nil {
return err
}
if payload.Included != nil {
includedMap := make(map[string]*Node)
for _, included := range payload.Included {
key := fmt.Sprintf("%s,%s", included.Type, included.ID)
includedMap[key] = included
}
return unmarshalNode(payload.Data, reflect.ValueOf(model), &includedMap)
}
return unmarshalNode(payload.Data, reflect.ValueOf(model), nil)
}
// UnmarshalManyPayload converts an io into a set of struct instances using
// jsonapi tags on the type's struct fields.
func UnmarshalManyPayload(in io.Reader, t reflect.Type) ([]interface{}, error) {
payload := new(ManyPayload)
if err := json.NewDecoder(in).Decode(payload); err != nil {
return nil, err
}
models := []interface{}{} // will be populated from the "data"
includedMap := map[string]*Node{} // will be populate from the "included"
if payload.Included != nil {
for _, included := range payload.Included {
key := fmt.Sprintf("%s,%s", included.Type, included.ID)
includedMap[key] = included
}
}
for _, data := range payload.Data {
model := reflect.New(t.Elem())
err := unmarshalNode(data, model, &includedMap)
if err != nil {
return nil, err
}
models = append(models, model.Interface())
}
return models, nil
}
// unmarshalNode handles embedded struct models from top to down.
// it loops through the struct fields, handles attributes/relations at that level first
// the handling the embedded structs are done last, so that you get the expected composition behavior
// data (*Node) attributes are cleared on each success.
// relations/sideloaded models use deeply copied Nodes (since those sideloaded models can be referenced in multiple relations)
func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("data is not a jsonapi representation of '%v'", model.Type())
}
}()
modelValue := model.Elem()
modelType := model.Type().Elem()
type embedded struct {
structField, model reflect.Value
}
embeddeds := []*embedded{}
for i := 0; i < modelValue.NumField(); i++ {
structField := modelType.Field(i)
fieldValue := modelValue.Field(i)
tag := structField.Tag.Get(annotationJSONAPI)
// handle explicit ignore annotation
if shouldIgnoreField(tag) {
continue
}
// handles embedded structs
if isEmbeddedStruct(structField) {
embeddeds = append(embeddeds,
&embedded{
model: reflect.ValueOf(fieldValue.Addr().Interface()),
structField: fieldValue,
},
)
continue
}
// handles pointers to embedded structs
if isEmbeddedStructPtr(structField) {
embeddeds = append(embeddeds,
&embedded{
model: reflect.ValueOf(fieldValue.Interface()),
structField: fieldValue,
},
)
continue
}
// handle tagless; after handling embedded structs (which could be tagless)
if tag == "" {
continue
}
args := strings.Split(tag, annotationSeperator)
// require atleast 1
if len(args) < 1 {
return ErrBadJSONAPIStructTag
}
// args[0] == annotation
switch args[0] {
case annotationClientID:
if err := handleClientIDUnmarshal(data, args, fieldValue); err != nil {
return err
}
case annotationPrimary:
if err := handlePrimaryUnmarshal(data, args, structField, fieldValue); err != nil {
return err
}
case annotationAttribute:
if err := handleAttributeUnmarshal(data, args, structField, fieldValue); err != nil {
return err
}
case annotationRelation:
if err := handleRelationUnmarshal(data, args, fieldValue, included); err != nil {
return err
}
default:
return fmt.Errorf(unsuportedStructTagMsg, args[0])
}
}
// handle embedded last
for _, em := range embeddeds {
// if nil, need to construct and rollback accordingly
if em.model.IsNil() {
copy := deepCopyNode(data)
tmp := reflect.New(em.model.Type().Elem())
if err := unmarshalNode(copy, tmp, included); err != nil {
return err
}
// had changes; assign value to struct field, replace orig node (data) w/ mutated copy
if !reflect.DeepEqual(copy, data) {
assign(em.structField, tmp)
data = copy
}
} else {
// handle non-nil scenarios
if err := unmarshalNode(data, em.model, included); err != nil {
return err
}
}
}
return nil
}
func handleClientIDUnmarshal(data *Node, args []string, fieldValue reflect.Value) error {
if len(args) != 1 {
return ErrBadJSONAPIStructTag
}
if data.ClientID == "" {
return nil
}
// set value and clear clientID to denote it's already been processed
fieldValue.Set(reflect.ValueOf(data.ClientID))
data.ClientID = ""
return nil
}
func handlePrimaryUnmarshal(data *Node, args []string, fieldType reflect.StructField, fieldValue reflect.Value) error {
if len(args) < 2 {
return ErrBadJSONAPIStructTag
}
if data.ID == "" {
return nil
}
// Check the JSON API Type
if data.Type != args[1] {
return fmt.Errorf(
"Trying to Unmarshal an object of type %#v, but %#v does not match",
data.Type,
args[1],
)
}
// Deal with PTRS
var kind reflect.Kind
if fieldValue.Kind() == reflect.Ptr {
kind = fieldType.Type.Elem().Kind()
} else {
kind = fieldType.Type.Kind()
}
switch kind {
default:
// only handle strings and numerics
return ErrBadJSONAPIID
case reflect.String:
assign(fieldValue, reflect.ValueOf(data.ID))
case
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fv, err := strconv.ParseFloat(data.ID, 64)
if err != nil {
return ErrBadJSONAPIID
}
b, err := json.Marshal(fv)
if err != nil {
return err
}
v := fieldValue.Addr().Interface()
if err := json.Unmarshal(b, v); err != nil {
return err
}
}
// clear ID to denote it's already been processed
data.ID = ""
return nil
}
func handleRelationUnmarshal(data *Node, args []string, fieldValue reflect.Value, included *map[string]*Node) error {
if len(args) < 2 {
return ErrBadJSONAPIStructTag
}
if data.Relationships == nil || data.Relationships[args[1]] == nil {
return nil
}
// to-one relationships
handler := handleToOneRelationUnmarshal
isSlice := fieldValue.Type().Kind() == reflect.Slice
if isSlice {
// to-many relationship
handler = handleToManyRelationUnmarshal
}
v, err := handler(data.Relationships[args[1]], fieldValue.Type(), included)
if err != nil {
return err
}
// set only if there is a val since val can be null (e.g. to disassociate the relationship)
if v != nil {
fieldValue.Set(*v)
}
delete(data.Relationships, args[1])
return nil
}
// to-one relationships
func handleToOneRelationUnmarshal(relationData interface{}, fieldType reflect.Type, included *map[string]*Node) (*reflect.Value, error) {
relationship := new(RelationshipOneNode)
buf := bytes.NewBuffer(nil)
json.NewEncoder(buf).Encode(relationData)
json.NewDecoder(buf).Decode(relationship)
m := reflect.New(fieldType.Elem())
/*
http://jsonapi.org/format/#document-resource-object-relationships
http://jsonapi.org/format/#document-resource-object-linkage
relationship can have a data node set to null (e.g. to disassociate the relationship)
so unmarshal and set fieldValue only if data obj is not null
*/
if relationship.Data == nil {
return nil, nil
}
if err := unmarshalNode(
fullNode(relationship.Data, included),
m,
included,
); err != nil {
return nil, err
}
return &m, nil
}
// to-many relationship
func handleToManyRelationUnmarshal(relationData interface{}, fieldType reflect.Type, included *map[string]*Node) (*reflect.Value, error) {
relationship := new(RelationshipManyNode)
buf := bytes.NewBuffer(nil)
json.NewEncoder(buf).Encode(relationData)
json.NewDecoder(buf).Decode(relationship)
models := reflect.New(fieldType).Elem()
rData := relationship.Data
for _, n := range rData {
m := reflect.New(fieldType.Elem().Elem())
if err := unmarshalNode(
fullNode(n, included),
m,
included,
); err != nil {
return nil, err
}
models = reflect.Append(models, m)
}
return &models, nil
}
// handleAttributeUnmarshal
func handleAttributeUnmarshal(data *Node, args []string, fieldType reflect.StructField, fieldValue reflect.Value) error {
if len(args) < 2 {
return ErrBadJSONAPIStructTag
}
attributes := data.Attributes
if attributes == nil || len(data.Attributes) == 0 {
return nil
}
val := attributes[args[1]]
// continue if the attribute was not included in the request
if val == nil {
return nil
}
// custom handling of time
if isTimeValue(fieldValue) {
return handleTimeAttributes(data, args, fieldValue, fieldType)
}
// standard attributes that the json package knows how to handle, plus implementions on json.Unmarshaler
return handleWithJSONMarshaler(data, args, fieldValue)
}
func fullNode(n *Node, included *map[string]*Node) *Node {
includedKey := fmt.Sprintf("%s,%s", n.Type, n.ID)
if included != nil && (*included)[includedKey] != nil {
return deepCopyNode((*included)[includedKey])
}
return deepCopyNode(n)
}
// assign will take the value specified and assign it to the field; if
// field is expecting a ptr assign will assign a ptr.
func assign(field, value reflect.Value) {
if field.Kind() == reflect.Ptr {
field.Set(value)
} else {
field.Set(reflect.Indirect(value))
}
}
// handleTimeAttributes - handle field of type time.Time and *time.Time
// TODO: consider refactoring/removing this toggling (would be a breaking change)
// standard time.Time implements RFC3339 (https://golang.org/pkg/time/#Time.UnmarshalJSON) but is overridden here.
// jsonapi doesn't specify, but does recommends ISO8601 (http://jsonapi.org/recommendations/#date-and-time-fields)
// IMHO (skimata): just default on recommended ISO8601, all others desired formats
// should implement w/ a custom marshaler/unmarshaler
func handleTimeAttributes(data *Node, args []string, fieldValue reflect.Value, structField reflect.StructField) error {
b, err := json.Marshal(data.Attributes[args[1]])
if err != nil {
return err
}
var tm time.Time
if useISO8601(args) {
iso := &iso8601Datetime{}
if err := iso.UnmarshalJSON(b); err != nil {
return err
}
tm = iso.Time
} else {
epoch := &unix{}
if err := epoch.UnmarshalJSON(b); err != nil {
return err
}
tm = epoch.Time
}
if structField.Type.Kind() == reflect.Ptr {
fieldValue.Set(reflect.ValueOf(&tm))
} else {
fieldValue.Set(reflect.ValueOf(tm))
}
delete(data.Attributes, args[1])
return nil
}
func handleWithJSONMarshaler(data *Node, args []string, fieldValue reflect.Value) error {
v := fieldValue.Addr().Interface()
b, err := json.Marshal(data.Attributes[args[1]])
if err != nil {
return err
}
if err := json.Unmarshal(b, v); err != nil {
return err
}
// success; clear value
delete(data.Attributes, args[1])
return nil
}
func isTimeValue(fieldValue reflect.Value) bool {
switch fieldValue.Type() {
default:
return false
case timeType, ptrTimeType:
return true
}
}
func hasStandardJSONSupport(structField reflect.StructField) bool {
kind := structField.Type.Kind()
if kind == reflect.Ptr {
kind = structField.Type.Elem().Kind()
}
switch kind {
default:
return false
case reflect.Map, reflect.Slice:
return hasStandardJSONSupport(reflect.StructField{Type: structField.Type.Elem()})
case
reflect.Bool,
reflect.String,
reflect.Complex64, reflect.Complex128,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Uintptr,
reflect.Float32, reflect.Float64:
return true
}
}
func useISO8601(args []string) bool {
if len(args) > 2 {
for _, arg := range args[2:] {
if arg == annotationISO8601 {
return true
}
}
}
return false
}