Skip to content
Myr

add support time.Duration on mapping (#1794)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Dmitry Kutakov · committed by 田欧 · parent f8f1459 · Browse files · 2 files+30 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+30 added−0 removed

  • other:+30 added−0 removed

2 files+30 added−0 removed

modified:binding/binding_test.go+17 added
1411 unmodified lines
1412 err := Form.Bind(req, &s)1413 assert.Error(t, err)1414 assert.Equal(t, errUnknownType, err)1415}14161417func TestBindingTimeDuration(t *testing.T) {1418 var s struct {1419 Timeout time.Duration `form:"timeout"`1420 }14211422 // ok1423 req := formPostRequest("", "timeout=5s")1424 err := Form.Bind(req, &s)1425 assert.NoError(t, err)1426 assert.Equal(t, 5*time.Second, s.Timeout)14271428 // error1429 req = formPostRequest("", "timeout=wrong")1430 err = Form.Bind(req, &s)1431 assert.Error(t, err)1432}
modified:binding/form_mapping.go+13 added
139 unmodified lines
140 return setIntField(val, 16, value)141 case reflect.Int32:142 return setIntField(val, 32, value)143 case reflect.Int64:144 switch value.Interface().(type) {145 case time.Duration:146 return setTimeDuration(val, value, field)147 }148 return setIntField(val, 64, value)149 case reflect.Uint:150 return setUintField(val, 0, value)151 case reflect.Uint8:
114 unmodified lines
266 value.Set(slice)267 return nil268}269270func setTimeDuration(val string, value reflect.Value, field reflect.StructField) error {271 d, err := time.ParseDuration(val)272 if err != nil {273 return err274 }275 value.Set(reflect.ValueOf(d))276 return nil277}278279func head(str, sep string) (head string, tail string) {280 idx := strings.Index(str, sep)281 if idx < 0 {282 return str, ""
3 unmodified lines