binds the param string to a struct use params tag (#1968)

* add: params parse

* fix: binds the param string to a struct use params tag
pull/1974/head
olongfen 2022-07-06 18:42:54 +08:00 committed by GitHub
parent dfa24a0958
commit bad7001570
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

4
ctx.go
View File

@ -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

View File

@ -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()