bind

package
v0.1.45 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NodeMapPool is a sync.Pool for recycling Node.Children maps.
	// This reduces GC pressure when parsing many form submissions.
	NodeMapPool = &sync.Pool{
		New: func() any {
			return make(map[string]Node)
		},
	}
)

Functions

func Bind

func Bind(source url.Values, dest any) error

Bind binds URL form values to a destination object. The destination must be a pointer to a struct, map, or other supported type. It uses struct field tags with the "form" key to map form values to struct fields. Returns an error if binding fails or if the destination is invalid.

Types

type Node

type Node struct {
	Val      string          // The value of this node, if it's a leaf node
	Children map[string]Node // Child nodes, for nested structures
}

Node represents a parsed form data node in a tree structure. It can contain a leaf value (Val) and/or child nodes organized in a hierarchy. This allows for representing nested form data structures.

func (Node) Bind

func (node Node) Bind(dest reflect.Value) error

Bind recursively binds the node's data to the provided destination value. It handles different types (structs, maps, slices, etc.) appropriately. Returns an error if binding fails due to type incompatibility or invalid data.

func (Node) Cleanup

func (node Node) Cleanup(pool *sync.Pool)

Cleanup frees resources used by the node, returning them to the provided pool. This helps reduce memory allocations by recycling node maps.

func (Node) String

func (node Node) String() string

String returns a string representation of the node for debugging purposes. The output shows the node's value and its children in a hierarchical format.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses URL form values into a tree structure for binding. It uses a sync.Pool to recycle maps for better performance.

func NewParser

func NewParser(nodeMapPool *sync.Pool) Parser

NewParser creates a new Parser with the provided node map pool. The node map pool is used to recycle maps when parsing form data.

func (Parser) Parse

func (parser Parser) Parse(source url.Values) Node

Parse converts URL form values into a structured Node tree. It handles nested form fields using bracket notation (e.g., "user[name]"). The resulting Node tree can then be bound to Go types using Bind.

Jump to

Keyboard shortcuts

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