modify code

This commit is contained in:
guonaihong 2019-12-11 19:05:54 +08:00
parent 7e9f808e02
commit 1e858d5bd7
3 changed files with 43 additions and 28 deletions

View File

@ -1271,30 +1271,3 @@ func requestWithBody(method, path, body string) (req *http.Request) {
req, _ = http.NewRequest(method, path, bytes.NewBufferString(body)) req, _ = http.NewRequest(method, path, bytes.NewBufferString(body))
return 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)
}
}

View File

@ -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 { func setWithProperType(val string, value reflect.Value, field reflect.StructField) error {
if value.Kind() != reflect.String {
val = strings.TrimSpace(val)
}
switch value.Kind() { switch value.Kind() {
case reflect.Int: case reflect.Int:
return setIntField(val, 0, value) return setIntField(val, 0, value)

View File

@ -190,7 +190,34 @@ func TestMappingTime(t *testing.T) {
assert.Error(t, err) 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 { var s struct {
D time.Duration D time.Duration
} }
@ -200,6 +227,17 @@ func TestMapiingTimeDuration(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, 5*time.Second, s.D) 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 // error
err = mappingByPtr(&s, formSource{"D": {"wrong"}}, "form") err = mappingByPtr(&s, formSource{"D": {"wrong"}}, "form")
assert.Error(t, err) assert.Error(t, err)