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
DefaultQuery of type int, float or bool #1898
Comments
I am very interested in your thoughts. func DefaulQueryVar(key string, var, defValue interface{}) error Var can be *int, *float, *bool, etc. var ss []string
c.DefaultQueryVar(key, &ss, []string{"1", "2", "3"})
var i int
c.DefaultQueryVar(key, &i, 3)
var f float64
c.DefaultQueryVar(key, &f, 3.14) |
```go package main import ( "fmt" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.GET("/", func(c *gin.Context) { var i int var err error err = c.DefaultQueryVar("int", &i, -1) fmt.Printf("%v, %v\n", i, err) var ss []string err = c.DefaultQueryVar("slice", &ss, []string{"5", "5", "5"}) fmt.Printf("%v, %v\n", ss, err) var b bool err = c.DefaultQueryVar("bool", &b, false) fmt.Printf("%v, %v\n", b, err) var f float64 err = c.DefaultQueryVar("f", &f, 3.14) fmt.Printf("%v, %v\n", f, err) }) r.Run() return } ```
That sounds good. Why do you want to do reflection and not creating |
|
This is a interesting idea, I'm not holding my breath for generics, and when or if it happens, so many other things will want to get upgraded this will seem very small. If it turns out this is going to be included with gin, I really dislike the idea if using reflection and a empty interface. As painful as it is to have to make the extra methods, that is what Go is currently about it's everywhere mainly because most people don't want to pay the cost of refection or losing type safety with the empty interface. |
The ShadowdBind, ShouldBindJSON, ShouldBindXML, ShouldBindQuery, ShouldBindYAML functions in gin are based on the reflect package. Although many people don't like reflect, the ShouldBind APIs are super easy to use. The idea of making DefaultQueryVar is the same as that of ShouldBindJSON, making an API that everyone likes. |
If need, I like the follow:
|
Those Methods build on top of Lower level Internal and external Libraries, None of them add reflection code into
It's not the same as Don't get me wrong I like this idea, we should do it for path |
@dmarkham The idea is very good, so I extend the C. Param method. |
Now it’s done. |
Why not use |
xxxVar function is only an enhanced version of c.Param and c.DefaultQuery |
@guonaihong hi, guonaihong var flag bool maybe the design exists error. |
Could be helpful to have func's like
DefaultQueryInt
for example.then we could write something like
Or what's the best way to use
c.DefaultQuery
for types like int, float, bool or time.is there a best practise to implement?
The text was updated successfully, but these errors were encountered: