mapstructure

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

mapstructure

GitHub Workflow Status go.dev reference Go Version

mapstructure is a Go library for decoding generic map values to structures and vice versa, while providing helpful error handling.

This library is most useful when decoding values from some data stream (JSON, Gob, etc.) where you don't quite know the structure of the underlying data until you read a part of it. You can therefore read a map[string]interface{} and use this library to decode it into the proper underlying native Go structure.

Installation

go get github.com/zhangdapeng520/zdpgo_viper/mapstructure

Migrating from github.com/mitchellh/mapstructure

@mitchehllh announced his intent to archive some of his unmaintained projects (see here and here). This is a repository achieved the "blessed fork" status.

You can migrate to this package by changing your import paths in your Go files to github.com/zhangdapeng520/zdpgo_viper/mapstructure. The API is the same, so you don't need to change anything else.

Here is a script that can help you with the migration:

sed -i 's/github.com\/mitchellh\/mapstructure/github.com\/go-viper\/mapstructure\/v2/g' $(find . -type f -name '*.go')

If you need more time to migrate your code, that is absolutely fine.

Some of the latest fixes are backported to the v1 release branch of this package, so you can use the Go modules replace feature until you are ready to migrate:

replace github.com/mitchellh/mapstructure => github.com/go-viper/mapstructure v1.6.0

Usage & Example

For usage and examples see the documentation.

The Decode function has examples associated with it there.

But Why?!

Go offers fantastic standard libraries for decoding formats such as JSON. The standard method is to have a struct pre-created, and populate that struct from the bytes of the encoded format. This is great, but the problem is if you have configuration or an encoding that changes slightly depending on specific fields. For example, consider this JSON:

{
  "type": "person",
  "name": "Mitchell"
}

Perhaps we can't populate a specific structure without first reading the "type" field from the JSON. We could always do two passes over the decoding of the JSON (reading the "type" first, and the rest later). However, it is much simpler to just decode this into a map[string]interface{} structure, read the "type" key, then use something like this library to decode it into the proper structure.

Credits

Mapstructure was originally created by @mitchellh. This is a maintained fork of the original library.

Read more about the reasons for the fork here.

License

The project is licensed under the MIT License.

Documentation

Overview

Package mapstructure exposes functionality to convert one arbitrary Go type into another, typically to convert a map[string]interface{} into a native Go structure.

The Go structure can be arbitrarily complex, containing slices, other structs, etc. and the decoder will properly decode nested maps and so on into the proper structures in the native Go struct. See the examples to see what the decoder is capable of.

The simplest function to start with is Decode.

Field Tags

When decoding to a struct, mapstructure will use the field name by default to perform the mapping. For example, if a struct has a field "Username" then mapstructure will look for a key in the source value of "username" (case insensitive).

type User struct {
    Username string
}

You can change the behavior of mapstructure by using struct tags. The default struct tag that mapstructure looks for is "mapstructure" but you can customize it using DecoderConfig.

Renaming Fields

To rename the key that mapstructure looks for, use the "mapstructure" tag and set a value directly. For example, to change the "username" example above to "user":

type User struct {
    Username string `mapstructure:"user"`
}

Embedded Structs and Squashing

Embedded structs are treated as if they're another field with that name. By default, the two structs below are equivalent when decoding with mapstructure:

type Person struct {
    Name string
}

type Friend struct {
    Person
}

type Friend struct {
    Person Person
}

This would require an input that looks like below:

map[string]interface{}{
    "person": map[string]interface{}{"name": "alice"},
}

If your "person" value is NOT nested, then you can append ",squash" to your tag value and mapstructure will treat it as if the embedded struct were part of the struct directly. Example:

type Friend struct {
    Person `mapstructure:",squash"`
}

Now the following input would be accepted:

map[string]interface{}{
    "name": "alice",
}

When decoding from a struct to a map, the squash tag squashes the struct fields into a single map. Using the example structs from above:

Friend{Person: Person{Name: "alice"}}

Will be decoded into a map:

map[string]interface{}{
    "name": "alice",
}

