-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Auto-detect time format in form binding #1037
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1037 +/- ##
=======================================
Coverage 96.66% 96.66%
=======================================
Files 16 16
Lines 1711 1711
=======================================
Hits 1654 1654
Misses 49 49
Partials 8 8 Continue to review full report at Codecov.
|
Please add testing code. |
There's no tests for form_mapping in general. Are they in a separate dir? |
Ok I added a test to binding_test.go. |
LGTM. @javierprovecho Please help to review. |
👍 for this change. Do you really need to add another lib for this? We can try try some common formats like this: var timeFormats = []string{
"02/01/2006",
"02/01/2006 15:04",
"02/01/2006 15:04:05",
// many other common formats
}
func tryParseTime(str string) (time.Time, error) {
for _, fmt := range timeFormats {
if t, err := time.ParseInLocation(fmt, str, time.Local); err == nil {
return t, nil
}
}
return time.Time{}, ErrTimeFormatNotReconized
} |
binding/form_mapping.go
Outdated
|
||
if val == "" { | ||
value.Set(reflect.ValueOf(time.Time{})) | ||
return nil | ||
} | ||
|
||
timeFormat := structField.Tag.Get("time_format") | ||
if timeFormat == "" { | ||
t, err := dateparse.ParseAny(val) |
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.
What location does ParseAny
assume?
See below that the current behavior is using local timezone by default, unless a utc
tag is informed in the field. I think it is desirable to keep this behavior.
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.
Quoting from the araddon/dateparse
module
The location your server is configured effects the results
There exists the dateparse.ParseIn(datestr string, loc *time.Location) (time.Time, error)
method. I can switch to this to retain the tag behaviour.
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.
About the separate lib the only benefit is that it uses a byte reader with state switch to determine the format to be more efficient.
Changed the behaviour to respect |
less 3rd party deps is better. I think user should be able to choose any datetime parsing lib he wants. |
I don't see a way to add user choosable date parser without resorting to treating date args as strings and parsing dates in the controller. The whole point of binding is to get over with parsing request parameters with just adding a few tags to a struct. As to the separate dependency issue, the lib is quite simple and has it's own tests and documentation. Adding a simple |
it's a feature, however i don't like the idea of loading chains of deps in gin, I've changed the milestone for the next version (not 1.4, already due), i'll give it a look later... |
By using
github.com/araddon/dateparse
inbinding/form_mapping.go
thetime_format
tag can be optional.