form mapping optimisation

This commit is contained in:
Manu Mtz-Almeida 2015-05-24 03:56:11 +02:00
parent 208e1f5569
commit 967e62337a
1 changed files with 13 additions and 10 deletions

View File

@ -20,18 +20,21 @@ func mapForm(ptr interface{}, form map[string][]string) error {
continue continue
} }
// support for embeded fields structFieldKind := structField.Kind()
if structField.Kind() == reflect.Struct {
err := mapForm(structField.Addr().Interface(), form)
if err != nil {
return err
}
continue
}
inputFieldName := typeField.Tag.Get("form") inputFieldName := typeField.Tag.Get("form")
if inputFieldName == "" { if inputFieldName == "" {
inputFieldName = typeField.Name inputFieldName = typeField.Name
// if "form" tag is nil, we inspect if the field is a struct.
// this would not make sense for JSON parsing but it does for a form
// since data is flatten
if structFieldKind == reflect.Struct {
err := mapForm(structField.Addr().Interface(), form)
if err != nil {
return err
}
continue
}
} }
inputValue, exists := form[inputFieldName] inputValue, exists := form[inputFieldName]
if !exists { if !exists {
@ -39,7 +42,7 @@ func mapForm(ptr interface{}, form map[string][]string) error {
} }
numElems := len(inputValue) numElems := len(inputValue)
if structField.Kind() == reflect.Slice && numElems > 0 { if structFieldKind == reflect.Slice && numElems > 0 {
sliceOf := structField.Type().Elem().Kind() sliceOf := structField.Type().Elem().Kind()
slice := reflect.MakeSlice(structField.Type(), numElems, numElems) slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
for i := 0; i < numElems; i++ { for i := 0; i < numElems; i++ {