fix: empty value in unix and unixnano query param

This commit is contained in:
Fernando Lisboa Costa 2024-11-22 15:07:51 -03:00
parent e46bd52185
commit abb667dc95
2 changed files with 27 additions and 5 deletions

View File

@ -397,6 +397,11 @@ 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":
tv, err := strconv.ParseInt(val, 10, 64)
@ -414,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

View File

@ -635,3 +635,25 @@ func TestMappingCustomArrayForm(t *testing.T) {
expected, _ := convertTo(val)
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)
}