mergo: github.com/imdario/mergo Index | Files

package mergo

import "github.com/imdario/mergo"

Package mergo merges same-type structs and maps by setting default values in zero-value fields.

Mergo won't merge unexported (private) fields but will do recursively any exported one. It also won't merge structs inside maps (because they are not addressable using Go reflection).

Usage

From my own work-in-progress project:

type networkConfig struct {
	Protocol string
	Address string
	ServerType string `json: "server_type"`
	Port uint16
}

type FssnConfig struct {
	Network networkConfig
}

var fssnDefault = FssnConfig {
	networkConfig {
		"tcp",
		"127.0.0.1",
		"http",
		31560,
	},
}

// Inside a function [...]

if err := mergo.Merge(&config, fssnDefault); err != nil {
	log.Fatal(err)
}

// More code [...]

Index

Package Files

doc.go map.go merge.go mergo.go

Variables

var (
    ErrNilArguments                = errors.New("src and dst must not be nil")
    ErrDifferentArgumentsTypes     = errors.New("src and dst must be of same type")
    ErrNotSupported                = errors.New("only structs and maps are supported")
    ErrExpectedMapAsDestination    = errors.New("dst was expected to be a map")
    ErrExpectedStructAsDestination = errors.New("dst was expected to be a struct")
)

Errors reported by Mergo when it finds invalid arguments.

func Map Uses

func Map(dst, src interface{}, opts ...func(*Config)) error

Map sets fields' values in dst from src. src can be a map with string keys or a struct. dst must be the opposite: if src is a map, dst must be a valid pointer to struct. If src is a struct, dst must be map[string]interface{}. It won't merge unexported (private) fields and will do recursively any exported field. If dst is a map, keys will be src fields' names in lower camel case. Missing key in src that doesn't match a field in dst will be skipped. This doesn't apply if dst is a map. This is separated method from Merge because it is cleaner and it keeps sane semantics: merging equal types, mapping different (restricted) types.

func MapWithOverwrite Uses

func MapWithOverwrite(dst, src interface{}, opts ...func(*Config)) error

MapWithOverwrite will do the same as Map except that non-empty dst attributes will be overridden by non-empty src attribute values. Deprecated: Use Map(…) with WithOverride

func Merge Uses

func Merge(dst, src interface{}, opts ...func(*Config)) error

Merge will fill any empty for value type attributes on the dst struct using corresponding src attributes if they themselves are not empty. dst and src must be valid same-type structs and dst must be a pointer to struct. It won't merge unexported (private) fields and will do recursively any exported field.

func MergeWithOverwrite Uses

func MergeWithOverwrite(dst, src interface{}, opts ...func(*Config)) error

MergeWithOverwrite will do the same as Merge except that non-empty dst attributes will be overriden by non-empty src attribute values. Deprecated: use Merge(…) with WithOverride

func WithAppendSlice Uses

func WithAppendSlice(config *Config)

WithAppendSlice will make merge append slices instead of overwriting it

func WithOverride Uses

func WithOverride(config *Config)

WithOverride will make merge override non-empty dst attributes with non-empty src attributes values.

func WithTransformers Uses

func WithTransformers(transformers Transformers) func(*Config)

WithTransformers adds transformers to merge, allowing to customize the merging of some types.

type Config Uses

type Config struct {
    Overwrite    bool
    AppendSlice  bool
    Transformers Transformers
    // contains filtered or unexported fields
}

type Transformers Uses

type Transformers interface {
    Transformer(reflect.Type) func(dst, src reflect.Value) error
}

Package mergo imports 5 packages (graph) and is imported by 627 packages. Updated 2019-01-23. Refresh now. Tools for package owners.