Documentation
¶
Overview ¶
Package databridge provides flexible input-to-struct transformation helpers.
It detects and parses JSON, URL-encoded forms (with dotted keys => nested objects), CSV (header row), and optionally YAML. It then maps incoming keys to your target struct's JSON tags, with case-insensitive, non-alphanumeric-agnostic matching by default, and performs type-aware coercion so values like "30" or "true" decode into int/bool fields naturally. Strict mode can be enabled to reject unknown fields.
Primary APIs:
- TransformToStructUniversal(input, &out, options...)
- Transform[T any](input, options...) (T, error)
- TransformToJSON(input, &out, options...) ([]byte, error)
Example:
type Person struct {
FirstName string `json:"First_Name"`
Age int64 `json:"Age"`
Active bool `json:"Active"`
Address struct { City string `json:"city"` } `json:"address"`
}
p, err := databridge.Transform[Person](`{"first-name":"Ada","Age":"30","active":"true","address":{"City":"Paris"}}`)
if err != nil { /* handle */ }
Package databridge provides data transformation utilities. This placeholder file exists to ensure the package compiles cleanly.
Code generated by databridge-gen; DO NOT EDIT.
Index ¶
- Variables
- func FromJSON[T any](b []byte, opts ...Option) (T, error)
- func FromJSONString[T any](s string, opts ...Option) (T, error)
- func Transform[T any](input interface{}, opts ...Option) (T, error)
- func TransformToJSON(input interface{}, outputPtr interface{}, opts ...Option) ([]byte, error)
- func TransformToStructUniversal(input interface{}, output interface{}, opts ...Option) error
- type Option
- type Order
- type User
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnsupportedInput = errors.New("databridge: unsupported input type") ErrDecodeFailed = errors.New("databridge: failed to decode input into target") )
Functions ¶
func FromJSON ¶
FromJSON decodes JSON bytes into T using the fastest path (no key normalization), honoring Strict mode if provided via options. Use when your payload keys already match your struct json tags.
func FromJSONString ¶
FromJSONString is a convenience wrapper over FromJSON.
func Transform ¶
Transform is a generic convenience wrapper that returns a value of type T. Example: user := databridge.Transform[User](formOrJSON)
Example ¶
package main
import (
"fmt"
databridge "github.com/dataBridgeGoPkg/dataBridge"
)
type Person struct {
FirstName string `json:"First_Name"`
Age int64 `json:"Age"`
Active bool `json:"Active"`
Address struct {
City string `json:"city"`
} `json:"address"`
}
func main() {
in := `{"first-name":"John","Age":"30","active":"true","address":{"City":"Paris"}}`
p, _ := databridge.Transform[Person](in)
fmt.Println(p.Address.City)
}
Output: Paris
func TransformToJSON ¶
TransformToJSON marshals the decoded struct into JSON bytes. outputPtr should be a pointer to the desired struct or slice type; if nil, a generic map will be produced.
func TransformToStructUniversal ¶
TransformToStructUniversal attempts to decode `input` into `output`. output must be a non-nil pointer to a struct OR pointer to a slice (e.g. *[]T).
Supported input types:
- string / []byte / io.Reader / *bytes.Buffer : guessed as JSON, form, YAML, XML, CSV (CSV if looks like comma-separated lines + header)
- url.Values (form)
- map[string]interface{}
Options:
WithYAML(true) - enable YAML parsing for strings/bytes WithStrict(true) - use DisallowUnknownFields on JSON decode WithKeyNormalizer(fn) - override default key normalization
Behavior highlights:
- Forms with dotted keys (e.g., address.city) produce nested maps.
- If output is slice type, CSV or multi-row input will map to slice elements.
- If output is a struct and CSV contains multiple rows, the first row is used.
Example ¶
package main
import (
"fmt"
"net/url"
databridge "github.com/dataBridgeGoPkg/dataBridge"
)
type Person struct {
FirstName string `json:"First_Name"`
Age int64 `json:"Age"`
Active bool `json:"Active"`
Address struct {
City string `json:"city"`
} `json:"address"`
}
func main() {
f := url.Values{"First_Name": {"John"}, "Age": {"30"}, "Active": {"true"}, "address.city": {"Lyon"}}
var p Person
_ = databridge.TransformToStructUniversal(f, &p)
fmt.Println(p.Address.City)
}
Output: Lyon
Types ¶
type Option ¶
type Option func(*config)
func WithKeyNormalization ¶
func WithKeyNormalizer ¶
func WithLogger ¶
func WithNumberConversion ¶
func WithStrict ¶
type Order ¶
type User ¶
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Active bool `json:"active"`
Tags []string `json:"tags"`
CreatedAt time.Time `json:"created_at"`
Address struct {
Line1 string `json:"line1"`
City string `json:"city"`
Zip string `json:"zip"`
} `json:"address"`
}
Sample domain structs for generator demo.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
databridge-gen
command
Command databridge-gen generates reflection-free binders for url.Values (forms) into your struct types.
|
Command databridge-gen generates reflection-free binders for url.Values (forms) into your struct types. |