binding

package
v0.0.0-...-6f569f0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package binding provides a type-safe, reflect-free, and expression-oriented way to bind data from HTTP requests to Go structs.

Index

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.

func New

func New(req *http.Request, pathValue func(string) string) *Binding

New creates a new Binding instance from an *http.Request and a function to retrieve path parameters. The pathValue function is typically provided by a routing library (e.g., chi, gorilla/mux).

func (*Binding) Lookup

func (b *Binding) Lookup(source Source, key string) (string, bool)

Lookup is an internal method that retrieves a value and its existence from a given source. It abstracts away the differences in how each source (query, header, etc.) is accessed.

type Parser

type Parser[T any] func(string) (T, error)

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
)

type Source

type Source string

Source represents the source of a value in an HTTP request.

const (
	Query  Source = "query"
	Header Source = "header"
	Cookie Source = "cookie"
	Path   Source = "path"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL