Skip to content

Commit

Permalink
🩹 Allow parsing of square bracket query param
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadingole committed Mar 17, 2022
1 parent bd20e90 commit ac6ed60
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ctx.go
Expand Up @@ -907,10 +907,20 @@ func (c *Ctx) Query(key string, defaultValue ...string) string {
// QueryParser binds the query string to a struct.
func (c *Ctx) QueryParser(out interface{}) error {
data := make(map[string][]string)
var err error

c.fasthttp.QueryArgs().VisitAll(func(key, val []byte) {
if err != nil {
return
}

k := utils.UnsafeString(key)
v := utils.UnsafeString(val)

if strings.Contains(k, "[") {
k, err = parseQuery(k)
}

if strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) {
values := strings.Split(v, ",")
for i := 0; i < len(values); i++ {
Expand All @@ -922,9 +932,39 @@ func (c *Ctx) QueryParser(out interface{}) error {

})

if err != nil {
return err
}

return c.parseToStruct(queryTag, out, data)
}

func parseQuery(k string) (string, error) {
bb := bytebufferpool.Get()
defer bytebufferpool.Put(bb)

kbytes := []byte(k)

for i, b := range kbytes {

if b == '[' && kbytes[i+1] != ']' {
if err := bb.WriteByte('.'); err != nil {
return "", err
}
}

if b == '[' || b == ']' {
continue
}

if err := bb.WriteByte(b); err != nil {
return "", err
}
}

return bb.String(), nil
}

// ReqHeaderParser binds the request header strings to a struct.
func (c *Ctx) ReqHeaderParser(out interface{}) error {
data := make(map[string][]string)
Expand Down
63 changes: 63 additions & 0 deletions ctx_test.go
Expand Up @@ -2931,6 +2931,14 @@ func Test_Ctx_QueryParser(t *testing.T) {
rq := new(RequiredQuery)
c.Request().URI().SetQueryString("")
utils.AssertEqual(t, "name is empty", c.QueryParser(rq).Error())

type ArrayQuery struct {
Data []string
}
aq := new(ArrayQuery)
c.Request().URI().SetQueryString("data[]=john&data[]=doe")
utils.AssertEqual(t, nil, c.QueryParser(aq))
utils.AssertEqual(t, 2, len(aq.Data))
}

// go test -run Test_Ctx_QueryParser_WithSetParserDecoder -v
Expand Down Expand Up @@ -3059,6 +3067,33 @@ func Test_Ctx_QueryParser_Schema(t *testing.T) {
utils.AssertEqual(t, nil, c.QueryParser(n))
utils.AssertEqual(t, 3, n.Value)
utils.AssertEqual(t, 0, n.Next.Value)

type Person struct {
Name string `query:"name"`
Age int `query:"age"`
}

type CollectionQuery struct {
Data []Person `query:"data"`
}

c.Request().URI().SetQueryString("data[0][name]=john&data[0][age]=10&data[1][name]=doe&data[1][age]=12")
cq := new(CollectionQuery)
utils.AssertEqual(t, nil, c.QueryParser(cq))
utils.AssertEqual(t, 2, len(cq.Data))
utils.AssertEqual(t, "john", cq.Data[0].Name)
utils.AssertEqual(t, 10, cq.Data[0].Age)
utils.AssertEqual(t, "doe", cq.Data[1].Name)
utils.AssertEqual(t, 12, cq.Data[1].Age)

c.Request().URI().SetQueryString("data.0.name=john&data.0.age=10&data.1.name=doe&data.1.age=12")
cq = new(CollectionQuery)
utils.AssertEqual(t, nil, c.QueryParser(cq))
utils.AssertEqual(t, 2, len(cq.Data))
utils.AssertEqual(t, "john", cq.Data[0].Name)
utils.AssertEqual(t, 10, cq.Data[0].Age)
utils.AssertEqual(t, "doe", cq.Data[1].Name)
utils.AssertEqual(t, 12, cq.Data[1].Age)
}

// go test -run Test_Ctx_ReqHeaderParser -v
Expand Down Expand Up @@ -3320,6 +3355,34 @@ func Benchmark_Ctx_QueryParser(b *testing.B) {
utils.AssertEqual(b, nil, c.QueryParser(q))
}

// go test -v -run=^$ -bench=Benchmark_Ctx_parseQuery -benchmem -count=4
func Benchmark_Ctx_parseQuery(b *testing.B) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
type Person struct {
Name string `query:"name"`
Age int `query:"age"`
}

type CollectionQuery struct {
Data []Person `query:"data"`
}

c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
c.Request().URI().SetQueryString("data[0][name]=john&data[0][age]=10")
cq := new(CollectionQuery)

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
c.QueryParser(cq)
}

utils.AssertEqual(b, nil, c.QueryParser(cq))
}

// go test -v -run=^$ -bench=Benchmark_Ctx_QueryParser_Comma -benchmem -count=4
func Benchmark_Ctx_QueryParser_Comma(b *testing.B) {
app := New()
Expand Down

0 comments on commit ac6ed60

Please sign in to comment.