@@ -248,26 +248,14 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
248
248
249
249
structField := fieldType
250
250
251
- if structField .Type .Kind () != reflect .Struct ||
252
- fieldValue .Type () == reflect .TypeOf (new (time.Time )) ||
253
- fieldValue .Type () == reflect .TypeOf (time.Time {}) {
254
- value , err := unmarshalAttribute (attribute , args , structField , fieldValue )
255
- if err != nil {
256
- er = err
257
- break
258
- }
259
- assign (fieldValue , value )
260
- continue
261
-
262
- } else {
263
- structModel , err := unmarshalFromAttribute (attribute , fieldValue )
264
- if err != nil {
265
- er = err
266
- break
267
- }
268
- fieldValue .Set ((* structModel ).Elem ())
269
- continue
251
+ value , err := unmarshalAttribute (attribute , args , structField , fieldValue )
252
+ if err != nil {
253
+ er = err
254
+ break
270
255
}
256
+
257
+ assign (fieldValue , value )
258
+ continue
271
259
} else if annotation == annotationRelation {
272
260
isSlice := fieldValue .Type ().Kind () == reflect .Slice
273
261
@@ -346,21 +334,23 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
346
334
return er
347
335
}
348
336
349
- func unmarshalFromAttribute (attribute interface {}, fieldValue reflect.Value ) (* reflect.Value , error ) {
337
+ func unmarshalFromAttribute (attribute interface {}, fieldValue reflect.Value ) (reflect.Value , error ) {
350
338
structData , err := json .Marshal (attribute )
351
339
if err != nil {
352
- return nil , err
340
+ return reflect. Value {} , err
353
341
}
342
+
354
343
structNode := new (Node )
355
344
if err := json .Unmarshal (structData , & structNode .Attributes ); err != nil {
356
- return nil , err
345
+ return reflect. Value {} , err
357
346
}
347
+
358
348
structModel := reflect .New (fieldValue .Type ())
359
349
if err := unmarshalNode (structNode , structModel , nil ); err != nil {
360
- return nil , err
350
+ return reflect. Value {} , err
361
351
}
362
352
363
- return & structModel , nil
353
+ return structModel , nil
364
354
}
365
355
366
356
func fullNode (n * Node , included * map [string ]* Node ) * Node {
@@ -376,7 +366,7 @@ func fullNode(n *Node, included *map[string]*Node) *Node {
376
366
// assign will take the value specified and assign it to the field; if
377
367
// field is expecting a ptr assign will assign a ptr.
378
368
func assign (field , value reflect.Value ) {
379
- if field .Kind () == reflect .Ptr || field . Kind () == reflect . Struct {
369
+ if field .Kind () == reflect .Ptr {
380
370
field .Set (value )
381
371
} else {
382
372
field .Set (reflect .Indirect (value ))
@@ -391,7 +381,6 @@ func unmarshalAttribute(
391
381
value = reflect .ValueOf (attribute )
392
382
fieldType := structField .Type
393
383
394
-
395
384
// Handle field of type []string
396
385
if fieldValue .Type () == reflect .TypeOf ([]string {}) {
397
386
value , err = handleStringSlice (attribute )
@@ -402,12 +391,13 @@ func unmarshalAttribute(
402
391
if fieldValue .Type () == reflect .TypeOf (time.Time {}) ||
403
392
fieldValue .Type () == reflect .TypeOf (new (time.Time )) {
404
393
value , err = handleTime (attribute , args , fieldValue )
394
+
405
395
return
406
396
}
407
397
408
398
// Handle field of type struct
409
- if fieldValue .Type (). Kind () == reflect .Struct {
410
- value , err = handleStruct (attribute , fieldValue )
399
+ if fieldValue .Kind () == reflect .Struct {
400
+ value , err = unmarshalFromAttribute (attribute , fieldValue )
411
401
return
412
402
}
413
403
@@ -426,7 +416,7 @@ func unmarshalAttribute(
426
416
427
417
// Field was a Pointer type
428
418
if fieldValue .Kind () == reflect .Ptr {
429
- value , err = handlePointer (attribute , args , fieldType , fieldValue , structField )
419
+ value , err = handlePointer (attribute , fieldType , fieldValue , structField )
430
420
return
431
421
}
432
422
@@ -482,7 +472,6 @@ func handleTime(attribute interface{}, args []string, fieldValue reflect.Value)
482
472
}
483
473
484
474
var at int64
485
-
486
475
if v .Kind () == reflect .Float64 {
487
476
at = int64 (v .Interface ().(float64 ))
488
477
} else if v .Kind () == reflect .Int {
@@ -492,7 +481,6 @@ func handleTime(attribute interface{}, args []string, fieldValue reflect.Value)
492
481
}
493
482
494
483
t := time .Unix (at , 0 )
495
-
496
484
return reflect .ValueOf (t ), nil
497
485
}
498
486
@@ -558,7 +546,6 @@ func handleNumeric(
558
546
559
547
func handlePointer (
560
548
attribute interface {},
561
- args []string ,
562
549
fieldType reflect.Type ,
563
550
fieldValue reflect.Value ,
564
551
structField reflect.StructField ) (reflect.Value , error ) {
@@ -574,11 +561,13 @@ func handlePointer(
574
561
concreteVal = reflect .ValueOf (& cVal )
575
562
case map [string ]interface {}:
576
563
var err error
577
- concreteVal , err = handleStruct (attribute , fieldValue )
564
+ fieldValueType := reflect .New (fieldValue .Type ().Elem ()).Elem ()
565
+ concreteVal , err = unmarshalFromAttribute (attribute , fieldValueType )
578
566
if err != nil {
579
567
return reflect.Value {}, newErrUnsupportedPtrType (
580
568
reflect .ValueOf (attribute ), fieldType , structField )
581
569
}
570
+
582
571
return concreteVal , err
583
572
default :
584
573
return reflect.Value {}, newErrUnsupportedPtrType (
@@ -624,13 +613,13 @@ func handleStruct(
624
613
func handleStructSlice (
625
614
attribute interface {},
626
615
fieldValue reflect.Value ) (reflect.Value , error ) {
616
+
627
617
models := reflect .New (fieldValue .Type ()).Elem ()
628
618
dataMap := reflect .ValueOf (attribute ).Interface ().([]interface {})
629
619
for _ , data := range dataMap {
630
620
model := reflect .New (fieldValue .Type ().Elem ()).Elem ()
631
621
632
- value , err := handleStruct (data , model )
633
-
622
+ value , err := unmarshalFromAttribute (data , model )
634
623
if err != nil {
635
624
continue
636
625
}
0 commit comments