DecoderConfig has a field that changes the behavior of mapstructure to always squash embedded structs.

Remainder Values

If there are any unmapped keys in the source value, mapstructure by default will silently ignore them. You can error by setting ErrorUnused in DecoderConfig. If you're using Metadata you can also maintain a slice of the unused keys.

You can also use the ",remain" suffix on your tag to collect all unused values in a map. The field with this tag MUST be a map type and should probably be a "map[string]interface{}" or "map[interface{}]interface{}". See example below:

type Friend struct {
    Name  string
    Other map[string]interface{} `mapstructure:",remain"`
}

Given the input below, Other would be populated with the other values that weren't used (everything but "name"):

map[string]interface{}{
    "name":    "bob",
    "address": "123 Maple St.",
}

Omit Empty Values

When decoding from a struct to any other value, you may use the ",omitempty" suffix on your tag to omit that value if it equates to the zero value. The zero value of all types is specified in the Go specification.

For example, the zero type of a numeric type is zero ("0"). If the struct field value is zero and a numeric type, the field is empty, and it won't be encoded into the destination type.

type Source struct {
    Age int `mapstructure:",omitempty"`
}

Unexported fields

Since unexported (private) struct fields cannot be set outside the package where they are defined, the decoder will simply skip them.

For this output type definition:

type Exported struct {
    private string // this unexported field will be skipped
    Public string
}

Using this map as input:

map[string]interface{}{
    "private": "I will be ignored",
    "Public":  "I made it through!",
}

The following struct will be decoded:

type Exported struct {
    private: "" // field is left with an empty string (zero value)
    Public: "I made it through!"
}

Other Configuration

mapstructure is highly configurable. See the DecoderConfig struct for other features and options that are supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(input interface{}, output interface{}) error

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

func DecodeHookExec

func DecodeHookExec(
	raw DecodeHookFunc,
	from reflect.Value, to reflect.Value,
) (interface{}, error)

DecodeHookExec executes the given decode hook. This should be used since it'll naturally degrade to the older backwards compatible DecodeHookFunc that took reflect.Kind instead of reflect.Type.

func DecodeMetadata

func DecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error

DecodeMetadata is the same as Decode, but is shorthand to enable metadata collection. See DecoderConfig for more info.

func WeakDecode

func WeakDecode(input, output interface{}) error

WeakDecode is the same as Decode but is shorthand to enable WeaklyTypedInput. See DecoderConfig for more info.

func WeakDecodeMetadata

func WeakDecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error

WeakDecodeMetadata is the same as Decode, but is shorthand to enable both WeaklyTypedInput and metadata collection. See DecoderConfig for more info.

func WeaklyTypedHook

func WeaklyTypedHook(
	f reflect.Kind,
	t reflect.Kind,
	data interface{},
) (interface{}, error)

WeaklyTypedHook is a DecodeHookFunc which adds support for weak typing to the decoder.

Note that this is significantly different from the WeaklyTypedInput option of the DecoderConfig.

Types

type DecodeHookFunc

type DecodeHookFunc interface{}

DecodeHookFunc is the callback function that can be used for data transformations. See "DecodeHook" in the DecoderConfig struct.

The type must be one of DecodeHookFuncType, DecodeHookFuncKind, or DecodeHookFuncValue. Values are a superset of Types (Values can return types), and Types are a superset of Kinds (Types can return Kinds) and are generally a richer thing to use, but Kinds are simpler if you only need those.

The reason DecodeHookFunc is multi-typed is for backwards compatibility: we started with Kinds and then realized Types were the better solution, but have a promise to not break backwards compat so we now support both.

func ComposeDecodeHookFunc

func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc

ComposeDecodeHookFunc creates a single DecodeHookFunc that automatically composes multiple DecodeHookFuncs.

The composed funcs are called in order, with the result of the previous transformation.

func OrComposeDecodeHookFunc

func OrComposeDecodeHookFunc(ff ...DecodeHookFunc) DecodeHookFunc

