diff --git a/binding/binding_test.go b/binding/binding_test.go index a789412c..f0b6f795 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -1271,30 +1271,3 @@ func requestWithBody(method, path, body string) (req *http.Request) { req, _ = http.NewRequest(method, path, bytes.NewBufferString(body)) return } - -type bindTestData struct { - need interface{} - got interface{} - in map[string][]string -} - -func Test_Binding_BaseType(t *testing.T) { - type needFixDurationEmpty struct { - Duration time.Duration `form:"duration"` - } - - type needFixUnixNanoEmpty struct { - CreateTime time.Time `form:"createTime" time_format:"unixNano"` - } - - tests := []bindTestData{ - {need: &needFixDurationEmpty{}, got: &needFixDurationEmpty{}, in: http.Header{"duration": []string{}}}, - {need: &needFixUnixNanoEmpty{}, got: &needFixUnixNanoEmpty{}, in: http.Header{"createTime": []string{}}}, - } - - for _, v := range tests { - err := mapForm(v.got, v.in) - assert.NoError(t, err) - assert.Equal(t, v.need, v.got) - } -} diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 972a56e8..dcd305e7 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -170,6 +170,10 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][ } func setWithProperType(val string, value reflect.Value, field reflect.StructField) error { + if value.Kind() != reflect.String { + val = strings.TrimSpace(val) + } + switch value.Kind() { case reflect.Int: return setIntField(val, 0, value) diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index 2a560371..b3efe7b3 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -190,7 +190,34 @@ func TestMappingTime(t *testing.T) { assert.Error(t, err) } -func TestMapiingTimeDuration(t *testing.T) { +type bindTestData struct { + need interface{} + got interface{} + in map[string][]string +} + +func TestMappingTimeUnixNano(t *testing.T) { + type needFixUnixNanoEmpty struct { + CreateTime time.Time `form:"createTime" time_format:"unixNano"` + } + + // ok + tests := []bindTestData{ + {need: &needFixUnixNanoEmpty{}, got: &needFixUnixNanoEmpty{}, in: formSource{"createTime": []string{" "}}}, + {need: &needFixUnixNanoEmpty{}, got: &needFixUnixNanoEmpty{}, in: formSource{"createTime": []string{}}}, + } + + for _, v := range tests { + err := mapForm(v.got, v.in) + assert.NoError(t, err) + assert.Equal(t, v.need, v.got) + } +} + +func TestMappingTimeDuration(t *testing.T) { + type needFixDurationEmpty struct { + Duration time.Duration `form:"duration"` + } var s struct { D time.Duration } @@ -200,6 +227,17 @@ func TestMapiingTimeDuration(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 5*time.Second, s.D) + // ok + tests := []bindTestData{ + {need: &needFixDurationEmpty{}, got: &needFixDurationEmpty{}, in: formSource{"duration": []string{" "}}}, + {need: &needFixDurationEmpty{}, got: &needFixDurationEmpty{}, in: formSource{"duration": []string{}}}, + } + + for _, v := range tests { + err := mapForm(v.got, v.in) + assert.NoError(t, err) + assert.Equal(t, v.need, v.got) + } // error err = mappingByPtr(&s, formSource{"D": {"wrong"}}, "form") assert.Error(t, err)