From 967e62337a0c63e445c0efe4e36aa423f3983e4f Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sun, 24 May 2015 03:56:11 +0200 Subject: [PATCH] form mapping optimisation --- binding/form_mapping.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 02371b88..d8b13b1e 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -20,18 +20,21 @@ func mapForm(ptr interface{}, form map[string][]string) error { continue } - // support for embeded fields - if structField.Kind() == reflect.Struct { - err := mapForm(structField.Addr().Interface(), form) - if err != nil { - return err - } - continue - } - + structFieldKind := structField.Kind() inputFieldName := typeField.Tag.Get("form") if inputFieldName == "" { 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] if !exists { @@ -39,7 +42,7 @@ func mapForm(ptr interface{}, form map[string][]string) error { } numElems := len(inputValue) - if structField.Kind() == reflect.Slice && numElems > 0 { + if structFieldKind == reflect.Slice && numElems > 0 { sliceOf := structField.Type().Elem().Kind() slice := reflect.MakeSlice(structField.Type(), numElems, numElems) for i := 0; i < numElems; i++ {