-
Notifications
You must be signed in to change notification settings - Fork 8.5k
fix(time): binding time with empty value #4103
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR prevents ParseInt errors by filling empty time values with zero time before parsing and adds tests covering the new behavior for UNIX timestamps.
- Added an early check in
setTimeFieldto handle empty strings by settingtime.Time{}. - Removed the redundant post-switch empty-value block.
- Extended
TestMappingTimewith two new struct fields and assertions forunixandunixnano.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| binding/form_mapping.go | Inserted an early empty-string check and removed the lower duplicate |
| binding/form_mapping_test.go | Added ZeroUnixTime/ZeroUnixNanoTime fields and assertions |
Comments suppressed due to low confidence (2)
binding/form_mapping_test.go:189
- Add a test case for an empty string with a non-
unixformat (e.g.,time_format:"2006-01-02") to ensure zero-value behavior is consistent across all formats.
ZeroUnixTime time.Time `time_format:"unix"`
binding/form_mapping.go:400
- [nitpick] Consider trimming whitespace (e.g. strings.TrimSpace) before checking for empty to avoid treating " " as a valid timestamp.
if val == "" {
| value.Set(reflect.ValueOf(time.Time{})) | ||
| return nil |
Copilot
AI
May 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The early return for empty values bypasses any time_location/time_utc tag processing; if users expect zero values in a specific time zone, consider applying tag-based location adjustments after setting zero time.
| value.Set(reflect.ValueOf(time.Time{})) | |
| return nil | |
| // Set to zero time after applying tag-based adjustments | |
| val = "0001-01-01T00:00:00Z" // Default zero time in RFC3339 format |
|
testing fail so revert #4245 |
Fixes: #4098
In
setTimeField, when the stringvalis empty and thetimeFormatisunixorunixnano, an error occurs due tostrconv.ParseInthandling empty values. We can handle this by filling it with a zero value before parsing.master