Documentation
¶
Overview ¶
Package structx converts between Go structs and map[string]any.
Field names are derived from json struct tags, falling back to the Go field name when no tag is present. Fields tagged with json:"-" are skipped.
Usage:
m, err := structx.ToMap(myStruct)
result, err := structx.FromMap(m, reflect.TypeOf(MyStruct{}))
Index ¶
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. It returns ErrTargetTypeMustBeStruct if target is not a struct type. Fields are matched by json tag name, falling back to the Go field name.
func Marshal ¶
Marshal converts a struct to map[string]any using json tag 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 json tag field names. It returns ErrInvalidStructInput if input is nil, a nil pointer, or not a struct type.
Types ¶
This section is empty.