@@ -247,15 +247,27 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
247
247
}
248
248
249
249
structField := fieldType
250
- value , err := unmarshalAttribute (attribute , args , structField , fieldValue )
251
- if err != nil {
252
- er = err
253
- break
254
- }
255
250
256
- assign (fieldValue , value )
257
- continue
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
258
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
270
+ }
259
271
} else if annotation == annotationRelation {
260
272
isSlice := fieldValue .Type ().Kind () == reflect .Slice
261
273
@@ -334,6 +346,23 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
334
346
return er
335
347
}
336
348
349
+ func unmarshalFromAttribute (attribute interface {}, fieldValue reflect.Value ) (* reflect.Value , error ) {
350
+ structData , err := json .Marshal (attribute )
351
+ if err != nil {
352
+ return nil , err
353
+ }
354
+ structNode := new (Node )
355
+ if err := json .Unmarshal (structData , & structNode .Attributes ); err != nil {
356
+ return nil , err
357
+ }
358
+ structModel := reflect .New (fieldValue .Type ())
359
+ if err := unmarshalNode (structNode , structModel , nil ); err != nil {
360
+ return nil , err
361
+ }
362
+
363
+ return & structModel , nil
364
+ }
365
+
337
366
func fullNode (n * Node , included * map [string ]* Node ) * Node {
338
367
includedKey := fmt .Sprintf ("%s,%s" , n .Type , n .ID )
339
368
@@ -347,7 +376,7 @@ func fullNode(n *Node, included *map[string]*Node) *Node {
347
376
// assign will take the value specified and assign it to the field; if
348
377
// field is expecting a ptr assign will assign a ptr.
349
378
func assign (field , value reflect.Value ) {
350
- if field .Kind () == reflect .Ptr {
379
+ if field .Kind () == reflect .Ptr || field . Kind () == reflect . Struct {
351
380
field .Set (value )
352
381
} else {
353
382
field .Set (reflect .Indirect (value ))
@@ -362,6 +391,7 @@ func unmarshalAttribute(
362
391
value = reflect .ValueOf (attribute )
363
392
fieldType := structField .Type
364
393
394
+
365
395
// Handle field of type []string
366
396
if fieldValue .Type () == reflect .TypeOf ([]string {}) {
367
397
value , err = handleStringSlice (attribute )
@@ -566,7 +596,6 @@ func handlePointer(
566
596
func handleStruct (
567
597
attribute interface {},
568
598
fieldValue reflect.Value ) (reflect.Value , error ) {
569
-
570
599
data , err := json .Marshal (attribute )
571
600
if err != nil {
572
601
return reflect.Value {}, err
0 commit comments