Skip to content

Commit 0c97f0c

Browse files
committed
extract attribute handling into custom method for recursive usage later on
1 parent ab915cc commit 0c97f0c

File tree

1 file changed

+58
-72
lines changed

1 file changed

+58
-72
lines changed

request.go

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -271,66 +271,14 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
271271
fieldValue,
272272
}
273273

274-
_ = data
275-
276-
value := reflect.ValueOf(attribute)
277-
278-
// Handle field of type []string
279-
if fieldValue.Type() == reflect.TypeOf([]string{}) {
280-
values, err := handleStringSlice(data)
281-
if err != nil {
282-
er = err
283-
break
284-
}
285-
assign(fieldValue, values)
286-
continue
287-
}
288-
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 {
293-
er = err
294-
break
295-
}
296-
297-
assign(fieldValue, time)
298-
continue
299-
}
300-
301-
// JSON value was a float (numeric)
302-
if value.Kind() == reflect.Float64 {
303-
304-
numericValue, err := handleNumeric(data)
305-
306-
if err != nil {
307-
er = err
308-
break
309-
}
310-
311-
assign(fieldValue, numericValue)
312-
continue
313-
}
314-
315-
// Field was a Pointer type
316-
if fieldValue.Kind() == reflect.Ptr {
317-
318-
concreteVal, err := handlePointer(attribute, fieldValue.Type())
319-
320-
if err != nil {
321-
er = err
322-
break
323-
}
324-
325-
assign(fieldValue, concreteVal)
326-
continue
274+
value, err := unmarshalAttribute(data)
275+
if err != nil {
276+
er = err
277+
break
327278
}
328279

329-
// As a final catch-all, ensure types line up to avoid a runtime panic.
330-
if fieldValue.Kind() != value.Kind() {
331-
return ErrInvalidType
332-
}
333-
assign(fieldValue, reflect.ValueOf(attribute))
280+
assign(fieldValue, value)
281+
continue
334282

335283
} else if annotation == annotationRelation {
336284
isSlice := fieldValue.Type().Kind() == reflect.Slice
@@ -430,8 +378,45 @@ func assign(field, value reflect.Value) {
430378
}
431379
}
432380

433-
func handleStringSlice(m unmarshal) (reflect.Value, error) {
434-
v := reflect.ValueOf(m.attribute)
381+
func unmarshalAttribute(data unmarshal) (value reflect.Value, err error) {
382+
383+
value = reflect.ValueOf(data.attribute)
384+
385+
// Handle field of type []string
386+
if data.fieldValue.Type() == reflect.TypeOf([]string{}) {
387+
value, err = handleStringSlice(data)
388+
return
389+
}
390+
391+
// Handle field of type time.Time
392+
if data.fieldValue.Type() == reflect.TypeOf(time.Time{}) || data.fieldValue.Type() == reflect.TypeOf(new(time.Time)) {
393+
value, err = handleTime(data)
394+
return
395+
}
396+
397+
// JSON value was a float (numeric)
398+
if value.Kind() == reflect.Float64 {
399+
value, err = handleNumeric(data)
400+
return
401+
}
402+
403+
// Field was a Pointer type
404+
if data.fieldValue.Kind() == reflect.Ptr {
405+
value, err = handlePointer(data)
406+
return
407+
}
408+
409+
// As a final catch-all, ensure types line up to avoid a runtime panic.
410+
if data.fieldValue.Kind() != value.Kind() {
411+
err = ErrInvalidType
412+
return
413+
}
414+
415+
return
416+
}
417+
418+
func handleStringSlice(data unmarshal) (reflect.Value, error) {
419+
v := reflect.ValueOf(data.attribute)
435420
values := make([]string, v.Len())
436421
for i := 0; i < v.Len(); i++ {
437422
values[i] = v.Index(i).Interface().(string)
@@ -440,13 +425,13 @@ func handleStringSlice(m unmarshal) (reflect.Value, error) {
440425
return reflect.ValueOf(values), nil
441426
}
442427

443-
func handleTime(m unmarshal) (reflect.Value, error) {
428+
func handleTime(data unmarshal) (reflect.Value, error) {
444429

445430
var isIso8601 bool
446-
v := reflect.ValueOf(m.attribute)
431+
v := reflect.ValueOf(data.attribute)
447432

448-
if len(m.args) > 2 {
449-
for _, arg := range m.args[2:] {
433+
if len(data.args) > 2 {
434+
for _, arg := range data.args[2:] {
450435
if arg == annotationISO8601 {
451436
isIso8601 = true
452437
}
@@ -466,7 +451,7 @@ func handleTime(m unmarshal) (reflect.Value, error) {
466451
return reflect.ValueOf(time.Now()), ErrInvalidISO8601
467452
}
468453

469-
if m.fieldValue.Kind() == reflect.Ptr {
454+
if data.fieldValue.Kind() == reflect.Ptr {
470455
return reflect.ValueOf(&t), nil
471456
}
472457

@@ -488,15 +473,15 @@ func handleTime(m unmarshal) (reflect.Value, error) {
488473
return reflect.ValueOf(t), nil
489474
}
490475

491-
func handleNumeric(m unmarshal) (reflect.Value, error) {
492-
v := reflect.ValueOf(m.attribute)
476+
func handleNumeric(data unmarshal) (reflect.Value, error) {
477+
v := reflect.ValueOf(data.attribute)
493478
floatValue := v.Interface().(float64)
494479

495480
var kind reflect.Kind
496-
if m.fieldValue.Kind() == reflect.Ptr {
497-
kind = m.fieldType.Type.Elem().Kind()
481+
if data.fieldValue.Kind() == reflect.Ptr {
482+
kind = data.fieldType.Type.Elem().Kind()
498483
} else {
499-
kind = m.fieldType.Type.Kind()
484+
kind = data.fieldType.Type.Kind()
500485
}
501486

502487
var numericValue reflect.Value
@@ -545,10 +530,11 @@ func handleNumeric(m unmarshal) (reflect.Value, error) {
545530
return numericValue, nil
546531
}
547532

548-
func handlePointer(val interface{}, t reflect.Type) (reflect.Value, error) {
533+
func handlePointer(data unmarshal) (reflect.Value, error) {
534+
t := data.fieldValue.Type()
549535
var concreteVal reflect.Value
550536

551-
switch cVal := val.(type) {
537+
switch cVal := data.attribute.(type) {
552538
case string:
553539
concreteVal = reflect.ValueOf(&cVal)
554540
case bool:

0 commit comments

Comments
 (0)