mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 20:22:20 +08:00
fix: empty value in unix and unixnano query param
This commit is contained in:
parent
e46bd52185
commit
abb667dc95
@ -397,6 +397,11 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
|
|||||||
timeFormat = time.RFC3339
|
timeFormat = time.RFC3339
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if val == "" {
|
||||||
|
value.Set(reflect.ValueOf(time.Time{}))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
switch tf := strings.ToLower(timeFormat); tf {
|
switch tf := strings.ToLower(timeFormat); tf {
|
||||||
case "unix", "unixnano":
|
case "unix", "unixnano":
|
||||||
tv, err := strconv.ParseInt(val, 10, 64)
|
tv, err := strconv.ParseInt(val, 10, 64)
|
||||||
@ -414,11 +419,6 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if val == "" {
|
|
||||||
value.Set(reflect.ValueOf(time.Time{}))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
l := time.Local
|
l := time.Local
|
||||||
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
|
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
|
||||||
l = time.UTC
|
l = time.UTC
|
||||||
|
@ -635,3 +635,25 @@ func TestMappingCustomArrayForm(t *testing.T) {
|
|||||||
expected, _ := convertTo(val)
|
expected, _ := convertTo(val)
|
||||||
assert.EqualValues(t, expected, s.FileData)
|
assert.EqualValues(t, expected, s.FileData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMappingTimeUnixAndUnixNano(t *testing.T) {
|
||||||
|
var s struct {
|
||||||
|
Unix time.Time `time_format:"unix"`
|
||||||
|
UnixNano time.Time `time_format:"unixnano"`
|
||||||
|
EmptyValueUnix time.Time `time_format:"unix"`
|
||||||
|
EmptyValueUnixNano time.Time `time_format:"unixnano"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := mapForm(&s, map[string][]string{
|
||||||
|
"Unix": {"1548000000"},
|
||||||
|
"UnixNano": {"1548000000000000000"},
|
||||||
|
"EmptyValue": {""},
|
||||||
|
"EmptyValueUnixNano": {""},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, "2019-01-20 16:00:00 +0000 UTC", s.Unix.UTC().String())
|
||||||
|
assert.Equal(t, "2019-01-20 16:00:00 +0000 UTC", s.UnixNano.UTC().String())
|
||||||
|
assert.Zero(t, s.EmptyValueUnix)
|
||||||
|
assert.Zero(t, s.EmptyValueUnixNano)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user