mirror of https://github.com/gofiber/fiber.git
binds the param string to a struct use params tag (#1968)
* add: params parse * fix: binds the param string to a struct use params tagpull/1974/head
parent
dfa24a0958
commit
bad7001570
4
ctx.go
4
ctx.go
|
@ -39,7 +39,7 @@ const (
|
|||
queryTag = "query"
|
||||
reqHeaderTag = "reqHeader"
|
||||
bodyTag = "form"
|
||||
uriTag = "uri"
|
||||
paramsTag = "params"
|
||||
)
|
||||
|
||||
// userContextKey define the key name for storing context.Context in *fasthttp.RequestCtx
|
||||
|
@ -861,7 +861,7 @@ func (c *Ctx) ParamsParser(out interface{}) error {
|
|||
for _, param := range c.route.Params {
|
||||
params[param] = append(params[param], c.Params(param))
|
||||
}
|
||||
return c.parseToStruct(uriTag, out, params)
|
||||
return c.parseToStruct(paramsTag, out, params)
|
||||
}
|
||||
|
||||
// ParamsInt is used to get an integer from the route parameters
|
||||
|
|
12
ctx_test.go
12
ctx_test.go
|
@ -431,8 +431,8 @@ func Test_Ctx_ParamParser(t *testing.T) {
|
|||
app := New()
|
||||
app.Get("/test1/userId/role/:roleId", func(ctx *Ctx) error {
|
||||
type Demo struct {
|
||||
UserID uint `uri:"userId"`
|
||||
RoleID uint `uri:"roleId"`
|
||||
UserID uint `params:"userId"`
|
||||
RoleID uint `params:"roleId"`
|
||||
}
|
||||
var (
|
||||
d = new(Demo)
|
||||
|
@ -1470,10 +1470,10 @@ func Benchmark_Ctx_ParamsParse(b *testing.B) {
|
|||
"john", "doe", "is", "awesome",
|
||||
}
|
||||
var res struct {
|
||||
Param1 string `uri:"param1"`
|
||||
Param2 string `uri:"param2"`
|
||||
Param3 string `uri:"param3"`
|
||||
Param4 string `uri:"param4"`
|
||||
Param1 string `params:"param1"`
|
||||
Param2 string `params:"param2"`
|
||||
Param3 string `params:"param3"`
|
||||
Param4 string `params:"param4"`
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
|
|
Loading…
Reference in New Issue