mapstruct

package module
v0.0.0-...-f98774e Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package mapstruct exposes functionality to convert one arbitrary Go type into another, typically to convert a map[string]any 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, mapstruct will use the field name by default to perform the mapping. For example, if a struct has a field "Username" then mapstruct will look for a key in the source value of "username" (case insensitive).

type User struct {
    Username string
}

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

Renaming Fields

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

type User struct {
    Username string `mapstruct:"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 mapstruct:

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]any{
    "person": map[string]any{"name": "alice"},
}

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

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

Now the following input would be accepted:

map[string]any{
    "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]any{
    "name": "alice",
}

Config has a field that changes the behavior of mapstruct to always squash embedded structs.

Remainder Values

If there are any unmapped keys in the source value, mapstruct by default will silently ignore them. You can error by setting ErrorUnused in Config. 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]any" or "map[any]any". See example below:

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

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

map[string]any{
    "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 {
    Age int `mapstruct:",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]any{
    "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

mapstruct is highly configurable. See the Config struct for other features and options that are supported.

Index

Examples

Constants

This section is empty.

Variables

View Source
var TimeType = reflect.TypeOf((*time.Time)(nil)).Elem()

Functions

func Decode

func Decode(input any, output any, fns ...ConfigFn) 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.

Example
type Person struct {
	Name   string
	Age    int
	Emails []string
	Extra  map[string]string
}

// This input can come from anywhere, but typically comes from
// something like decoding JSON where we're not quite sure of the
// struct initially.
input := map[string]any{
	"name":   "Mitchell",
	"age":    91,
	"emails": []string{"one", "two", "three"},
	"extra": map[string]string{
		"twitter": "mitchellh",
	},
}

var result Person
err := Decode(input, &result)
if err != nil {
	panic(err)
}

fmt.Printf("%#v", result)
Output:

mapstruct.Person{Name:"Mitchell", Age:91, Emails:[]string{"one", "two", "three"}, Extra:map[string]string{"twitter":"mitchellh"}}
Example (EmbeddedStruct)
// Squashing multiple embedded structs is allowed using the squash tag.
// This is demonstrated by creating a composite struct of multiple types
// and decoding into it. In this case, a person can carry with it both
// a Family and a Location, as well as their own FirstName.
type Family struct {
	LastName string
}
type Location struct {
	City string
}
type Person struct {
	Family
	Location
	FirstName string
}

input := map[string]any{
	"FirstName": "Mitchell",
	"LastName":  "Hashimoto",
	"City":      "San Francisco",
}

var result Person
err := Decode(input, &result, WithSquash(true))
if err != nil {
	panic(err)
}

fmt.Printf("%s %s, %s", result.FirstName, result.LastName, result.City)
Output:

Mitchell Hashimoto, San Francisco
Example (Errors)
type Person struct {
	Name   string
	Age    int
	Emails []string
	Extra  map[string]string
}

// This input can come from anywhere, but typically comes from
// something like decoding JSON where we're not quite sure of the
// struct initially.
input := map[string]any{
	"name":   123,
	"age":    "bad value",
	"emails": []int{1, 2, 3},
}

var result Person
err := Decode(input, &result)
if err == nil {
	panic("should have an error")
}

fmt.Println(err.Error())
Output:

5 error(s) decoding:

* 'Age' expected type 'int', got unconvertible type 'string', value: 'bad value'
* 'Emails[0]' expected type 'string', got unconvertible type 'int', value: '1'
* 'Emails[1]' expected type 'string', got unconvertible type 'int', value: '2'
* 'Emails[2]' expected type 'string', got unconvertible type 'int', value: '3'
* 'Name' expected type 'string', got unconvertible type 'int', value: '123'
Example (Metadata)
type Person struct {
	Name string
	Age  int
}

// This input can come from anywhere, but typically comes from
// something like decoding JSON where we're not quite sure of the
// struct initially.
input := map[string]any{
	"name":  "Mitchell",
	"age":   91,
	"email": "foo@bar.com",
}

// For metadata, we make a more advanced Config so we can
// more finely configure the decoder that is used. In this case, we
// just tell the decoder we want to track metadata.
var md Metadata
var result Person
config := &Config{
	Metadata: &md,
	Result:   &result,
}

decoder, err := NewDecoder(config)
if err != nil {
	panic(err)
}

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

fmt.Printf("Unused keys: %#v", md.Unused)
Output:

Unused keys: []string{"email"}
Example (Omitempty)
// Add omitempty annotation to avoid map keys for empty values
type Family struct {
	LastName string
}
type Location struct {
	City string
}
type Person struct {
	*Family   `mapstruct:",omitempty"`
	*Location `mapstruct:",omitempty"`
	Age       int
	FirstName string
}

result := &map[string]any{}
input := Person{FirstName: "Somebody"}
err := Decode(input, &result)
if err != nil {
	panic(err)
}

fmt.Printf("%+v", result)
Output:

&map[Age:0 FirstName:Somebody]
Example (RemainingData)
// Note that the mapstruct tags defined in the struct type
// can indicate which fields the values are mapped to.
type Person struct {
	Name  string
	Age   int
	Other map[string]any `mapstruct:",remain"`
}

input := map[string]any{
	"name":  "Mitchell",
	"age":   91,
	"email": "mitchell@example.com",
}

var result Person
err := Decode(input, &result)
if err != nil {
	panic(err)
}

fmt.Printf("%#v", result)
Output:

mapstruct.Person{Name:"Mitchell", Age:91, Other:map[string]interface {}{"email":"mitchell@example.com"}}
Example (Tags)
// Note that the mapstruct tags defined in the struct type
// can indicate which fields the values are mapped to.
type Person struct {
	Name string `mapstruct:"person_name"`
	Age  int    `mapstruct:"person_age"`
}

input := map[string]any{
	"person_name": "Mitchell",
	"person_age":  91,
}

var result Person
err := Decode(input, &result)
if err != nil {
	panic(err)
}

fmt.Printf("%#v", result)
Output:

mapstruct.Person{Name:"Mitchell", Age:91}
Example (WeaklyTypedInput)
type Person struct {
	Name   string
	Age    int
	Emails []string
}

// This input can come from anywhere, but typically comes from
// something like decoding JSON, generated by a weakly typed language
// such as PHP.
input := map[string]any{
	"name":   123,              // number => string
	"age":    "42",             // string => number
	"emails": map[string]any{}, // empty map => empty array
}

var result Person
config := &Config{
	WeakType: true,
	Result:   &result,
}

decoder, err := NewDecoder(config)
if err != nil {
	panic(err)
}

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

fmt.Printf("%#v", result)
Output:

mapstruct.Person{Name:"123", Age:42, Emails:[]string{}}

func DecodeHookExec

func DecodeHookExec(raw HookFunc, from, to reflect.Value) (any, error)

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

func WeaklyTypedHook

func WeaklyTypedHook(
	f reflect.Kind,
	t reflect.Kind,
	data any,
) (any, error)

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

Note that this is significantly different from the WeakType option of the Config.

Types

type Config

type Config struct {
	// Hook, if set, will be called before any decoding and any
	// type conversion (if WeakType is on). This lets you modify
	// the values before they're set down onto the resulting struct. The
	// Hook 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.
	Hook HookFunc

	// 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

	// 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 WeakType 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.
	//
	WeakType 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 `mapstruct:",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 any

	// The tag name that mapstruct reads for field names. This
	// defaults to ["mapstruct", "field", "json", "yaml"]
	TagNames []string
}

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

type ConfigFn

type ConfigFn func(*Config)

func WithMetadata

func WithMetadata(v *Metadata) ConfigFn

func WithSquash

func WithSquash(v bool) ConfigFn

func WithTagNames

func WithTagNames(v ...string) ConfigFn

func WithWeakType

func WithWeakType(v bool) ConfigFn

type Decoder

type Decoder struct {
	*Config
}

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 Config structure. The top-level Decode method is just a convenience that sets up the most basic Decoder.

func NewDecoder

func NewDecoder(config *Config) (*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 any) error

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

type Error

type Error struct {
	Errors []string
}

Error implements the error interface and can represents multiple errors that occur in the course of a single decode.

func (*Error) Error

func (e *Error) Error() string

func (*Error) WrappedErrors

func (e *Error) WrappedErrors() []error

WrappedErrors implements the errwrap.Wrapper interface to make this return value more useful with the errwrap and go-multierror libraries.

type HookFunc

type HookFunc any

HookFunc is the callback function that can be used for data transformations. See "Hook" in the Config struct.

The type must be one of HookFuncType, HookFuncKind, or HookFuncValue. 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 HookFunc 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 ...HookFunc) HookFunc

ComposeDecodeHookFunc creates a single HookFunc that automatically composes multiple DecodeHookFuncs.

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

func RecursiveStructToMapHookFunc

func RecursiveStructToMapHookFunc() HookFunc

func StringToIPHookFunc

func StringToIPHookFunc() HookFunc

StringToIPHookFunc returns a HookFunc that converts strings to net.IP

func StringToIPNetHookFunc

func StringToIPNetHookFunc() HookFunc

StringToIPNetHookFunc returns a HookFunc that converts strings to net.IPNet

func StringToSliceHookFunc

func StringToSliceHookFunc(sep string) HookFunc

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

func StringToTimeDurationHookFunc

func StringToTimeDurationHookFunc() HookFunc

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

func StringToTimeHookFunc

func StringToTimeHookFunc(layout string) HookFunc

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

type HookFuncKind

type HookFuncKind func(from, to reflect.Kind, fromValue any) (any, error)

HookFuncKind is a HookFunc which knows only the Kinds of the source and target types.

type HookFuncType

type HookFuncType func(from, to reflect.Type, fromValue any) (any, error)

HookFuncType is a HookFunc which has complete information about the source and target types.

func TextUnmarshallerHookFunc

func TextUnmarshallerHookFunc() HookFuncType

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

type HookFuncValue

type HookFuncValue func(from, to reflect.Value) (any, error)

DecodeHookFuncRaw is a HookFunc which has complete access to both the source and target values.

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