-
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
Compatible way to enhance context.Param(), supportted from GET POST COOKIE Route/Var #1627
base: master
Are you sure you want to change the base?
Conversation
…ute:key, _GET, _POST, _COOKIE.
Codecov Report
@@ Coverage Diff @@
## master #1627 +/- ##
==========================================
+ Coverage 99.28% 99.53% +0.25%
==========================================
Files 40 22 -18
Lines 1958 431 -1527
==========================================
- Hits 1944 429 -1515
+ Misses 10 1 -9
+ Partials 4 1 -3
Continue to review full report at Codecov.
|
…ute:key, _GET, _POST, _COOKIE.
you should have one utils package, like:
and like the follow use it:
I think framework should not have these functions, it should keep simple. do you think? |
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.
I don't think this is strictly needed as you can just have these conversions in your application, it doesn't belong to Gin, I guess?
@@ -289,6 +289,69 @@ func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string) | |||
return | |||
} | |||
|
|||
// Force Type convert | |||
func StringToInt(s string) (i int) { |
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.
This could be:
func StringToInt(s string) (i int) {
i, _ = strconv.Atoi(s)
return
}
or even better:
func StringToInt(s string) int {
i, _ := strconv.Atoi(s)
return i
}
as you shouldn't use named returns...
@@ -16,7 +16,7 @@ import ( | |||
"os" | |||
"strings" | |||
"time" | |||
|
|||
"strconv" |
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.
move internal package to top.
} | ||
} | ||
} | ||
return v |
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 v := c.Query(key); v != "" {
return v
}
if v := c. PostForm(key); v != "" {
return v
}
...
i support this. In gin framework , we can see these functions of *context like below // GetStringSlice returns the value associated with the key as a slice of strings.
func (c *Context) GetStringSlice(key string) (ss []string) {
//
}
// GetStringMap returns the value associated with the key as a map of interfaces.
func (c *Context) GetStringMap(key string) (sm map[string]interface{}) {
//
} these also can use utils function to convert, but gin support convenient functions to call so why not support a high call frequency function like ctx.ParamInt()? cc @thinkerou |
Compatible way to enhance context.Param(), support fetch data from Route:key, _GET, _POST, _COOKIE.
In some nodejs frameworks, such as express koa, we can easily use param to get parameters of different ways(GET POST COOKIE...). Considering Gin's design principles, I feel a little wasteful for param to only get parameters on the route. So we can get the parameters conveniently.
Before I got the int in POST, I thought it was not convenient to have a mandatory conversion or bind to display.
Before:
Now: