mapstructurex

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: MIT Imports: 2 Imported by: 0

README

Extensions for mapstructure

GitHub Workflow Status go.dev reference Go Version built with nix

Installation

go get github.com/sagikazarmark/mapstructurex

Development

For an optimal developer experience, it is recommended to install Nix and direnv.

Run the test suite:

just test

License

The project is licensed under the MIT License.

Documentation

Overview

Package mapstructurex contains various extensions (primarily decoding hooks) to mapstructure.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapDecoderHookFunc

func MapDecoderHookFunc() mapstructure.DecodeHookFunc

MapDecoderHookFunc returns a mapstructure.DecodeHookFunc that applies maps to the DecodeMap function, when the target type implements the MapDecoder interface.

Example
var person Person

decoder, _ := CreateDecoderWithMapDecoderHook(&person)

input := map[string]any{
	"name": "Bob",
	"age":  42,
}

_ = decoder.Decode(input)

fmt.Println(person)
Output:

Bob: 42
Example (Polymorphic)
var config Config

decoder, _ := CreateDecoderWithMapDecoderHook(&config)

input := map[string]any{
	"type": "baz",
	"config": map[string]any{
		"baz": "bat",
	},
}

err := decoder.Decode(input)
if err != nil {
	panic(err)
}

fmt.Println(config.Foo())
Output:

bat

Types

type MapDecoder

type MapDecoder interface {
	DecodeMap(map[string]any) error
}

MapDecoder is the interface implemented by an object that can decode a raw map representation of itself.

Jump to

Keyboard shortcuts

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