support default value for form

This commit is contained in:
thinkerou 2017-10-24 17:44:09 +08:00
parent dfb68ce085
commit 13042dfd4e

View File

@ -8,6 +8,7 @@ import (
"errors" "errors"
"reflect" "reflect"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -23,6 +24,15 @@ func mapForm(ptr interface{}, form map[string][]string) error {
structFieldKind := structField.Kind() structFieldKind := structField.Kind()
inputFieldName := typeField.Tag.Get("form") inputFieldName := typeField.Tag.Get("form")
inputFieldNameList := strings.Split(inputFieldName, ",")
inputFieldName = inputFieldNameList[0]
var defaultValue interface{}
if len(inputFieldNameList) > 1 {
defaultList := strings.Split(inputFieldNameList[1], "=")
if len(defaultList) == 2 && defaultList[0] == "default" {
defaultValue = defaultList[1]
}
}
if inputFieldName == "" { if inputFieldName == "" {
inputFieldName = typeField.Name inputFieldName = typeField.Name
@ -38,8 +48,13 @@ func mapForm(ptr interface{}, form map[string][]string) error {
} }
} }
inputValue, exists := form[inputFieldName] inputValue, exists := form[inputFieldName]
if !exists { if !exists {
continue if defaultValue.(string) == "" {
continue
}
inputValue = make([]string, 1)
inputValue[0] = defaultValue.(string)
} }
numElems := len(inputValue) numElems := len(inputValue)