Repository: gin-gonic/gin
Pull request #1794 (2019-03-03 12:39:06)

add support time.Duration on mapping
```
var s struct {
    Timeout time.Duration `form:"timeout"`
}
req := formPostRequest("", "timeout=5s")
Form.Bind(req, &s)
```

codecov[bot]: # [Codecov](https://codecov.io/gh/gin-gonic/gin/pull/1794?src=pr&el=h1) Report
> Merging [#1794](https://codecov.io/gh/gin-gonic/gin/pull/1794?src=pr&el=desc) into [master](https://codecov.io/gh/gin-gonic/gin/commit/0d50ce859745354fa83dcf2bf4c972abed25e53b?src=pr&el=desc) will **decrease** coverage by `0.08%`.
> The diff coverage is `77.77%`.

[![Impacted file tree graph](https://codecov.io/gh/gin-gonic/gin/pull/1794/graphs/tree.svg?width=650&token=Qpqxhwery8&height=150&src=pr)](https://codecov.io/gh/gin-gonic/gin/pull/1794?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1794      +/-   ##
==========================================
- Coverage   98.62%   98.53%   -0.09%     
==========================================
  Files          41       41              
  Lines        2103     2112       +9     
==========================================
+ Hits         2074     2081       +7     
- Misses         18       19       +1     
- Partials       11       12       +1
```


| [Impacted Files](https://codecov.io/gh/gin-gonic/gin/pull/1794?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [binding/form\_mapping.go](https://codecov.io/gh/gin-gonic/gin/pull/1794/diff?src=pr&el=tree#diff-YmluZGluZy9mb3JtX21hcHBpbmcuZ28=) | `98.9% <77.77%> (-1.1%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/gin-gonic/gin/pull/1794?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/gin-gonic/gin/pull/1794?src=pr&el=footer). Last update [0d50ce8...68e9428](https://codecov.io/gh/gin-gonic/gin/pull/1794?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/binding/form_mapping.go b/binding/form_mapping.go
index 1109e4d..91fadcf 100644
--- a/binding/form_mapping.go
+++ b/binding/form_mapping.go
@@ -141,6 +141,10 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
 	case reflect.Int32:
 		return setIntField(val, 32, value)
 	case reflect.Int64:
+		switch value.Interface().(type) {
+		case time.Duration:
+			return setTimeDuration(val, value, field)
+		}
 		return setIntField(val, 64, value)
 	case reflect.Uint:
 		return setUintField(val, 0, value)
@@ -263,6 +267,15 @@ func setSlice(vals []string, value reflect.Value, field reflect.StructField) err
 	return nil
 }
 
+func setTimeDuration(val string, value reflect.Value, field reflect.StructField) error {
+	d, err := time.ParseDuration(val)
+	if err != nil {
+		return err
+	}
+	value.Set(reflect.ValueOf(d))
+	return nil
+}
+
 func head(str, sep string) (head string, tail string) {
 	idx := strings.Index(str, sep)
 	if idx < 0 {


Test patch:
diff --git a/binding/binding_test.go b/binding/binding_test.go
index 16ca202..5ae8795 100644
--- a/binding/binding_test.go
+++ b/binding/binding_test.go
@@ -1413,3 +1413,20 @@ func TestBindingUnknownTypeChan(t *testing.T) {
 	assert.Error(t, err)
 	assert.Equal(t, errUnknownType, err)
 }
+
+func TestBindingTimeDuration(t *testing.T) {
+	var s struct {
+		Timeout time.Duration `form:"timeout"`
+	}
+
+	// ok
+	req := formPostRequest("", "timeout=5s")
+	err := Form.Bind(req, &s)
+	assert.NoError(t, err)
+	assert.Equal(t, 5*time.Second, s.Timeout)
+
+	// error
+	req = formPostRequest("", "timeout=wrong")
+	err = Form.Bind(req, &s)
+	assert.Error(t, err)
+}