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

🩹 Allow parsing of square bracket query param #1818

Merged

Conversation

ninadingole
Copy link
Contributor

closes #1802

Explain the details for making this change. What existing problem does the pull request solve?

The PR solves the issue of parsing query params that contains array key in [] format. Following type of keys are parsed and the tests are added:

  • data[]=john&data[]=doe
  • data[0][name]=john&data[0][age]=10&data[1][name]=doe&data[1][age]=12 // This will be parsed to a struct
  • data.0.name=john&data.0.age=10&data.1.name=doe&data.1.age=12

@welcome
Copy link

welcome bot commented Mar 13, 2022

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

Please add a benchmark
Look more closely tomorrow

Copy link
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

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

Can you add before/after benchmark results of queryparser?
Also, can you add new benchmark for this feature?

ctx.go Outdated Show resolved Hide resolved
ctx_test.go Outdated Show resolved Hide resolved
ctx.go Outdated Show resolved Hide resolved
@ninadingole ninadingole force-pushed the patch/parse-array-query-param branch from effe03e to 5a7744d Compare March 16, 2022 19:41
@ninadingole
Copy link
Contributor Author

I did the benchmarking:

BEFORE:

go test -v  -run=^\$ -bench=Benchmark_Ctx_QueryParser -benchmem -count=4                                                              25.5m  Wed Mar 16 20:38:39 2022
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_Ctx_QueryParser
Benchmark_Ctx_QueryParser-16          	  349766	      3171 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  364510	      3176 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  355460	      3161 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  361420	      3171 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser_Comma
Benchmark_Ctx_QueryParser_Comma-16    	  309963	      3514 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  309619	      3565 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  286914	      3519 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  328557	      3532 ns/op	     929 B/op	      44 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2	9.755s
go test -v  -run=^\$ -bench=Benchmark_Ctx_QueryParser_Comma -benchmem -count=4                                                            10.4s  Wed Mar 16 20:38:54 2022
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_Ctx_QueryParser_Comma
Benchmark_Ctx_QueryParser_Comma-16    	  307441	      3551 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  335578	      3562 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  296473	      3554 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  327487	      3525 ns/op	     929 B/op	      44 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2	4.887s

AFTER:

go test -v  -run=^\$ -bench=Benchmark_Ctx_QueryParser -benchmem -count=4                                                                   5.5s  Wed Mar 16 20:39:39 2022
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_Ctx_QueryParser
Benchmark_Ctx_QueryParser-16          	  335406	      3427 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  356060	      3241 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  342423	      3223 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  351063	      3250 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser_Comma
Benchmark_Ctx_QueryParser_Comma-16    	  316597	      3587 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  313954	      3590 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  308415	      3626 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  322690	      3647 ns/op	     929 B/op	      44 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2	9.642s
go test -v  -run=^\$ -bench=Benchmark_Ctx_QueryParser_Comma -benchmem -count=4                                                            10.2s  Wed Mar 16 20:40:05 2022
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_Ctx_QueryParser_Comma
Benchmark_Ctx_QueryParser_Comma-16    	  297918	      3614 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  321032	      3583 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  323709	      3608 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  312877	      3606 ns/op	     929 B/op	      44 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2	4.915s

Benchmarking for the method parseQuery:

go test -v  -run=^\$ -bench=Benchmark_Ctx_parseQuery -benchmem -count=4                                                                     5.5s  Wed Mar 16 20:44:41 2022
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_Ctx_parseQuery
Benchmark_Ctx_parseQuery-16    	  401638	      2637 ns/op	     753 B/op	      29 allocs/op
Benchmark_Ctx_parseQuery-16    	  451958	      2637 ns/op	     753 B/op	      29 allocs/op
Benchmark_Ctx_parseQuery-16    	  454078	      2664 ns/op	     753 B/op	      29 allocs/op
Benchmark_Ctx_parseQuery-16    	  432850	      2669 ns/op	     753 B/op	      29 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2	5.246s

