Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty value in unix and unixnano binding #4105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: empty value in unix and unixnano query param
  • Loading branch information
fernandollisboa committed Nov 22, 2024
commit abb667dc950c141a9828a198c27cd3b09cf169f4
10 changes: 5 additions & 5 deletions binding/form_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions binding/form_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}