mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-18 06:42:10 +08:00
fix bug that tag form:",default=1"
are not effective in some cases
This commit is contained in:
parent
235898e642
commit
57226d1bf9
@ -158,12 +158,8 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
return true, setArray(vs, value, field)
|
||||
default:
|
||||
var val string
|
||||
if !ok {
|
||||
val = opt.defaultValue
|
||||
}
|
||||
|
||||
if len(vs) > 0 {
|
||||
val := opt.defaultValue
|
||||
if len(vs) > 0 && vs[0] != "" {
|
||||
val = vs[0]
|
||||
}
|
||||
return true, setWithProperType(val, value, field)
|
||||
|
@ -62,14 +62,25 @@ func TestMappingBaseTypes(t *testing.T) {
|
||||
|
||||
func TestMappingDefault(t *testing.T) {
|
||||
var s struct {
|
||||
Int int `form:",default=9"`
|
||||
Slice []int `form:",default=9"`
|
||||
Array [1]int `form:",default=9"`
|
||||
Int int `form:",default=9"`
|
||||
PageNo int `form:"page_no,default=1"`
|
||||
PageSize int `form:"page_size,default=60"`
|
||||
String string `form:"string,default=9"`
|
||||
Slice []int `form:",default=9"`
|
||||
Array [1]int `form:",default=9"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{}, "form")
|
||||
form := map[string][]string{
|
||||
"page_no": {""},
|
||||
"page_size": {""},
|
||||
"string": {"test"},
|
||||
}
|
||||
err := mappingByPtr(&s, formSource(form), "form")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 9, s.Int)
|
||||
assert.Equal(t, 1, s.PageNo)
|
||||
assert.Equal(t, 60, s.PageSize)
|
||||
assert.Equal(t, "test", s.String)
|
||||
assert.Equal(t, []int{9}, s.Slice)
|
||||
assert.Equal(t, [1]int{9}, s.Array)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user