@@ -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,22 @@ 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
}
354
342
structNode := new (Node )
355
343
if err := json .Unmarshal (structData , & structNode .Attributes ); err != nil {
356
- return nil , err
344
+ return reflect. Value {} , err
357
345
}
346
+
358
347
structModel := reflect .New (fieldValue .Type ())
359
348
if err := unmarshalNode (structNode , structModel , nil ); err != nil {
360
- return nil , err
349
+ return reflect. Value {} , err
361
350
}
362
351
363
- return & structModel , nil
352
+ return structModel , nil
364
353
}
365
354
366
355
func fullNode (n * Node , included * map [string ]* Node ) * Node {
@@ -376,7 +365,7 @@ func fullNode(n *Node, included *map[string]*Node) *Node {
376
365
// assign will take the value specified and assign it to the field; if
377
366
// field is expecting a ptr assign will assign a ptr.
378
367
func assign (field , value reflect.Value ) {
379
- if field .Kind () == reflect .Ptr || field . Kind () == reflect . Struct {
368
+ if field .Kind () == reflect .Ptr {
380
369
field .Set (value )
381
370
} else {
382
371
field .Set (reflect .Indirect (value ))
@@ -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,12 +561,15 @@ 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
572
+
583
573
default :
584
574
return reflect.Value {}, newErrUnsupportedPtrType (
585
575
reflect .ValueOf (attribute ), fieldType , structField )
@@ -624,13 +614,13 @@ func handleStruct(
624
614
func handleStructSlice (
625
615
attribute interface {},
626
616
fieldValue reflect.Value ) (reflect.Value , error ) {
617
+
627
618
models := reflect .New (fieldValue .Type ()).Elem ()
628
619
dataMap := reflect .ValueOf (attribute ).Interface ().([]interface {})
629
620
for _ , data := range dataMap {
630
621
model := reflect .New (fieldValue .Type ().Elem ()).Elem ()
631
622
632
- value , err := handleStruct (data , model )
633
-
623
+ value , err := unmarshalFromAttribute (data , model )
634
624
if err != nil {
635
625
continue
636
626
}
0 commit comments