Documentation
¶
Overview ¶
Package structx converts between Go structs and map[string]any.
Every function takes the struct tag used for field names as its first argument (e.g. "json", "yaml", "toml"). Field names fall back to the Go field name when no such tag is present, and fields tagged with `tag:"-"` are skipped.
Usage:
m, err := structx.ToMap("json", myStruct)
result, err := structx.FromMap("yaml", m, reflect.TypeOf(MyStruct{}))
Index ¶
- Variables
- func FromMap(fieldNameTag string, data map[string]any, target reflect.Type) (any, error)
- func Marshal(fieldNameTag string, input any) map[string]any
- func ToMap(fieldNameTag string, input any) (map[string]any, error)
- func Unmarshal(fieldNameTag string, data map[string]any, typ reflect.Type) (any, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidStructInput indicates the input is not a struct or is nil. ErrInvalidStructInput = errors.New("input is not a struct or is nil") // ErrTargetTypeMustBeStruct indicates the target type is not a struct. ErrTargetTypeMustBeStruct = errors.New("target type must be struct") )
Sentinel errors for structx operations.
Functions ¶
func FromMap ¶
FromMap converts map[string]any to a struct of the given type using the named field-name tag. It returns ErrTargetTypeMustBeStruct if target is not a struct type. Fields are matched by tag name, falling back to the Go field name.
func Marshal ¶
Marshal converts a struct to map[string]any using the named field-name tag for field names. Marshal returns nil if input is nil, a nil pointer, or not a struct.
func ToMap ¶
ToMap converts a struct to map[string]any using the named field-name tag (e.g. "json", "yaml", "toml") for field names. It returns ErrInvalidStructInput if input is nil, a nil pointer, or not a struct type.
func Unmarshal ¶
Unmarshal converts map[string]any to a struct of the given type using the named field-name tag. It returns ErrTargetTypeMustBeStruct if typ is not a struct type. Fields are matched by tag name, falling back to the Go field name.
Types ¶
This section is empty.