mapstructurex

package
v0.0.0-...-e7df723 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDecoder

func NewDecoder(output any, opts ...Option) encodingx.Decoder

NewDecoder returns a new decoder.

func Unmarshal

func Unmarshal(input, output any, opts ...Option) error

Unmarshal takes an input structure and uses reflection to translate it to the output structure. output must be a pointer to a map or struct.

Types

type Option

type Option func(*options)

func DecodeHookFuncKinds

func DecodeHookFuncKinds(fs ...func(reflect.Kind, reflect.Kind, any) (any, error)) Option

DecodeHookFuncKinds is a DecodeHookFunc which knows only the Kinds of the source and target types.

func DecodeHookFuncTypes

func DecodeHookFuncTypes(fs ...func(reflect.Type, reflect.Type, any) (any, error)) Option

DecodeHookFuncTypes is a DecodeHookFunc which has complete information about the source and target types.

func DecodeHookFuncValues

func DecodeHookFuncValues(fs ...mapstructure.DecodeHookFuncValue) Option

DecodeHookFuncValues is a DecodeHookFunc which has complete access to both the source and target values.

func ErrorUnset

func ErrorUnset() Option

ErrorUnset If set to true, then it is an error for there to exist fields in the result that were not set in the decoding process (extra fields). This only applies to decoding to a struct. This will affect all nested structs as well.

func ErrorUnused

func ErrorUnused() Option

ErrorUnused If set to true, then it is an error for there to exist keys in the original map that were unused in the decoding process (extra keys).

func IgnoreUntaggedFields

func IgnoreUntaggedFields() Option

IgnoreUntaggedFields ignores all struct fields without explicit TagName, comparable to `mapstructure:"-"` as default behaviour.

func MatchName

func MatchName(f func(mapKey, fieldName string) bool) Option

MatchName is the function used to match the map key to the struct field name or tag. Defaults to `strings.EqualFold`. This can be used to implement case-sensitive tag values, support snake casing, etc.

func Metadata

func Metadata(md *mapstructure.Metadata) Option

Metadata is the struct that will contain extra metadata about the decoding. If this is nil, then no metadata will be tracked.

func RecursiveStructToMap

func RecursiveStructToMap() Option

func Squash

func Squash() Option

Squash will squash embedded structs. A squash tag may also be added to an individual struct field using a tag. For example:

type Parent struct {
    Child `mapstructure:",squash"`
}

func StringToIP

func StringToIP() Option

StringToIP converts strings to net.IP

func StringToIPNet

func StringToIPNet() Option

StringToIPNet converts strings to net.IPNet

func StringToSlice

func StringToSlice(separators ...string) Option

StringToSlice converts string to []string by splitting on the given sep.

func StringToTimeDuration

func StringToTimeDuration() Option

StringToTimeDuration converts strings to time.Duration.

func StringToTimeTime

func StringToTimeTime(layout string) Option

StringToTimeTime converts strings to time.Time.

func TagName

func TagName(name string) Option

TagName the tag name that mapstructure reads for field names. This defaults to "mapstructure"

func TextUnmarshaller

func TextUnmarshaller() Option

TextUnmarshaller that applies strings to the UnmarshalText function, when the target type implements the encoding.TextUnmarshaler interface

func WeaklyTyped

func WeaklyTyped() Option

func WeaklyTypedInput

func WeaklyTypedInput() Option

WeaklyTypedInput if set to true, the decoder will make the following "weak" conversions:

  • bools to string (true = "1", false = "0")
  • numbers to string (base 10)
  • bools to int/uint (true = 1, false = 0)
  • strings to int/uint (base implied by prefix)
  • int to bool (true if value != 0)
  • string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Anything else is an error)
  • empty array = empty map and vice versa
  • negative numbers to overflowed uint values (base 10)
  • slice of maps to a merged map
  • single values are converted to slices if required. Each element is weakly decoded. For example: "4" can become []int{4} if the target type is an int slice.

func ZeroFields

func ZeroFields() Option

ZeroFields if set to true, will zero fields before writing them. For example, a map will be emptied before decoded values are put in it. If this is false, a map will be merged.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL