Documentation
¶
Overview ¶
Package xstructs provides functionality for working with structs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToMap ¶
func ToMap(obj any, opts ...ToMapOption) (map[string]any, error)
ToMap converts a struct or map to a map[string]any. It handles nested structs, maps, and slices. By default, it uses the "json" and "yaml" tags to determine the key names in that order. It respects the `omitempty` tag for fields. It respects the `inline` tag for nested structs. It respects the `-` tag to omit fields.
If the input is nil, it returns nil. If the input is not a struct or map, it returns an error.
Types ¶
type ToMapOption ¶ added in v0.2.0
type ToMapOption func(*handler)
ToMapOption is a function that modifies the handler.
func WithAllowNoTags ¶ added in v0.2.0
func WithAllowNoTags() ToMapOption
WithAllowNoTags allows you to specify whether to allow fields without tags. If used, fields without tags will be included in the output map.
func WithTags ¶ added in v0.2.0
func WithTags(tags ...string) ToMapOption
WithTags allows you to specify custom tag categories to check for. It can be used to override the default "json" and "yaml" tags. The tags are checked in the order they are provided.