Documentation
¶
Overview ¶
Package binding provides a type-safe, reflect-free, and expression-oriented way to bind data from HTTP requests to Go structs.
Index ¶
- func One[T any](b *Binding, dest *T, source Source, key string, parse Parser[T], ...) error
- func OnePtr[T any](b *Binding, dest **T, source Source, key string, parse Parser[T], ...) error
- func Slice[T any](b *Binding, dest *[]T, source Source, key string, parse Parser[T], ...) error
- func SlicePtr[T any](b *Binding, dest *[]*T, source Source, key string, parse Parser[T], ...) error
- type Binding
- type Parser
- type Requirement
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func One ¶
func One[T any](b *Binding, dest *T, source Source, key string, parse Parser[T], req Requirement) error
One binds a single value of a non-pointer type (e.g., int, string). 'dest' must be a pointer to the field where the value will be stored (e.g., &r.ID).
func OnePtr ¶
func OnePtr[T any](b *Binding, dest **T, source Source, key string, parse Parser[T], req Requirement) error
OnePtr binds a single value of a pointer type (e.g., *int, *string). 'dest' must be a pointer to the pointer field (e.g., &r.Name). If the value is optional and not present, the destination pointer is set to nil.
func Slice ¶
func Slice[T any](b *Binding, dest *[]T, source Source, key string, parse Parser[T], req Requirement) error
Slice binds values into a slice of a non-pointer type (e.g., []int, []string). 'dest' must be a pointer to the slice field (e.g., &r.Tags). It handles multiple values from Query parameters (e.g., ?tags=a&tags=b) and comma-separated values from Header, Cookie, or Path (e.g., X-Tags: a,b,c).
func SlicePtr ¶
func SlicePtr[T any](b *Binding, dest *[]*T, source Source, key string, parse Parser[T], req Requirement) error
SlicePtr binds values into a slice of a pointer type (e.g., []*int, []*string). 'dest' must be a pointer to the slice field (e.g., &r.CategoryIDs). It handles multiple values from Query parameters and comma-separated values from other sources. Empty strings resulting from parsing (e.g., "a,,b") will result in a nil pointer for that element if the parser succeeds for an empty string (which it typically might not, but if it did, it would be *new(T) where T is zero-valued). More commonly, an empty string like "" in "1,,2" will be passed to the parser. If the parser errors on "", that error is collected. If it somehow parses "" to a value, a pointer to that value is added.
Types ¶
type Binding ¶
type Binding struct {
// contains filtered or unexported fields
}
Binding holds the context for a binding operation, including the HTTP request and a function to retrieve path parameters.
type Parser ¶
Parser is a generic function that parses a string into a value of type T. It returns an error if parsing fails.
type Requirement ¶
type Requirement bool
Requirement specifies whether a value is required or optional.
const ( Required Requirement = true Optional Requirement = false )