OrComposeDecodeHookFunc executes all input hook functions until one of them returns no error. In that case its value is returned. If all hooks return an error, OrComposeDecodeHookFunc returns an error concatenating all error messages.

func RecursiveStructToMapHookFunc

func RecursiveStructToMapHookFunc() DecodeHookFunc

func StringToBasicTypeHookFunc

func StringToBasicTypeHookFunc() DecodeHookFunc

StringToBasicTypeHookFunc returns a DecodeHookFunc that converts strings to basic types. int8, uint8, int16, uint16, int32, uint32, int64, uint64, int, uint, float32, float64, bool, byte, rune, complex64, complex128

func StringToBoolHookFunc

func StringToBoolHookFunc() DecodeHookFunc

StringToBoolHookFunc returns a DecodeHookFunc that converts strings to bool.

func StringToByteHookFunc

func StringToByteHookFunc() DecodeHookFunc

StringToByteHookFunc returns a DecodeHookFunc that converts strings to byte.

func StringToComplex128HookFunc

func StringToComplex128HookFunc() DecodeHookFunc

StringToComplex128HookFunc returns a DecodeHookFunc that converts strings to complex128.

func StringToComplex64HookFunc

func StringToComplex64HookFunc() DecodeHookFunc

StringToComplex64HookFunc returns a DecodeHookFunc that converts strings to complex64.

func StringToFloat32HookFunc

func StringToFloat32HookFunc() DecodeHookFunc

StringToFloat32HookFunc returns a DecodeHookFunc that converts strings to float32.

func StringToFloat64HookFunc

func StringToFloat64HookFunc() DecodeHookFunc

StringToFloat64HookFunc returns a DecodeHookFunc that converts strings to float64.

func StringToIPHookFunc

func StringToIPHookFunc() DecodeHookFunc

StringToIPHookFunc returns a DecodeHookFunc that converts strings to net.IP

func StringToIPNetHookFunc

func StringToIPNetHookFunc() DecodeHookFunc

StringToIPNetHookFunc returns a DecodeHookFunc that converts strings to net.IPNet

func StringToInt16HookFunc

func StringToInt16HookFunc() DecodeHookFunc

StringToInt16HookFunc returns a DecodeHookFunc that converts strings to int16.

func StringToInt32HookFunc

func StringToInt32HookFunc() DecodeHookFunc

StringToInt32HookFunc returns a DecodeHookFunc that converts strings to int32.

func StringToInt64HookFunc

func StringToInt64HookFunc() DecodeHookFunc

StringToInt64HookFunc returns a DecodeHookFunc that converts strings to int64.

func StringToInt8HookFunc

func StringToInt8HookFunc() DecodeHookFunc

StringToInt8HookFunc returns a DecodeHookFunc that converts strings to int8.

func StringToIntHookFunc

func StringToIntHookFunc() DecodeHookFunc

StringToIntHookFunc returns a DecodeHookFunc that converts strings to int.

func StringToNetIPAddrHookFunc

func StringToNetIPAddrHookFunc() DecodeHookFunc

StringToNetIPAddrHookFunc returns a DecodeHookFunc that converts strings to netip.Addr.

func StringToNetIPAddrPortHookFunc

func StringToNetIPAddrPortHookFunc() DecodeHookFunc

StringToNetIPAddrPortHookFunc returns a DecodeHookFunc that converts strings to netip.AddrPort.

func StringToRuneHookFunc

func StringToRuneHookFunc() DecodeHookFunc

StringToRuneHookFunc returns a DecodeHookFunc that converts strings to rune.

func StringToSliceHookFunc

func StringToSliceHookFunc(sep string) DecodeHookFunc

StringToSliceHookFunc returns a DecodeHookFunc that converts string to []string by splitting on the given sep.

func StringToTimeDurationHookFunc

func StringToTimeDurationHookFunc() DecodeHookFunc

StringToTimeDurationHookFunc returns a DecodeHookFunc that converts strings to time.Duration.

func StringToTimeHookFunc

func StringToTimeHookFunc(layout string) DecodeHookFunc

