Skip to content
Myr

binding: support unix time (#1980)

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

* binding: support unix time ref:#1979 * binding: support unix time add test file modify readme ```golang package main import ( "fmt" "github.com/gin-gonic/gin" "time" ) type shareTime struct { CreateTime time.Time `form:"createTime" time_format:"unixNano"` UnixTime time.Time `form:"unixTime" time_format:"unix"` } func main() { r := gin.Default() unix := r.Group("/unix") testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local) fmt.Printf("%d\n", testCT.UnixNano()) testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local) fmt.Printf("%d\n", testUT.Unix()) unix.GET("/nano", func(c *gin.Context) { s := shareTime{} c.ShouldBindQuery(&s) if !testCT.Equal(s.CreateTime) { c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime) return } c.JSON(200, s) }) unix.GET("/sec", func(c *gin.Context) { s := shareTime{} c.ShouldBindQuery(&s) if !testUT.Equal(s.UnixTime) { c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime) return } c.JSON(200, s) }) r.Run() } ``` * Contraction variable scope

authored by guonaihong · committed by thinkerou · parent 0349de5 · Browse files · 3 files+68 added−13 removed
This commit

State

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

Diff stats

3 files+68 added−13 removed

  • other:+68 added−13 removed

3 files+68 added−13 removed

modified:binding/binding_test.go+37 added-4 removed
63 unmodified lines
64 Foo interface{} `json:"foo" binding:"required"`65}6667type FooBarStructForTimeType struct {68 TimeFoo time.Time `form:"time_foo" time_format:"2006-01-02" time_utc:"1" time_location:"Asia/Chongqing"`69 TimeBar time.Time `form:"time_bar" time_format:"2006-01-02" time_utc:"1"`68 TimeFoo time.Time `form:"time_foo" time_format:"2006-01-02" time_utc:"1" time_location:"Asia/Chongqing"`69 TimeBar time.Time `form:"time_bar" time_format:"2006-01-02" time_utc:"1"`70 CreateTime time.Time `form:"createTime" time_format:"unixNano"`71 UnixTime time.Time `form:"unixTime" time_format:"unix"`72}7374type FooStructForTimeTypeNotUnixFormat struct {75 CreateTime time.Time `form:"createTime" time_format:"unixNano"`76 UnixTime time.Time `form:"unixTime" time_format:"unix"`77}7879type FooStructForTimeTypeNotFormat struct {80 TimeFoo time.Time `form:"time_foo"`81}82
149 unmodified lines
232233func TestBindingFormForTime(t *testing.T) {234 testFormBindingForTime(t, "POST",235 "/", "/",229 "time_foo=2017-11-15&time_bar=", "bar2=foo")236 "time_foo=2017-11-15&time_bar=&createTime=1562400033000000123&unixTime=1562400033", "bar2=foo")237 testFormBindingForTimeNotUnixFormat(t, "POST",238 "/", "/",239 "time_foo=2017-11-15&createTime=bad&unixTime=bad", "bar2=foo")240 testFormBindingForTimeNotFormat(t, "POST",241 "/", "/",242 "time_foo=2017-11-15", "bar2=foo")243 testFormBindingForTimeFailFormat(t, "POST",
5 unmodified lines
249}250251func TestBindingFormForTime2(t *testing.T) {252 testFormBindingForTime(t, "GET",243 "/?time_foo=2017-11-15&time_bar=", "/?bar2=foo",253 "/?time_foo=2017-11-15&time_bar=&createTime=1562400033000000123&unixTime=1562400033", "/?bar2=foo",254 "", "")255 testFormBindingForTimeNotUnixFormat(t, "POST",256 "/", "/",257 "time_foo=2017-11-15&createTime=bad&unixTime=bad", "bar2=foo")258 testFormBindingForTimeNotFormat(t, "GET",259 "/?time_foo=2017-11-15", "/?bar2=foo",260 "", "")261 testFormBindingForTimeFailFormat(t, "GET",
599 unmodified lines
861 assert.Equal(t, int64(1510675200), obj.TimeFoo.Unix())862 assert.Equal(t, "Asia/Chongqing", obj.TimeFoo.Location().String())863 assert.Equal(t, int64(-62135596800), obj.TimeBar.Unix())864 assert.Equal(t, "UTC", obj.TimeBar.Location().String())865 assert.Equal(t, int64(1562400033000000123), obj.CreateTime.UnixNano())866 assert.Equal(t, int64(1562400033), obj.UnixTime.Unix())867868 obj = FooBarStructForTimeType{}869 req = requestWithBody(method, badPath, badBody)870 err = JSON.Bind(req, &obj)871 assert.Error(t, err)872}873874func testFormBindingForTimeNotUnixFormat(t *testing.T, method, path, badPath, body, badBody string) {875 b := Form876 assert.Equal(t, "form", b.Name())877878 obj := FooStructForTimeTypeNotUnixFormat{}879 req := requestWithBody(method, path, body)880 if method == "POST" {881 req.Header.Add("Content-Type", MIMEPOSTForm)882 }883 err := b.Bind(req, &obj)884 assert.Error(t, err)885886 obj = FooStructForTimeTypeNotUnixFormat{}887 req = requestWithBody(method, badPath, badBody)888 err = JSON.Bind(req, &obj)889 assert.Error(t, err)890}891892func testFormBindingForTimeNotFormat(t *testing.T, method, path, badPath, body, badBody string) {893 b := Form894 assert.Equal(t, "form", b.Name())895
346 unmodified lines
modified:binding/form_mapping.go+18 added
264 unmodified lines
265 if timeFormat == "" {266 timeFormat = time.RFC3339267 }268269 switch tf := strings.ToLower(timeFormat); tf {270 case "unix", "unixnano":271 tv, err := strconv.ParseInt(val, 10, 0)272 if err != nil {273 return err274 }275276 d := time.Duration(1)277 if tf == "unixnano" {278 d = time.Second279 }280281 t := time.Unix(tv/int64(d), tv%int64(d))282 value.Set(reflect.ValueOf(t))283 return nil284285 }286287 if val == "" {288 value.Set(reflect.ValueOf(time.Time{}))289 return nil290 }
59 unmodified lines
modified:README.md+13 added-9 removed
844 unmodified lines
845 "github.com/gin-gonic/gin"846)847848type Person struct {849 Name string `form:"name"`850 Address string `form:"address"`851 Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`849 Name string `form:"name"`850 Address string `form:"address"`851 Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`852 CreateTime time.Time `form:"createTime" time_format:"unixNano"`853 UnixTime time.Time `form:"unixTime" time_format:"unix"`854}855856func main() {857 route := gin.Default()
5 unmodified lines
863 var person Person864 // If `GET`, only `Form` binding engine (`query`) used.865 // If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`).866 // See more at https://github.com/gin-gonic/gin/blob/master/binding/binding.go#L48865 if c.ShouldBind(&person) == nil {866 log.Println(person.Name)867 log.Println(person.Address)868 log.Println(person.Birthday)869 }867 if c.ShouldBind(&person) == nil {868 log.Println(person.Name)869 log.Println(person.Address)870 log.Println(person.Birthday)871 log.Println(person.CreateTime)872 log.Println(person.UnixTime)873 }874875 c.String(200, "Success")876}877```878879Test it with:880```sh877$ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz&birthday=1992-03-15"881$ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz&birthday=1992-03-15&createTime=1562400033000000123&unixTime=1562400033"882```883884### Bind Uri885
1236 unmodified lines