Skip to content

Commit 8587993

Browse files
olongfentrim21
olongfen
authored andcommitted
binds the param string to a struct use params tag (gofiber#1968)
* add: params parse * fix: binds the param string to a struct use params tag
1 parent 0a698b9 commit 8587993

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

ctx.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
queryTag = "query"
4040
reqHeaderTag = "reqHeader"
4141
bodyTag = "form"
42-
uriTag = "uri"
42+
paramsTag = "params"
4343
)
4444

4545
// userContextKey define the key name for storing context.Context in *fasthttp.RequestCtx
@@ -861,7 +861,7 @@ func (c *Ctx) ParamsParser(out interface{}) error {
861861
for _, param := range c.route.Params {
862862
params[param] = append(params[param], c.Params(param))
863863
}
864-
return c.parseToStruct(uriTag, out, params)
864+
return c.parseToStruct(paramsTag, out, params)
865865
}
866866

867867
// ParamsInt is used to get an integer from the route parameters

ctx_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ func Test_Ctx_ParamParser(t *testing.T) {
431431
app := New()
432432
app.Get("/test1/userId/role/:roleId", func(ctx *Ctx) error {
433433
type Demo struct {
434-
UserID uint `uri:"userId"`
435-
RoleID uint `uri:"roleId"`
434+
UserID uint `params:"userId"`
435+
RoleID uint `params:"roleId"`
436436
}
437437
var (
438438
d = new(Demo)
@@ -1470,10 +1470,10 @@ func Benchmark_Ctx_ParamsParse(b *testing.B) {
14701470
"john", "doe", "is", "awesome",
14711471
}
14721472
var res struct {
1473-
Param1 string `uri:"param1"`
1474-
Param2 string `uri:"param2"`
1475-
Param3 string `uri:"param3"`
1476-
Param4 string `uri:"param4"`
1473+
Param1 string `params:"param1"`
1474+
Param2 string `params:"param2"`
1475+
Param3 string `params:"param3"`
1476+
Param4 string `params:"param4"`
14771477
}
14781478
b.ReportAllocs()
14791479
b.ResetTimer()

0 commit comments

Comments
 (0)