@@ -117,6 +117,13 @@ func UnmarshalManyPayload(in io.Reader, t reflect.Type) ([]interface{}, error) {
117117 return models , nil
118118}
119119
120+ type unmarshal struct {
121+ attribute interface {}
122+ args []string
123+ fieldType reflect.StructField
124+ fieldValue reflect.Value
125+ }
126+
120127func unmarshalNode (data * Node , model reflect.Value , included * map [string ]* Node ) (err error ) {
121128
122129 defer func () {
@@ -245,71 +252,56 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
245252 fieldValue .Set (reflect .ValueOf (data .ClientID ))
246253 } else if annotation == annotationAttribute {
247254 attributes := data .Attributes
255+
248256 if attributes == nil || len (data .Attributes ) == 0 {
249257 continue
250258 }
251259
252- var isIso8601 bool
253-
254- if len (args ) > 2 {
255- for _ , arg := range args [2 :] {
256- if arg == annotationISO8601 {
257- isIso8601 = true
258- }
259- }
260- }
261-
262260 attribute := attributes [args [1 ]]
263261
264262 // continue if the attribute was not included in the request
265263 if attribute == nil {
266264 continue
267265 }
268266
267+ data := unmarshal {
268+ attribute ,
269+ args ,
270+ fieldType ,
271+ fieldValue ,
272+ }
273+
274+ _ = data
275+
269276 value := reflect .ValueOf (attribute )
270277
271278 // Handle field of type []string
272279 if fieldValue .Type () == reflect .TypeOf ([]string {}) {
273- values := handleStringSlice (value )
274- assign (fieldValue , reflect .ValueOf (values ))
275- continue
276- }
277-
278- // Handle field of type time.Time
279- if fieldValue .Type () == reflect .TypeOf (time.Time {}) {
280- var time time.Time
281- if time , err = handleTime (value , isIso8601 ); err != nil {
280+ values , err := handleStringSlice (data )
281+ if err != nil {
282282 er = err
283283 break
284284 }
285-
286- assign (fieldValue , reflect .ValueOf (time ))
285+ assign (fieldValue , values )
287286 continue
288287 }
289288
290- // Handle field of type * time.Time
291- if fieldValue .Type () == reflect .TypeOf (new (time.Time )) {
292- var time time. Time
293- if time , err = handleTime (value , isIso8601 ); err != nil {
289+ // Handle field of type time.Time
290+ if fieldValue .Type () == reflect .TypeOf (time. Time {}) || fieldValue . Type () == reflect . TypeOf ( new (time.Time )) {
291+ var time reflect. Value
292+ if time , err = handleTime (data ); err != nil {
294293 er = err
295294 break
296295 }
297296
298- assign (fieldValue , reflect . ValueOf ( & time ) )
297+ assign (fieldValue , time )
299298 continue
300299 }
301300
302301 // JSON value was a float (numeric)
303302 if value .Kind () == reflect .Float64 {
304303
305- var kind reflect.Kind
306- if fieldValue .Kind () == reflect .Ptr {
307- kind = fieldType .Type .Elem ().Kind ()
308- } else {
309- kind = fieldType .Type .Kind ()
310- }
311-
312- numericValue , err := handleNumeric (value , kind )
304+ numericValue , err := handleNumeric (data )
313305
314306 if err != nil {
315307 er = err
@@ -438,30 +430,47 @@ func assign(field, value reflect.Value) {
438430 }
439431}
440432
441- func handleStringSlice (v reflect.Value ) []string {
433+ func handleStringSlice (m unmarshal ) (reflect.Value , error ) {
434+ v := reflect .ValueOf (m .attribute )
442435 values := make ([]string , v .Len ())
443436 for i := 0 ; i < v .Len (); i ++ {
444437 values [i ] = v .Index (i ).Interface ().(string )
445438 }
446439
447- return values
440+ return reflect . ValueOf ( values ), nil
448441}
449442
450- func handleTime (v reflect.Value , isIso8601 bool ) (time.Time , error ) {
443+ func handleTime (m unmarshal ) (reflect.Value , error ) {
444+
445+ var isIso8601 bool
446+ v := reflect .ValueOf (m .attribute )
447+
448+ if len (m .args ) > 2 {
449+ for _ , arg := range m .args [2 :] {
450+ if arg == annotationISO8601 {
451+ isIso8601 = true
452+ }
453+ }
454+ }
455+
451456 if isIso8601 {
452457 var tm string
453458 if v .Kind () == reflect .String {
454459 tm = v .Interface ().(string )
455460 } else {
456- return time .Now (), ErrInvalidISO8601
461+ return reflect . ValueOf ( time .Now () ), ErrInvalidISO8601
457462 }
458463
459464 t , err := time .Parse (iso8601TimeFormat , tm )
460465 if err != nil {
461- return time .Now (), ErrInvalidISO8601
466+ return reflect .ValueOf (time .Now ()), ErrInvalidISO8601
467+ }
468+
469+ if m .fieldValue .Kind () == reflect .Ptr {
470+ return reflect .ValueOf (& t ), nil
462471 }
463472
464- return t , nil
473+ return reflect . ValueOf ( t ) , nil
465474 }
466475
467476 var at int64
@@ -471,18 +480,25 @@ func handleTime(v reflect.Value, isIso8601 bool) (time.Time, error) {
471480 } else if v .Kind () == reflect .Int {
472481 at = v .Int ()
473482 } else {
474- return time .Now (), ErrInvalidTime
483+ return reflect . ValueOf ( time .Now () ), ErrInvalidTime
475484 }
476485
477486 t := time .Unix (at , 0 )
478487
479- return t , nil
488+ return reflect . ValueOf ( t ) , nil
480489}
481490
482- func handleNumeric (v reflect. Value , kind reflect. Kind ) (reflect.Value , error ) {
483-
491+ func handleNumeric (m unmarshal ) (reflect.Value , error ) {
492+ v := reflect . ValueOf ( m . attribute )
484493 floatValue := v .Interface ().(float64 )
485494
495+ var kind reflect.Kind
496+ if m .fieldValue .Kind () == reflect .Ptr {
497+ kind = m .fieldType .Type .Elem ().Kind ()
498+ } else {
499+ kind = m .fieldType .Type .Kind ()
500+ }
501+
486502 var numericValue reflect.Value
487503
488504 switch kind {
0 commit comments