support struct pointer (#1342)

* support struct pointer

* add readme
This commit is contained in:
田欧
2018-05-01 14:24:18 +08:00
committed by Bo-Yi Wu
parent 2282be059b
commit bd4f73af67
3 changed files with 151 additions and 1 deletions

View File

@ -36,9 +36,16 @@ func mapForm(ptr interface{}, form map[string][]string) error {
if inputFieldName == "" {
inputFieldName = typeField.Name
// if "form" tag is nil, we inspect if the field is a struct.
// if "form" tag is nil, we inspect if the field is a struct or struct pointer.
// this would not make sense for JSON parsing but it does for a form
// since data is flatten
if structFieldKind == reflect.Ptr {
if !structField.Elem().IsValid() {
structField.Set(reflect.New(structField.Type().Elem()))
}
structField = structField.Elem()
structFieldKind = structField.Kind()
}
if structFieldKind == reflect.Struct {
err := mapForm(structField.Addr().Interface(), form)
if err != nil {