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

✨ v3 (feature): initial support for binding #1981

Merged
merged 16 commits into from
Aug 8, 2022
Merged

Conversation

efectn
Copy link
Member

@efectn efectn commented Jul 26, 2022

Partially related: #1821

This PR aims to create detailed and moduler binding system for Fiber v3. Fiber ctx will be more clean thanks to this feature. Also Fiber will be able to bind into the map.

To-Do List:

  • Embed bind into the DefaultCtx.
  • Params binder.
  • Resp header binder.
  • Cookie binder.
  • Add support for custom binders in automatically body binding.
  • Support for map binding.
  • Make Should/Must functional.
  • Custom struct validator support.
  • Add comment lines and simple README for binding.

Note: I won't change gorilla/schema in this PR. But next PRs, i'll create more minimal and useful parser for Fiber.

@efectn efectn added this to the v3 milestone Jul 26, 2022
@efectn efectn marked this pull request as draft July 26, 2022 10:58
@efectn efectn linked an issue Jul 26, 2022 that may be closed by this pull request
use pointer/references instead of copies
- custom binder support for body binding.
- test case for custom binder.
efectn added 5 commits July 29, 2022 14:44
- add map[string][]string & map[string]string support for binders.
- custom struct validator support.
- README for binding.
- Docs for binding methods.
@efectn efectn marked this pull request as ready for review July 29, 2022 16:30
@efectn efectn requested a review from ReneWerner87 July 29, 2022 16:30
@trim21
Copy link
Contributor

trim21 commented Aug 7, 2022

I have another idea and POC about this feature, we do not need internal temporary map and schema (or schema-lite), and gorilla/schema is really slow.

https://github.com/Trim21/inj/blob/master/example/main.go#L27

with go 1.18 generic, we can have a 2 allocs and good performance and (IMO) easy to use API like this:

app.Get("/:id", Wrap(func(c *fiber.Ctx, req Req) error {
	return c.JSON(req)
}))

and a 5 fields benchmark, (BenchmarkGorillaSchema_map_to_struct_only simple use schema to convert map to struct, not even contains code to build map from ctx)

cpu: AMD Ryzen 7 5800X 8-Core Processor
Benchmark_unmarshal_by_hand-16                          18636231                63.80 ns/op            0 B/op          0 allocs/op
Benchmark_lib_unmarshal-16                               6516075               183.0 ns/op            96 B/op          2 allocs/op
BenchmarkGorillaSchema_map_to_struct_only-16              435432              2818 ns/op             904 B/op         49 allocs/op
PASS

@efectn
Copy link
Member Author

efectn commented Aug 8, 2022

I have another idea and POC about this feature, we do not need internal temporary map and schema (or schema-lite), and gorilla/schema is really slow.

https://github.com/Trim21/inj/blob/master/example/main.go#L27

with go 1.18 generic, we can have a 2 allocs and good performance and (IMO) easy to use API like this:

app.Get("/:id", Wrap(func(c *fiber.Ctx, req Req) error {
	return c.JSON(req)
}))

and a 5 fields benchmark, (BenchmarkGorillaSchema_map_to_struct_only simple use schema to convert map to struct, not even contains code to build map from ctx)

cpu: AMD Ryzen 7 5800X 8-Core Processor
Benchmark_unmarshal_by_hand-16                          18636231                63.80 ns/op            0 B/op          0 allocs/op
Benchmark_lib_unmarshal-16                               6516075               183.0 ns/op            96 B/op          2 allocs/op
BenchmarkGorillaSchema_map_to_struct_only-16              435432              2818 ns/op             904 B/op         49 allocs/op
PASS

I checked your code and it seems good idea to use context methods instead of creating temporarily map. I'll check it in the next PR

@ReneWerner87 ReneWerner87 merged commit eacde70 into v3-beta Aug 8, 2022
@efectn efectn deleted the v3-bind-feature branch August 8, 2022 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

🚀 v3 Request: Detailed Binding for Query, Form, Headers, Params...
3 participants