StringToTimeHookFunc returns a DecodeHookFunc that converts strings to time.Time.

func StringToUint16HookFunc

func StringToUint16HookFunc() DecodeHookFunc

StringToUint16HookFunc returns a DecodeHookFunc that converts strings to uint16.

func StringToUint32HookFunc

func StringToUint32HookFunc() DecodeHookFunc

StringToUint32HookFunc returns a DecodeHookFunc that converts strings to uint32.

func StringToUint64HookFunc

func StringToUint64HookFunc() DecodeHookFunc

StringToUint64HookFunc returns a DecodeHookFunc that converts strings to uint64.

func StringToUint8HookFunc

func StringToUint8HookFunc() DecodeHookFunc

StringToUint8HookFunc returns a DecodeHookFunc that converts strings to uint8.

func StringToUintHookFunc

func StringToUintHookFunc() DecodeHookFunc

StringToUintHookFunc returns a DecodeHookFunc that converts strings to uint.

type DecodeHookFuncKind

type DecodeHookFuncKind func(reflect.Kind, reflect.Kind, interface{}) (interface{}, error)

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

type DecodeHookFuncType

type DecodeHookFuncType func(reflect.Type, reflect.Type, interface{}) (interface{}, error)

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

func TextUnmarshallerHookFunc

func TextUnmarshallerHookFunc() DecodeHookFuncType

TextUnmarshallerHookFunc returns a DecodeHookFunc that applies strings to the UnmarshalText function, when the target type implements the encoding.TextUnmarshaler interface

type DecodeHookFuncValue

type DecodeHookFuncValue func(from reflect.Value, to reflect.Value) (interface{}, error)

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

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

A Decoder takes a raw interface value and turns it into structured data, keeping track of rich error information along the way in case anything goes wrong. Unlike the basic top-level Decode method, you can more finely control how the Decoder behaves using the DecoderConfig structure. The top-level Decode method is just a convenience that sets up the most basic Decoder.

func NewDecoder

func NewDecoder(config *DecoderConfig) (*Decoder, error)

NewDecoder returns a new decoder for the given configuration. Once a decoder has been returned, the same configuration must not be used again.

func (*Decoder) Decode

func (d *Decoder) Decode(input interface{}) error

Decode decodes the given raw interface to the target pointer specified by the configuration.

type DecoderConfig

type DecoderConfig struct {
	// DecodeHook, if set, will be called before any decoding and any
	// type conversion (if WeaklyTypedInput is on). This lets you modify
	// the values before they're set down onto the resulting struct. The
	// DecodeHook is called for every map and value in the input. This means
	// that if a struct has embedded fields with squash tags the decode hook
	// is called only once with all of the input data, not once for each
	// embedded struct.
	//
	// If an error is returned, the entire decode will fail with that error.
	DecodeHook DecodeHookFunc

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

	// If ErrorUnset is 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.
	ErrorUnset bool

	// 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.
	ZeroFields bool

	// If WeaklyTypedInput is 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.
	//
	WeaklyTypedInput bool

	// 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"`
	//  }
	Squash bool

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

	// Result is a pointer to the struct that will contain the decoded
	// value.
	Result interface{}

	// The tag name that mapstructure reads for field names. This
	// defaults to "mapstructure"
	TagName string

	// The option of the value in the tag that indicates a field should
	// be squashed. This defaults to "squash".
	SquashTagOption string

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

	// 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.
	MatchName func(mapKey, fieldName string) bool
}

DecoderConfig is the configuration that is used to create a new decoder and allows customization of various aspects of decoding.

type Metadata

type Metadata struct {
	// Keys are the keys of the structure which were successfully decoded
	Keys []string

	// Unused is a slice of keys that were found in the raw value but
	// weren't decoded since there was no matching field in the result interface
	Unused []string

	// Unset is a slice of field names that were found in the result interface
	// but weren't set in the decoding process since there was no matching value
	// in the input
	Unset []string
}

Metadata contains information about decoding a structure that is tedious or difficult to get otherwise.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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