From 8587993a4c6b1d802a50c3b3da8e4588920a44a9 Mon Sep 17 00:00:00 2001 From: olongfen Date: Wed, 6 Jul 2022 18:42:54 +0800 Subject: [PATCH] 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 --- ctx.go | 4 ++-- ctx_test.go | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ctx.go b/ctx.go index 2487eb4214..004cf3ec7a 100644 --- a/ctx.go +++ b/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 diff --git a/ctx_test.go b/ctx_test.go index 9f092327b5..325b67d78b 100644 --- a/ctx_test.go +++ b/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()