Skip to content

Commit

Permalink
🏗️ chore: enable decoder's ZeroEmpty by default (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
si3nloong authored May 17, 2021
1 parent 21c9fb4 commit da1877f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func (c *Ctx) Body() []byte {
// decoderPool helps to improve BodyParser's and QueryParser's performance
var decoderPool = &sync.Pool{New: func() interface{} {
var decoder = schema.NewDecoder()
decoder.ZeroEmpty(true)
decoder.IgnoreUnknownKeys(true)
return decoder
}}
Expand Down
12 changes: 10 additions & 2 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2003,20 +2003,28 @@ func Test_Ctx_QueryParser(t *testing.T) {
utils.AssertEqual(t, 0, len(empty.Hobby))

type Query2 struct {
Bool bool
ID int
Name string
Hobby string
FavouriteDrinks []string
Empty []string
Alloc []string
No []int64
}

c.Request().URI().SetQueryString("id=1&name=tom&hobby=basketball,football&favouriteDrinks=milo,coke,pepsi&empty=&no=1")
c.Request().URI().SetQueryString("id=1&name=tom&hobby=basketball,football&favouriteDrinks=milo,coke,pepsi&alloc=&no=1")
q2 := new(Query2)
q2.Bool = true
q2.Name = "hello world"
utils.AssertEqual(t, nil, c.QueryParser(q2))
utils.AssertEqual(t, "basketball,football", q2.Hobby)
utils.AssertEqual(t, true, q2.Bool)
utils.AssertEqual(t, "tom", q2.Name) // check value get overwritten
utils.AssertEqual(t, []string{"milo", "coke", "pepsi"}, q2.FavouriteDrinks)
utils.AssertEqual(t, []string{}, q2.Empty)
var nilSlice []string
utils.AssertEqual(t, nilSlice, q2.Empty)
utils.AssertEqual(t, []string{""}, q2.Alloc)
utils.AssertEqual(t, []int64{1}, q2.No)

type RequiredQuery struct {
Expand Down

0 comments on commit da1877f

Please sign in to comment.