Skip to content

Commit b391a84

Browse files
committed
remove duplicated code
1 parent af21dba commit b391a84

File tree

1 file changed

+7
-40
lines changed

1 file changed

+7
-40
lines changed

request.go

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,6 @@ func unmarshalAttribute(attribute interface{}, args []string, fieldType reflect.
385385
return
386386
}
387387

388-
// Handle field of type struct
389-
if fieldValue.Type().Kind() == reflect.Struct {
390-
value, err = handleStruct(attribute, args, fieldType, fieldValue)
391-
return
392-
}
393-
394388
// Handle field containing slice of structs
395389
if fieldValue.Type().Kind() == reflect.Slice && reflect.TypeOf(fieldValue.Interface()).Elem().Kind() == reflect.Struct {
396390
value, err = handleStructSlice(attribute, args, fieldType, fieldValue)
@@ -562,44 +556,17 @@ func handlePointer(attribute interface{}, args []string, fieldType reflect.Type,
562556
func handleStruct(attribute interface{}, args []string, fieldType reflect.Type, fieldValue reflect.Value) (reflect.Value, error) {
563557
model := reflect.New(fieldValue.Type())
564558

565-
modelValue := model.Elem()
566-
modelType := model.Type().Elem()
567-
568559
var er error
569560

570-
for i := 0; i < modelValue.NumField(); i++ {
571-
fieldType := modelType.Field(i)
572-
tag := fieldType.Tag.Get("jsonapi")
573-
if tag == "" {
574-
continue
575-
}
576-
577-
fieldValue := modelValue.Field(i)
578-
579-
args := strings.Split(tag, ",")
580-
581-
if len(args) < 1 {
582-
er = ErrBadJSONAPIStructTag
583-
break
584-
}
585-
586-
if reflect.TypeOf(attribute).Kind() != reflect.Map {
587-
return model, nil
588-
}
589-
590-
attributes := reflect.ValueOf(attribute).Interface().(map[string]interface{})
591-
attribute := attributes[args[1]]
592-
593-
if attribute == nil {
594-
continue
595-
}
561+
data, er := json.Marshal(attribute)
562+
if er != nil {
563+
return model, er
564+
}
596565

597-
value, err := unmarshalAttribute(attribute, args, fieldType.Type, fieldValue)
598-
if err != nil {
599-
return model, nil
600-
}
566+
er = json.Unmarshal(data, model.Interface())
601567

602-
assign(fieldValue, value)
568+
if er != nil {
569+
return model, er
603570
}
604571

605572
return model, er

0 commit comments

Comments
 (0)