From 67a65f1a5a5f62ed9c6f4a712f6a689686340cf2 Mon Sep 17 00:00:00 2001 From: ksw2000 <13825170+ksw2000@users.noreply.github.com> Date: Thu, 21 Nov 2024 23:34:41 +0800 Subject: [PATCH 1/3] fix: binding time with empty value (#4098) --- binding/form_mapping.go | 4 ++++ binding/form_mapping_test.go | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index f5f6f3ae93..db2045f400 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -399,6 +399,10 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val switch tf := strings.ToLower(timeFormat); tf { case "unix", "unixnano": + if val == "" { + val = "0" + } + tv, err := strconv.ParseInt(val, 10, 64) if err != nil { return err diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index 1277fd5ffd..8e99a136bf 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -183,11 +183,12 @@ func TestMapFormWithTag(t *testing.T) { func TestMappingTime(t *testing.T) { var s struct { - Time time.Time - LocalTime time.Time `time_format:"2006-01-02"` - ZeroValue time.Time - CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"` - UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"` + Time time.Time + LocalTime time.Time `time_format:"2006-01-02"` + ZeroValue time.Time + ZeroUnixTime time.Time `time_format:"unix"` // see issue #4098 + CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"` + UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"` } var err error @@ -195,11 +196,12 @@ func TestMappingTime(t *testing.T) { require.NoError(t, err) err = mapForm(&s, map[string][]string{ - "Time": {"2019-01-20T16:02:58Z"}, - "LocalTime": {"2019-01-20"}, - "ZeroValue": {}, - "CSTTime": {"2019-01-20"}, - "UTCTime": {"2019-01-20"}, + "Time": {"2019-01-20T16:02:58Z"}, + "LocalTime": {"2019-01-20"}, + "ZeroValue": {}, + "ZeroUnixTime": {}, + "CSTTime": {"2019-01-20"}, + "UTCTime": {"2019-01-20"}, }) require.NoError(t, err) @@ -207,6 +209,7 @@ func TestMappingTime(t *testing.T) { assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String()) assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String()) assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String()) + assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixTime.UTC().String()) assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String()) assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String()) assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String()) From 025db2a7c92e90c62c325086b6067a4c8df959c5 Mon Sep 17 00:00:00 2001 From: Kashiwa <13825170+ksw2000@users.noreply.github.com> Date: Sat, 30 Nov 2024 06:36:38 +0000 Subject: [PATCH 2/3] refact: simplify null-to-zero filling logic --- binding/form_mapping.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index db2045f400..f7e41e2483 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -397,12 +397,13 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val timeFormat = time.RFC3339 } + if val == "" { + value.Set(reflect.ValueOf(time.Time{})) + return nil + } + switch tf := strings.ToLower(timeFormat); tf { case "unix", "unixnano": - if val == "" { - val = "0" - } - tv, err := strconv.ParseInt(val, 10, 64) if err != nil { return err @@ -418,11 +419,6 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val return nil } - if val == "" { - value.Set(reflect.ValueOf(time.Time{})) - return nil - } - l := time.Local if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC { l = time.UTC From f4f5c444022eb8990f8a9879af183cc5200bb508 Mon Sep 17 00:00:00 2001 From: Kashiwa <13825170+ksw2000@users.noreply.github.com> Date: Sat, 30 Nov 2024 06:39:22 +0000 Subject: [PATCH 3/3] test: add test for zeroUnixNanoTime --- binding/form_mapping_test.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index 8e99a136bf..45cd9297ec 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -183,12 +183,13 @@ func TestMapFormWithTag(t *testing.T) { func TestMappingTime(t *testing.T) { var s struct { - Time time.Time - LocalTime time.Time `time_format:"2006-01-02"` - ZeroValue time.Time - ZeroUnixTime time.Time `time_format:"unix"` // see issue #4098 - CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"` - UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"` + Time time.Time + LocalTime time.Time `time_format:"2006-01-02"` + ZeroValue time.Time + ZeroUnixTime time.Time `time_format:"unix"` + ZeroUnixNanoTime time.Time `time_format:"unixnano"` + CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"` + UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"` } var err error @@ -196,12 +197,13 @@ func TestMappingTime(t *testing.T) { require.NoError(t, err) err = mapForm(&s, map[string][]string{ - "Time": {"2019-01-20T16:02:58Z"}, - "LocalTime": {"2019-01-20"}, - "ZeroValue": {}, - "ZeroUnixTime": {}, - "CSTTime": {"2019-01-20"}, - "UTCTime": {"2019-01-20"}, + "Time": {"2019-01-20T16:02:58Z"}, + "LocalTime": {"2019-01-20"}, + "ZeroValue": {}, + "ZeroUnixTime": {}, + "ZeroUnixNanoTime": {}, + "CSTTime": {"2019-01-20"}, + "UTCTime": {"2019-01-20"}, }) require.NoError(t, err) @@ -210,6 +212,7 @@ func TestMappingTime(t *testing.T) { assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String()) assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String()) assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixTime.UTC().String()) + assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixNanoTime.UTC().String()) assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String()) assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String()) assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String())