@ninadingole
Copy link
Contributor Author

I also tried with Replacer

rep := strings.NewReplacer("[]", ".", "[", ".", "]", "")

Benchmarking:

go test -v  -run=^\$ -bench=Benchmark_Ctx_QueryParser -benchmem -count=4                                                                    6.4s  Wed Mar 16 20:44:53 2022
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_Ctx_QueryParser
Benchmark_Ctx_QueryParser-16          	  335710	      3016 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  386149	      3042 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  363243	      3301 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser-16          	  377787	      3140 ns/op	     856 B/op	      38 allocs/op
Benchmark_Ctx_QueryParser_Comma
Benchmark_Ctx_QueryParser_Comma-16    	  329350	      3717 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  337248	      3575 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  337785	      3409 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  335216	      3315 ns/op	     929 B/op	      44 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2	11.173s
go test -v  -run=^\$ -bench=Benchmark_Ctx_QueryParser_Comma -benchmem -count=4                                                            12.5s  Wed Mar 16 20:46:40 2022
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_Ctx_QueryParser_Comma
Benchmark_Ctx_QueryParser_Comma-16    	  316009	      3499 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  356570	      3519 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  292567	      3841 ns/op	     929 B/op	      44 allocs/op
Benchmark_Ctx_QueryParser_Comma-16    	  301339	      3866 ns/op	     929 B/op	      44 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2	6.371s

Benchmark the only method:

 go test -v  -run=^\$ -bench=Benchmark_Ctx_parseQuery -benchmem -count=4                                                                    7.7s  Wed Mar 16 20:47:56 2022
goos: darwin
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Benchmark_Ctx_parseQuery
Benchmark_Ctx_parseQuery-16    	  211260	      5311 ns/op	    2371 B/op	      47 allocs/op
Benchmark_Ctx_parseQuery-16    	  232926	      5309 ns/op	    2371 B/op	      47 allocs/op
Benchmark_Ctx_parseQuery-16    	  212172	      5251 ns/op	    2371 B/op	      47 allocs/op
Benchmark_Ctx_parseQuery-16    	  210127	      5377 ns/op	    2371 B/op	      47 allocs/op
PASS
ok  	github.com/gofiber/fiber/v2	5.162s

Copy link
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

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

Check linter and gosec error.

@ReneWerner87
Copy link
Member

thx

pls check the comment from efectn

Check linter and gosec error.

@ninadingole ninadingole force-pushed the patch/parse-array-query-param branch from 5a7744d to 422315a Compare March 16, 2022 21:27
@ninadingole
Copy link
Contributor Author

thx

pls check the comment from efectn

Check linter and gosec error.

I handled the error handling, don't know if it is valid to panic, please let me know if the changes are good to merge the PR. Thank you!

ctx.go Outdated Show resolved Hide resolved
@efectn
Copy link
Member

efectn commented Mar 16, 2022

thx
pls check the comment from efectn

Check linter and gosec error.

I handled the error handling, don't know if it is valid to panic, please let me know if the changes are good to merge the PR. Thank you!

Check my last comment

@ninadingole ninadingole force-pushed the patch/parse-array-query-param branch from 422315a to ac6ed60 Compare March 17, 2022 19:40
@efectn
Copy link
Member

efectn commented Mar 18, 2022

I think we can add add example for this usage to docs. It would be helpful also. What about @ninadingole

@ninadingole
Copy link
Contributor Author

I think we can add add example for this usage to docs. It would be helpful also. What about @ninadingole

will update the doc

@ReneWerner87 ReneWerner87 merged commit 44bfdc3 into gofiber:master Mar 22, 2022
@welcome
Copy link

welcome bot commented Mar 22, 2022

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

trim21 pushed a commit to trim21/fiber that referenced this pull request Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using square brackets in query parameters
3 participants