SIGN IN SIGN UP
gofiber / fiber UNCLAIMED

⚡️ Express inspired web framework written in Go

:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
package fiber
import (
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
"reflect"
"slices"
"sync"
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
"github.com/gofiber/fiber/v3/binder"
"github.com/gofiber/utils/v2"
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
)
2024-04-23 08:18:19 +02:00
// CustomBinder An interface to register custom binders.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
type CustomBinder interface {
Name() string
MIMETypes() []string
Parse(c Ctx, out any) error
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
}
2024-04-23 08:18:19 +02:00
// StructValidator is an interface to register custom struct validator for binding.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
type StructValidator interface {
2024-04-23 08:18:19 +02:00
Validate(out any) error
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
}
var bindPool = sync.Pool{
New: func() any {
return &Bind{
dontHandleErrs: true,
}
},
}
// Bind provides helper methods for binding request data to Go values.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
type Bind struct {
ctx Ctx
dontHandleErrs bool
skipValidation bool
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
}
// AcquireBind returns Bind reference from bind pool.
func AcquireBind() *Bind {
b, ok := bindPool.Get().(*Bind)
if !ok {
panic(errBindPoolTypeAssertion)
}
return b
}
// ReleaseBind returns b acquired via Bind to bind pool.
func ReleaseBind(b *Bind) {
b.release()
bindPool.Put(b)
}
// releasePooledBinder resets a binder and returns it to its pool.
// It should be used with defer to ensure proper cleanup of pooled binders.
func releasePooledBinder[T interface{ Reset() }](pool *sync.Pool, bind T) {
bind.Reset()
binder.PutToThePool(pool, bind)
}
func (b *Bind) release() {
b.ctx = nil
b.dontHandleErrs = true
b.skipValidation = false
}
// WithoutAutoHandling If you want to handle binder errors manually, you can use `WithoutAutoHandling`.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
// It's default behavior of binder.
func (b *Bind) WithoutAutoHandling() *Bind {
b.dontHandleErrs = true
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return b
}
// WithAutoHandling If you want to handle binder errors automatically, you can use `WithAutoHandling`.
// If there's an error, it will return the error and set HTTP status to `400 Bad Request`.
// You must still return on error explicitly
func (b *Bind) WithAutoHandling() *Bind {
b.dontHandleErrs = false
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return b
}
// Check WithAutoHandling/WithoutAutoHandling errors and return it by usage.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
func (b *Bind) returnErr(err error) error {
if err == nil || b.dontHandleErrs {
return err
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
}
b.ctx.Status(StatusBadRequest)
return NewError(StatusBadRequest, "Bad request: "+err.Error())
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
}
// Struct validation.
func (b *Bind) validateStruct(out any) error {
if b.skipValidation {
return nil
}
2024-04-23 08:18:19 +02:00
validator := b.ctx.App().config.StructValidator
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
if validator != nil {
2024-04-23 08:18:19 +02:00
return validator.Validate(out)
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
}
return nil
}
2024-04-23 08:18:19 +02:00
// Custom To use custom binders, you have to use this method.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
// You can register them from RegisterCustomBinder method of Fiber instance.
// They're checked by name, if it's not found, it will return an error.
// NOTE: WithAutoHandling/WithAutoHandling is still valid for Custom binders.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
func (b *Bind) Custom(name string, dest any) error {
binders := b.ctx.App().customBinders
for _, customBinder := range binders {
if customBinder.Name() == name {
return b.returnErr(customBinder.Parse(b.ctx, dest))
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
}
}
return ErrCustomBinderNotFound
}
// Header binds the request header strings into the struct, map[string]string and map[string][]string.
func (b *Bind) Header(out any) error {
bind := binder.GetFromThePool[*binder.HeaderBinding](&binder.HeaderBinderPool)
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
defer releasePooledBinder(&binder.HeaderBinderPool, bind)
if err := b.returnErr(bind.Bind(b.ctx.Request(), out)); err != nil {
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return err
}
return b.validateStruct(out)
}
// RespHeader binds the response header strings into the struct, map[string]string and map[string][]string.
func (b *Bind) RespHeader(out any) error {
bind := binder.GetFromThePool[*binder.RespHeaderBinding](&binder.RespHeaderBinderPool)
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
defer releasePooledBinder(&binder.RespHeaderBinderPool, bind)
if err := b.returnErr(bind.Bind(b.ctx.Response(), out)); err != nil {
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return err
}
return b.validateStruct(out)
}
// Cookie binds the request cookie strings into the struct, map[string]string and map[string][]string.
// NOTE: If your cookie is like key=val1,val2; they'll be bound as a slice if your map is map[string][]string. Else, it'll use last element of cookie.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
func (b *Bind) Cookie(out any) error {
bind := binder.GetFromThePool[*binder.CookieBinding](&binder.CookieBinderPool)
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
defer releasePooledBinder(&binder.CookieBinderPool, bind)
if err := b.returnErr(bind.Bind(&b.ctx.RequestCtx().Request, out)); err != nil {
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return err
}
return b.validateStruct(out)
}
2024-04-23 08:18:19 +02:00
// Query binds the query string into the struct, map[string]string and map[string][]string.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
func (b *Bind) Query(out any) error {
bind := binder.GetFromThePool[*binder.QueryBinding](&binder.QueryBinderPool)
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
defer releasePooledBinder(&binder.QueryBinderPool, bind)
if err := b.returnErr(bind.Bind(&b.ctx.RequestCtx().Request, out)); err != nil {
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return err
}
return b.validateStruct(out)
}
// JSON binds the body string into the struct.
func (b *Bind) JSON(out any) error {
bind := binder.GetFromThePool[*binder.JSONBinding](&binder.JSONBinderPool)
bind.JSONDecoder = b.ctx.App().Config().JSONDecoder
defer releasePooledBinder(&binder.JSONBinderPool, bind)
if err := b.returnErr(bind.Bind(b.ctx.Body(), out)); err != nil {
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return err
}
return b.validateStruct(out)
}
// CBOR binds the body string into the struct.
func (b *Bind) CBOR(out any) error {
bind := binder.GetFromThePool[*binder.CBORBinding](&binder.CBORBinderPool)
bind.CBORDecoder = b.ctx.App().Config().CBORDecoder
defer releasePooledBinder(&binder.CBORBinderPool, bind)
if err := b.returnErr(bind.Bind(b.ctx.Body(), out)); err != nil {
return err
}
return b.validateStruct(out)
}
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
// XML binds the body string into the struct.
func (b *Bind) XML(out any) error {
bind := binder.GetFromThePool[*binder.XMLBinding](&binder.XMLBinderPool)
bind.XMLDecoder = b.ctx.App().config.XMLDecoder
defer releasePooledBinder(&binder.XMLBinderPool, bind)
if err := b.returnErr(bind.Bind(b.ctx.Body(), out)); err != nil {
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return err
}
return b.validateStruct(out)
}
// Form binds the form into the struct, map[string]string and map[string][]string.
// If Content-Type is "application/x-www-form-urlencoded" or "multipart/form-data", it will bind the form values.
//
// Binding multipart files is not supported yet.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
func (b *Bind) Form(out any) error {
bind := binder.GetFromThePool[*binder.FormBinding](&binder.FormBinderPool)
bind.EnableSplitting = b.ctx.App().config.EnableSplittingOnParsers
defer releasePooledBinder(&binder.FormBinderPool, bind)
if err := b.returnErr(bind.Bind(&b.ctx.RequestCtx().Request, out)); err != nil {
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return err
}
return b.validateStruct(out)
}
// URI binds the route parameters into the struct, map[string]string and map[string][]string.
func (b *Bind) URI(out any) error {
bind := binder.GetFromThePool[*binder.URIBinding](&binder.URIBinderPool)
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
defer releasePooledBinder(&binder.URIBinderPool, bind)
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
if err := b.returnErr(bind.Bind(b.ctx.Route().Params, b.ctx.Params, out)); err != nil {
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return err
}
return b.validateStruct(out)
}
// MsgPack binds the body string into the struct.
func (b *Bind) MsgPack(out any) error {
bind := binder.GetFromThePool[*binder.MsgPackBinding](&binder.MsgPackBinderPool)
bind.MsgPackDecoder = b.ctx.App().Config().MsgPackDecoder
defer releasePooledBinder(&binder.MsgPackBinderPool, bind)
if err := b.returnErr(bind.Bind(b.ctx.Body(), out)); err != nil {
return err
}
return b.validateStruct(out)
}
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
// Body binds the request body into the struct, map[string]string and map[string][]string.
// It supports decoding the following content types based on the Content-Type header:
// application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data
// If none of the content types above are matched, it'll take a look custom binders by checking the MIMETypes() method of custom binder.
// If there is no custom binder for mime type of body, it will return a ErrUnprocessableEntity error.
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
func (b *Bind) Body(out any) error {
// Get content-type
ctype := utils.ToLower(utils.UnsafeString(b.ctx.RequestCtx().Request.Header.ContentType()))
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
ctype = binder.FilterFlags(utils.ParseVendorSpecificContentType(ctype))
2024-04-23 08:18:19 +02:00
// Check custom binders
binders := b.ctx.App().customBinders
for _, customBinder := range binders {
if slices.Contains(customBinder.MIMETypes(), ctype) {
return b.returnErr(customBinder.Parse(b.ctx, out))
2024-04-23 08:18:19 +02:00
}
}
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
// Parse body accordingly
switch ctype {
case MIMEApplicationJSON:
return b.JSON(out)
case MIMEApplicationMsgPack:
return b.MsgPack(out)
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
case MIMETextXML, MIMEApplicationXML:
return b.XML(out)
case MIMEApplicationCBOR:
return b.CBOR(out)
case MIMEApplicationForm, MIMEMultipartForm:
:sparkles: v3 (feature): initial support for binding (#1981) * :sparkles: v3 (feature): initial support for binding * ✨ v3 (feature): initial support for binding #1981 use pointer/references instead of copies * :sparkles: v3 (feature): initial support for binding embed bind into the ctx * :sparkles: v3 (feature): initial support for binding - add URI binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add response header binder. * :sparkles: v3 (feature): initial support for binding - add cookie binder. * :sparkles: v3 (feature): initial support for binding - custom binder support for body binding. - test case for custom binder. * :sparkles: v3 (feature): initial support for binding - add map[string][]string & map[string]string support for binders. * :sparkles: v3 (feature): initial support for binding - fix Test_Bind_Header_Map * :sparkles: v3 (feature): initial support for binding - Functional Should/Must * :sparkles: v3 (feature): initial support for binding - custom struct validator support. * :sparkles: v3 (feature): initial support for binding - README for binding. - Docs for binding methods. * :sparkles: v3 (feature): initial support for binding - Bind() -> BindVars(), Binding() -> Bind() * :sparkles: v3 (feature): initial support for binding - fix doc problems * :sparkles: v3 (feature): initial support for binding - fix doc problems Co-authored-by: wernerr <rene@gofiber.io>
2022-08-08 10:16:08 +03:00
return b.Form(out)
}
// No suitable content type found
return ErrUnprocessableEntity
}
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
// All binds values from URI params, the request body, the query string,
// headers, and cookies into the provided struct in precedence order.
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
func (b *Bind) All(out any) error {
outVal := reflect.ValueOf(out)
if outVal.Kind() != reflect.Ptr || outVal.Elem().Kind() != reflect.Struct {
return ErrUnprocessableEntity
}
outElem := outVal.Elem()
// Precedence: URL Params -> Body -> Query -> Headers -> Cookies
sources := []func(any) error{b.URI}
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
// Check if both Body and Content-Type are set
if len(b.ctx.Request().Body()) > 0 && len(b.ctx.RequestCtx().Request.Header.ContentType()) > 0 {
sources = append(sources, b.Body)
}
sources = append(sources, b.Query, b.Header, b.Cookie)
prevSkip := b.skipValidation
b.skipValidation = true
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
// TODO: Support custom precedence with an optional binding_source tag
// TODO: Create WithOverrideEmptyValues
// Bind from each source, but only update unset fields
for _, bindFunc := range sources {
tempStruct := reflect.New(outElem.Type()).Interface()
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
if err := bindFunc(tempStruct); err != nil {
b.skipValidation = prevSkip
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
return err
}
tempStructVal := reflect.ValueOf(tempStruct).Elem()
mergeStruct(outElem, tempStructVal)
}
b.skipValidation = prevSkip
return b.returnErr(b.validateStruct(out))
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
}
func mergeStruct(dst, src reflect.Value) {
dstFields := dst.NumField()
2025-07-17 14:48:43 +02:00
for i := range dstFields {
🔥 feat: Add All method to Bind (#3373) * feat: Add All method to Bind This commit introduces a new `All` method to the `Bind` struct, enabling the binding of request data from multiple sources (URI parameters, body, query parameters, headers, and cookies) into a single struct. The `All` method iterates through the available binding sources, applying them in a predefined precedence order. It merges the values from each source into the output struct, only updating fields that are currently unset. Changes: - Added `All` method to `Bind` struct. - Added `mergeStruct` helper function to merge struct values. - Added `isZero` helper function to check if a value is zero. - Added a test case for the `All` method in `bind_test.go` to validate its functionality. * feat: Enhance Bind.All with comprehensive testing and configuration The changes include: - Added `RequestConfig` struct to encapsulate request configuration (ContentType, Body, Headers, Cookies, Query). - Implemented `ApplyTo` method on `RequestConfig` to apply the configuration to the context. - Created multiple test cases for `Bind.All` covering successful binding, missing fields, overriding query parameters, and form binding. - Added a test case `Test_Bind_All_Uri_Precedence` to validate the precedence of URI parameters. - Added benchmark test `BenchmarkBind_All` to measure the performance of the `Bind.All` method. - Refactored the `TestBind_All` to use the new `RequestConfig` and assertion libraries. * fix: Correct form binding in Test_Bind_All * refactor: Improve Bind.All test and struct field ordering - Reordered fields in `RequestConfig` and `User` structs for field alignment - Updated `Test_Bind_All` to use `require.NoError` for more robust error checking. - Corrected header key casing in `Test_Bind_All` to `X-User-Role` to match the struct tag. - Added `t.Parallel()` to the test function to enable parallel test execution. * feat: Document Bind.All function in API documentation This commit adds documentation for the `Bind.All` function to the API documentation. The documentation includes: - A description of the function's purpose and precedence order for binding data from different sources (URI, body, query, headers, cookies). * docs: lint Bind.All documentation * fix: Update parameter tags from 'param' to 'uri' in bind_test.go * fix: Update parameter tags from 'param' to 'uri' in bind.md * test: Replace assert with require in bind_test.go * feat: Add support for unified binding with defined precedence order in whats_new.md --------- Co-authored-by: RW <rene@gofiber.io>
2025-05-14 10:56:39 -03:00
dstField := dst.Field(i)
srcField := src.Field(i)
// Skip if the destination field is already set
if isZero(dstField.Interface()) {
if dstField.CanSet() && srcField.IsValid() {
dstField.Set(srcField)
}
}
}
}
func isZero(value any) bool {
v := reflect.ValueOf(value)
return v.IsZero()
}