-
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
support default value for form #1138
Conversation
binding/form_mapping.go
Outdated
inputFieldName = inputFieldNameList[0] | ||
var defaultValue interface{} | ||
if len(inputFieldNameList) > 1 { | ||
defaultList := strings.Split(inputFieldNameList[1], "=") |
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.
maybe use strings.SplitN(inputFieldNameList[1], "=", 2)
and remove the condition below len(defaultList) == 2
. as it is right now, is also ok.
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.
Yes, we should use SplitN
which can reduce some codes(interface{}
and len(defaultList)==2
). Thx!
binding/form_mapping.go
Outdated
@@ -23,6 +24,15 @@ func mapForm(ptr interface{}, form map[string][]string) error { | |||
|
|||
structFieldKind := structField.Kind() | |||
inputFieldName := typeField.Tag.Get("form") | |||
inputFieldNameList := strings.Split(inputFieldName, ",") | |||
inputFieldName = inputFieldNameList[0] | |||
var defaultValue interface{} |
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.
any need to cast it as interface{}
? As I see below, you'll always use it as string and parse it to the specific type, right?
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.
Fixed, use string
, thanks!
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.
a test case would be nice for checking this behavior, rest of comments are just pr feedback/suggestions
👍
EDIT: also check the CI failure https://travis-ci.org/gin-gonic/gin/jobs/291989136#L555
Codecov Report
@@ Coverage Diff @@
## master #1138 +/- ##
==========================================
+ Coverage 98.41% 98.41% +<.01%
==========================================
Files 34 34
Lines 1762 1772 +10
==========================================
+ Hits 1734 1744 +10
Misses 23 23
Partials 5 5
Continue to review full report at Codecov.
|
@javierprovecho I will prefect the pull request include test and comment, thanks! |
@thinkerou Please fix the conflicts. |
@appleboy fixed. |
* support default value for form * fix bug for nil interface * use SplitN and optimization code * add test case * add test cases for form(own default value) * fix invalid code * fix code indent * assert order (cherry picked from commit 41f951e)
@@ -38,8 +48,13 @@ func mapForm(ptr interface{}, form map[string][]string) error { | |||
} | |||
} | |||
inputValue, exists := form[inputFieldName] | |||
|
|||
if !exists { |
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.
if inputValue is empty array(inputField exist and its ""), default value not take effect
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.
can you post one example? thanks!
ref #1052
TODOs:
TODO(add cases) have completed, but it need #1168 merged firstly.