Skip to content

Commit 4ca6b47

Browse files
efectntrim21
authored andcommitted
add allparams method. (gofiber#1853)
1 parent 6be75c8 commit 4ca6b47

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

ctx.go

+11
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,17 @@ func (c *Ctx) Params(key string, defaultValue ...string) string {
834834
return defaultString("", defaultValue)
835835
}
836836

837+
// Params is used to get all route parameters.
838+
// Using Params method to get params.
839+
func (c *Ctx) AllParams() map[string]string {
840+
params := make(map[string]string, len(c.route.Params))
841+
for _, param := range c.route.Params {
842+
params[param] = c.Params(param)
843+
}
844+
845+
return params
846+
}
847+
837848
// ParamsInt is used to get an integer from the route parameters
838849
// it defaults to zero if the parameter is not found or if the
839850
// parameter cannot be converted to an integer

ctx_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,44 @@ func Test_Ctx_Params(t *testing.T) {
13181318
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
13191319
}
13201320

1321+
// go test -race -run Test_Ctx_AllParams
1322+
func Test_Ctx_AllParams(t *testing.T) {
1323+
t.Parallel()
1324+
app := New()
1325+
app.Get("/test/:user", func(c *Ctx) error {
1326+
utils.AssertEqual(t, map[string]string{"user": "john"}, c.AllParams())
1327+
return nil
1328+
})
1329+
app.Get("/test2/*", func(c *Ctx) error {
1330+
utils.AssertEqual(t, map[string]string{"*1": "im/a/cookie"}, c.AllParams())
1331+
return nil
1332+
})
1333+
app.Get("/test3/*/blafasel/*", func(c *Ctx) error {
1334+
utils.AssertEqual(t, map[string]string{"*1": "1111", "*2": "2222"}, c.AllParams())
1335+
return nil
1336+
})
1337+
app.Get("/test4/:optional?", func(c *Ctx) error {
1338+
utils.AssertEqual(t, map[string]string{"optional": ""}, c.AllParams())
1339+
return nil
1340+
})
1341+
1342+
resp, err := app.Test(httptest.NewRequest(MethodGet, "/test/john", nil))
1343+
utils.AssertEqual(t, nil, err, "app.Test(req)")
1344+
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
1345+
1346+
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test2/im/a/cookie", nil))
1347+
utils.AssertEqual(t, nil, err, "app.Test(req)")
1348+
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
1349+
1350+
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test3/1111/blafasel/2222", nil))
1351+
utils.AssertEqual(t, nil, err, "app.Test(req)")
1352+
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
1353+
1354+
resp, err = app.Test(httptest.NewRequest(MethodGet, "/test4", nil))
1355+
utils.AssertEqual(t, nil, err, "app.Test(req)")
1356+
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
1357+
}
1358+
13211359
// go test -v -run=^$ -bench=Benchmark_Ctx_Params -benchmem -count=4
13221360
func Benchmark_Ctx_Params(b *testing.B) {
13231361
app := New()
@@ -1343,6 +1381,32 @@ func Benchmark_Ctx_Params(b *testing.B) {
13431381
utils.AssertEqual(b, "awesome", res)
13441382
}
13451383

1384+
// go test -v -run=^$ -bench=Benchmark_Ctx_AllParams -benchmem -count=4
1385+
func Benchmark_Ctx_AllParams(b *testing.B) {
1386+
app := New()
1387+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
1388+
defer app.ReleaseCtx(c)
1389+
c.route = &Route{
1390+
Params: []string{
1391+
"param1", "param2", "param3", "param4",
1392+
},
1393+
}
1394+
c.values = [maxParams]string{
1395+
"john", "doe", "is", "awesome",
1396+
}
1397+
var res map[string]string
1398+
b.ReportAllocs()
1399+
b.ResetTimer()
1400+
for n := 0; n < b.N; n++ {
1401+
res = c.AllParams()
1402+
}
1403+
utils.AssertEqual(b, map[string]string{"param1": "john",
1404+
"param2": "doe",
1405+
"param3": "is",
1406+
"param4": "awesome"},
1407+
res)
1408+
}
1409+
13461410
// go test -run Test_Ctx_Path
13471411
func Test_Ctx_Path(t *testing.T) {
13481412
t.Parallel()

0 commit comments

Comments
 (0)