Gin: DefaultQuery of type int, float or bool

Created on 13 May 2019  路  12Comments  路  Source: gin-gonic/gin

Could be helpful to have func's like DefaultQueryInt for example.

then we could write something like

limit, err := c.DefaultQueryInt("limit", 50)
if err != nil {
  // query is not an integer, we can response a statuscode 400 
}

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?

Most helpful comment

I am very interested in your thoughts.
I am planning to design

 func DefaulQueryVar(key string, var, defValue interface{}) error

Var can be *int, *float, *bool, etc.
usage

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)

All 12 comments

I am very interested in your thoughts.
I am planning to design

 func DefaulQueryVar(key string, var, defValue interface{}) error

Var can be *int, *float, *bool, etc.
usage

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)

That sounds good. Why do you want to do reflection and not creating DefaulQueryInt, DefaulQueryString functions instead of DefaulQueryVar? just a question to understand the design...

  • For the gin web framework, the fewer functions provided, the lower the learning cost.
    DefaultQueryVar , supports many types, such as int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, bool, float32, float64, etc.
  • Go 2.0 will provide generic functions in the near future. To provide a function such as DefaultQueryInt, DefaultQueryFloat32., the function prototype may be DefaultQuery(key string) (<T>, error)

This is a interesting idea,
I for one would rather have this as a stand alone package for gin vs being built in directly.

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.
Most Libraries do not make them all. int, int64, bool, float64 seems to be kinda normal and make people will convert as needed. time you normally have to roll your own mainly because everyone has a different idea how to format them, any package you use to parse them has to make some assumptions.

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:

type Q struct {
  A string `query: "a" binding:"required"`
  B int `query:"b" binding:"required"`
}

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.

Those Methods build on top of Lower level Internal and external Libraries, None of them add reflection code into gin and all of them have to handle many different data types in a single incoming payload.

DefaultQueryVar is simply a []string converter to a single type like []int64.

It's not the same as ShouldBindJSON, ShouldBindXML, ShouldBindQuery, ShouldBindYAML.

Don't get me wrong I like this idea, we should do it for path c.Param also.
I'm just a little skeptical I guess :)

@dmarkham The idea is very good, so I extend the C. Param method.

Now it鈥檚 done.

Why not use ShouldBindUri and ShouldBindQuery?

xxxVar function is only an enhanced version of c.Param and c.DefaultQuery
. c.ShouldBindUri, c.ShouldBindQuery is the best choice when dealing with >=2 parameters. If only one parameter is processed, c.DefaultQueryVar and c.ParamVar are also a good choice. You don't need to define a structure first. Handle different types of variables.

@guonaihong hi, guonaihong
through you design "func DefaulQueryVar(key string, var, defValue interface{}) error",
I found, if defValue is nil, the var type how to determine.
such as

var flag bool
DefaulQueryVar("flag", &flag, nil)

maybe the design exists error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lilee picture lilee  路  3Comments

xpbliss picture xpbliss  路  3Comments

CodingPapi picture CodingPapi  路  3Comments

mdnight picture mdnight  路  3Comments

kekemuyu picture kekemuyu  路  3Comments