datas

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodePath

func DecodePath(m map[string]interface{}, rawVal interface{}) error

DecodePath takes a map and uses reflection to convert it into the given Go native structure. Tags are used to specify the mapping between fields in the map and structure

func DecodeSlicePath

func DecodeSlicePath(ms []map[string]interface{}, rawSlice interface{}) error

DecodeSlicePath decodes a slice of maps against a slice of structures that contain specified tags

func Map2Struct

func Map2Struct(m interface{}, rawVal interface{}) error

Map2Struct takes a map and uses reflection to convert it into the given Go native structure. val must be a pointer to a struct.

func Struct2Map

func Struct2Map(content interface{}) (map[string]interface{}, error)

Types

type DecodeHookFunc

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

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 NewPathDecoder

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

NewPathDecoder returns a new decoder for the given configuration. This is used to decode path specific structures

func (*Decoder) Decode

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

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

func (*Decoder) DecodePath

func (d *Decoder) DecodePath(m map[string]interface{}, rawVal interface{}) (bool, error)

DecodePath decodes the raw interface against the map based on the specified tags

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.
	//
	// 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 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
	//
	WeaklyTypedInput 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
}

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

type Error

type Error struct {
	Errors []string
}

func (*Error) Error

func (e *Error) Error() string

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
}

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

Jump to

Keyboard shortcuts

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