@@ -177,25 +177,25 @@ type structFieldIndex struct {
177
177
FieldNum int
178
178
}
179
179
180
- // joinStructMapping reflects on a value that may be a slice
181
- // of join structs or a join struct. A join struct is a struct
182
- // comprising of pointers to other jsonapi models, only one of
183
- // which is populated with a value by the decoder. The join struct is
184
- // probed and a data structure is generated that maps the
185
- // underlying model type (its 'primary' type) to the field number
186
- // within the join struct.
180
+ // choiceStructMapping reflects on a value that may be a slice
181
+ // of choice type structs or a choice type struct. A choice type
182
+ // struct is a struct comprising of pointers to other jsonapi models,
183
+ // only one of which is populated with a value by the decoder.
187
184
//
188
- // This data can then be used to correctly assign each data relationship
189
- // to the correct join struct field.
190
- func joinStructMapping (join reflect.Type ) (result map [string ]structFieldIndex , err error ) {
185
+ // The specified type is probed and a map is generated that maps the
186
+ // underlying model type (its 'primary' type) to the field number
187
+ // within the choice type struct. This data can then be used to correctly
188
+ // assign each data relationship node to the correct choice type
189
+ // struct field.
190
+ func choiceStructMapping (choice reflect.Type ) (result map [string ]structFieldIndex , err error ) {
191
191
result = make (map [string ]structFieldIndex )
192
192
193
- for join .Kind () != reflect .Struct {
194
- join = join .Elem ()
193
+ for choice .Kind () != reflect .Struct {
194
+ choice = choice .Elem ()
195
195
}
196
196
197
- for i := 0 ; i < join .NumField (); i ++ {
198
- fieldType := join .Field (i )
197
+ for i := 0 ; i < choice .NumField (); i ++ {
198
+ fieldType := choice .Field (i )
199
199
200
200
if fieldType .Type .Kind () != reflect .Ptr {
201
201
continue
@@ -234,22 +234,22 @@ func getStructTags(field reflect.StructField) ([]string, error) {
234
234
return args , nil
235
235
}
236
236
237
- // unmarshalNodeMaybeJoin populates a model that may or may not be
238
- // a join struct that corresponds to a polyrelation or relation
239
- func unmarshalNodeMaybeJoin (m * reflect.Value , data * Node , annotation string , joinMapping map [string ]structFieldIndex , included * map [string ]* Node ) error {
240
- // This will hold either the value of the join model or the actual
237
+ // unmarshalNodeMaybeChoice populates a model that may or may not be
238
+ // a choice type struct that corresponds to a polyrelation or relation
239
+ func unmarshalNodeMaybeChoice (m * reflect.Value , data * Node , annotation string , choiceTypeMapping map [string ]structFieldIndex , included * map [string ]* Node ) error {
240
+ // This will hold either the value of the choice type model or the actual
241
241
// model, depending on annotation
242
242
var actualModel = * m
243
- var joinElem * structFieldIndex = nil
243
+ var choiceElem * structFieldIndex = nil
244
244
245
245
if annotation == annotationPolyRelation {
246
- j , ok := joinMapping [data .Type ]
246
+ c , ok := choiceTypeMapping [data .Type ]
247
247
if ! ok {
248
248
// There is no valid join field to assign this type of relation.
249
249
return ErrBadJSONAPIJoinStruct
250
250
}
251
- joinElem = & j
252
- actualModel = reflect .New (joinElem .Type )
251
+ choiceElem = & c
252
+ actualModel = reflect .New (choiceElem .Type )
253
253
}
254
254
255
255
if err := unmarshalNode (
@@ -260,11 +260,12 @@ func unmarshalNodeMaybeJoin(m *reflect.Value, data *Node, annotation string, joi
260
260
return err
261
261
}
262
262
263
- if joinElem != nil {
263
+ if choiceElem != nil {
264
264
// actualModel is a pointer to the model type
265
- // m is a pointer to a struct that should hold the actualModel at joinElem.FieldNum
265
+ // m is a pointer to a struct that should hold the actualModel
266
+ // at choiceElem.FieldNum
266
267
v := m .Elem ()
267
- v .Field (joinElem .FieldNum ).Set (actualModel )
268
+ v .Field (choiceElem .FieldNum ).Set (actualModel )
268
269
}
269
270
return nil
270
271
}
@@ -384,10 +385,11 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
384
385
}
385
386
386
387
// If this is a polymorphic relation, each data relationship needs to be assigned
387
- // to it's appropriate join field and fieldValue should be a join field.
388
- var joinMapping map [string ]structFieldIndex = nil
388
+ // to it's appropriate choice field and fieldValue should be a choice
389
+ // struct type field.
390
+ var choiceMapping map [string ]structFieldIndex = nil
389
391
if annotation == annotationPolyRelation {
390
- joinMapping , err = joinStructMapping (fieldValue .Type ())
392
+ choiceMapping , err = choiceStructMapping (fieldValue .Type ())
391
393
if err != nil {
392
394
er = err
393
395
break
@@ -406,16 +408,16 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
406
408
407
409
data := relationship .Data
408
410
409
- // This will hold either the value of the slice of join models or
411
+ // This will hold either the value of the slice of choice type models or
410
412
// the slice of models, depending on the annotation
411
413
models := reflect .New (sliceType ).Elem ()
412
414
413
415
for _ , n := range data {
414
- // This will hold either the value of the join model or the actual
416
+ // This will hold either the value of the choice type model or the actual
415
417
// model, depending on annotation
416
418
m := reflect .New (sliceType .Elem ().Elem ())
417
419
418
- err = unmarshalNodeMaybeJoin (& m , n , annotation , joinMapping , included )
420
+ err = unmarshalNodeMaybeChoice (& m , n , annotation , choiceMapping , included )
419
421
if err != nil {
420
422
er = err
421
423
break
@@ -446,11 +448,11 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
446
448
continue
447
449
}
448
450
449
- // This will hold either the value of the join model or the actual
451
+ // This will hold either the value of the choice type model or the actual
450
452
// model, depending on annotation
451
453
m := reflect .New (fieldValue .Type ().Elem ())
452
454
453
- err = unmarshalNodeMaybeJoin (& m , relationship .Data , annotation , joinMapping , included )
455
+ err = unmarshalNodeMaybeChoice (& m , relationship .Data , annotation , choiceMapping , included )
454
456
if err != nil {
455
457
er = err
456
458
break
0 commit comments