From a63a842fb63b65b561f61c6d54f60673bf5afc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Efe=20=C3=87etin?= Date: Thu, 14 Apr 2022 11:48:41 +0300 Subject: [PATCH] add allparams method. (#1853) --- ctx.go | 11 +++++++++ ctx_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/ctx.go b/ctx.go index 2f0553faf1..835c14bfff 100644 --- a/ctx.go +++ b/ctx.go @@ -834,6 +834,17 @@ func (c *Ctx) Params(key string, defaultValue ...string) string { return defaultString("", defaultValue) } +// Params is used to get all route parameters. +// Using Params method to get params. +func (c *Ctx) AllParams() map[string]string { + params := make(map[string]string, len(c.route.Params)) + for _, param := range c.route.Params { + params[param] = c.Params(param) + } + + return params +} + // ParamsInt is used to get an integer from the route parameters // it defaults to zero if the parameter is not found or if the // parameter cannot be converted to an integer diff --git a/ctx_test.go b/ctx_test.go index b9b4f11d63..618baeb45d 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -1318,6 +1318,44 @@ func Test_Ctx_Params(t *testing.T) { utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") } +// go test -race -run Test_Ctx_AllParams +func Test_Ctx_AllParams(t *testing.T) { + t.Parallel() + app := New() + app.Get("/test/:user", func(c *Ctx) error { + utils.AssertEqual(t, map[string]string{"user": "john"}, c.AllParams()) + return nil + }) + app.Get("/test2/*", func(c *Ctx) error { + utils.AssertEqual(t, map[string]string{"*1": "im/a/cookie"}, c.AllParams()) + return nil + }) + app.Get("/test3/*/blafasel/*", func(c *Ctx) error { + utils.AssertEqual(t, map[string]string{"*1": "1111", "*2": "2222"}, c.AllParams()) + return nil + }) + app.Get("/test4/:optional?", func(c *Ctx) error { + utils.AssertEqual(t, map[string]string{"optional": ""}, c.AllParams()) + return nil + }) + + resp, err := app.Test(httptest.NewRequest(MethodGet, "/test/john", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") + + resp, err = app.Test(httptest.NewRequest(MethodGet, "/test2/im/a/cookie", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") + + resp, err = app.Test(httptest.NewRequest(MethodGet, "/test3/1111/blafasel/2222", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") + + resp, err = app.Test(httptest.NewRequest(MethodGet, "/test4", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code") +} + // go test -v -run=^$ -bench=Benchmark_Ctx_Params -benchmem -count=4 func Benchmark_Ctx_Params(b *testing.B) { app := New() @@ -1343,6 +1381,32 @@ func Benchmark_Ctx_Params(b *testing.B) { utils.AssertEqual(b, "awesome", res) } +// go test -v -run=^$ -bench=Benchmark_Ctx_AllParams -benchmem -count=4 +func Benchmark_Ctx_AllParams(b *testing.B) { + app := New() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + c.route = &Route{ + Params: []string{ + "param1", "param2", "param3", "param4", + }, + } + c.values = [maxParams]string{ + "john", "doe", "is", "awesome", + } + var res map[string]string + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + res = c.AllParams() + } + utils.AssertEqual(b, map[string]string{"param1": "john", + "param2": "doe", + "param3": "is", + "param4": "awesome"}, + res) +} + // go test -run Test_Ctx_Path func Test_Ctx_Path(t *testing.T) { t.Parallel()