An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

rivaas.dev/binding

Package binding provides request data binding for HTTP handlers. The binding package maps values from various sources (query parameters, form data, JSON bodies, headers, cookies, path parameters) into Go structs using struct tags. It supports nested structs, slices, maps, pointers, custom types, default values, and type conversion. Note: For validation (required fields, enum constraints, etc.), use the rivaas.dev/validation package separately after binding. The binding package follows the functional options pattern: The package provides both generic and non-generic APIs: Each binding source has dedicated functions: Bind from multiple sources using From* options: Bind multipart forms with file uploads directly to structs using the form tag. This enables handling file uploads and form values in a single struct: File fields use the *File or []*File types and support the same struct tag syntax as other binding sources (defaults, aliases, etc.). The File type provides methods for reading, streaming, and saving uploaded files with built-in security (filename sanitization, path cleaning). ## JSON in Form Fields Multipart binding automatically detects and parses JSON strings in form fields: If the form field value starts with { or [ and ends with } or ], the binding automatically attempts JSON unmarshaling. If JSON parsing fails, it falls back to dot-notation parsing (settings.orientation=landscape). ## File Security The File type includes built-in security features: Best practices for file handling: Use functional options to customize binding behavior: For shared configuration, create a Binder instance: The package uses struct tags to map values: The following formats are available as sub-packages: Example with YAML: Example with TOML: Example with MessagePack: Example with Protocol Buffers: default:"value": Default value when field is not present in the input. Works for all binding sources: query, path, header, cookie, form, and JSON body. Supported for all primitive types (string, int*, uint*, float*, bool), time.Time, time.Duration, net.IP, url.URL, and their pointer variants. For slices, use comma-separated values: default:"a,b,c" Whitespace around comma-separated elements is trimmed. Not supported for map fields. For JSON body binding, defaults are applied after decoding. A field explicitly sent as its zero value (e.g. {"active": false}) is NOT overwritten by the default — only fields absent from the JSON payload receive their default value. This also works for nested structs when the parent key is present in the JSON. For validation constraints (required, enum, etc.), use the rivaas.dev/validation package with the `validate` struct tag. Built-in support for common types: Register custom converters: The package provides factory functions for common converter patterns: ## Custom Time Layouts Use TimeConverter to parse times in non-standard formats: ## Duration Aliases Use DurationConverter to provide user-friendly duration names: ## Enum Validation Use EnumConverter to validate against allowed values: ## Custom Boolean Values Use BoolConverter to accept non-standard boolean representations: ## Combining Multiple Converters You can use multiple converter factories together: ## Third-Party Type Examples For types from third-party packages, use WithConverter: Or with a custom wrapper for error handling: Errors provide detailed context: Collect all errors instead of failing on first: Add hooks for monitoring: Built-in limits prevent resource exhaustion: Configure limits: The package provides extensive configuration through functional options: ## Security Limits ## Unknown Fields ## Slice Parsing ## Time Formats ## Type Converters ## Error Handling ## Observability ## Key Normalization ## Tag Aliases Provide multiple lookup names for a field: ## Nested Structs Use dot notation for nested fields: ## Bracket Notation Arrays can use bracket notation: For large payloads, use Reader variants to avoid loading entire body into memory: Reader variants are available for all body-based sources (JSON, XML, and sub-packages). ## Caching Struct reflection information is cached automatically: ## Memory Allocation ## Multi-Source Binding Precedence When using Bind with multiple sources, later sources override earlier ones: This allows for flexible request handling where body data takes precedence over URL parameters. For simple map-based sources, use the convenience helpers: For more complex sources, implement the ValueGetter interface: Alternatively, use GetterFunc for a function-based adapter: ## With net/http ## With rivaas.dev/router ## With rivaas.dev/app The package provides detailed error types for different failure scenarios: All error types implement standard error interfaces and integrate with rivaas.dev/errors for HTTP status code mapping. This package works independently without the full Rivaas framework. Use it with any Go HTTP handler (net/http, Gin, Echo, etc.).

Ecosystem
proxy.golang.org
Latest Release
v0.9.0
about 1 month ago
Versions
16
Links
Registry proxy.golang.org
Homepage Homepage
Docs Documentation
JSON API View JSON
CodeMeta codemeta.json
Package Details
PURL pkg:golang/rivaas.dev/binding
spec
License Apache-2.0
Namespace rivaas.dev
First Release 5 months ago
Last Synced 1 day ago
Repository
Stars 30 on GitHub
Forks 0 on GitHub
Rankings on proxy.golang.org
Overall Top 5.2%