Skip to content

Commit

Permalink
add allparams method. (#1853)
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Apr 14, 2022
1 parent ee65ea5 commit a63a842
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ctx.go
Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions ctx_test.go
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit a63a842

Please sign in to comment.