Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feature: add AllParams method #1853

Merged
merged 1 commit into from Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions ctx.go
Expand Up @@ -833,6 +833,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))
Comment on lines +838 to +839
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (c *Ctx) AllParams() map[string]string {
params := make(map[string]string, len(c.route.Params))
func (c *Ctx) AllParams() map[string][]string {
params := make(map[string][]string, len(c.route.Params))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no list, always only one parameter value for one parameter key

for _, param := range c.route.Params {
params[param] = c.Params(param)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please adjust it based on suggestion when having many params

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look at tomorrow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't understand why to need make it []string instead of string. Can you explain me?

}

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