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

add: params parse #1964

Merged
merged 1 commit into from Jul 5, 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
10 changes: 10 additions & 0 deletions ctx.go
Expand Up @@ -39,6 +39,7 @@ const (
queryTag = "query"
reqHeaderTag = "reqHeader"
bodyTag = "form"
uriTag = "uri"
)

// userContextKey define the key name for storing context.Context in *fasthttp.RequestCtx
Expand Down Expand Up @@ -854,6 +855,15 @@ func (c *Ctx) AllParams() map[string]string {
return params
}

// ParamsParser binds the param string to a struct.
func (c *Ctx) ParamsParser(out interface{}) error {
params := make(map[string][]string, len(c.route.Params))
for _, param := range c.route.Params {
params[param] = append(params[param], c.Params(param))
}
return c.parseToStruct(uriTag, out, 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
55 changes: 54 additions & 1 deletion ctx_test.go
Expand Up @@ -426,6 +426,29 @@ func Test_Ctx_BodyParser(t *testing.T) {
utils.AssertEqual(t, "doe", cq.Data[1].Name)
}

func Test_Ctx_ParamParser(t *testing.T) {
t.Parallel()
app := New()
app.Get("/test1/userId/role/:roleId", func(ctx *Ctx) error {
type Demo struct {
UserID uint `uri:"userId"`
RoleID uint `uri:"roleId"`
}
var (
d = new(Demo)
)
if err := ctx.ParamsParser(d); err != nil {
t.Fatal(err)
}
utils.AssertEqual(t, uint(111), d.UserID)
utils.AssertEqual(t, uint(222), d.RoleID)
return nil
})
app.Test(httptest.NewRequest(MethodGet, "/test1/111/role/222", nil))
app.Test(httptest.NewRequest(MethodGet, "/test2/111/role/222", nil))

}

// go test -run Test_Ctx_BodyParser_WithSetParserDecoder
func Test_Ctx_BodyParser_WithSetParserDecoder(t *testing.T) {
type CustomTime time.Time
Expand Down Expand Up @@ -1433,6 +1456,36 @@ func Benchmark_Ctx_AllParams(b *testing.B) {
res)
}

// go test -v -run=^$ -bench=Benchmark_Ctx_ParamsParse -benchmem -count=4
func Benchmark_Ctx_ParamsParse(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 struct {
Param1 string `uri:"param1"`
Param2 string `uri:"param2"`
Param3 string `uri:"param3"`
Param4 string `uri:"param4"`
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
c.ParamsParser(&res)
}
utils.AssertEqual(b, "john", res.Param1)
utils.AssertEqual(b, "doe", res.Param2)
utils.AssertEqual(b, "is", res.Param3)
utils.AssertEqual(b, "awesome", res.Param4)
}

// go test -run Test_Ctx_Path
func Test_Ctx_Path(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -2896,7 +2949,7 @@ func Test_Ctx_SendStream(t *testing.T) {
file, err := os.Open("./.github/index.html")
utils.AssertEqual(t, nil, err)
c.SendStream(bufio.NewReader(file))
utils.AssertEqual(t, true, (c.Response().Header.ContentLength() > 200))
utils.AssertEqual(t, true, c.Response().Header.ContentLength() > 200)
}

// go test -run Test_Ctx_Set
Expand Down