2019-01-18 01:50:10 +00:00
|
|
|
// Copyright 2013 Dario Castañé. All rights reserved.
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Based on src/pkg/reflect/deepequal.go from official
|
|
|
|
// golang's stdlib.
|
|
|
|
|
|
|
|
package mergo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2022-04-18 17:00:37 +00:00
|
|
|
func hasMergeableFields(dst reflect.Value) (exported bool) {
|
2019-01-18 01:50:10 +00:00
|
|
|
for i, n := 0, dst.NumField(); i < n; i++ {
|
|
|
|
field := dst.Type().Field(i)
|
|
|
|
if field.Anonymous && dst.Field(i).Kind() == reflect.Struct {
|
2022-04-18 17:00:37 +00:00
|
|
|
exported = exported || hasMergeableFields(dst.Field(i))
|
|
|
|
} else if isExportedComponent(&field) {
|
2019-01-18 01:50:10 +00:00
|
|
|
exported = exported || len(field.PkgPath) == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-18 17:00:37 +00:00
|
|
|
func isExportedComponent(field *reflect.StructField) bool {
|
|
|
|
pkgPath := field.PkgPath
|
|
|
|
if len(pkgPath) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c := field.Name[0]
|
|
|
|
if 'a' <= c && c <= 'z' || c == '_' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
type Config struct {
|
2022-04-18 17:00:37 +00:00
|
|
|
Overwrite bool
|
|
|
|
AppendSlice bool
|
|
|
|
TypeCheck bool
|
|
|
|
Transformers Transformers
|
|
|
|
overwriteWithEmptyValue bool
|
|
|
|
overwriteSliceWithEmptyValue bool
|
|
|
|
sliceDeepCopy bool
|
|
|
|
debug bool
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Transformers interface {
|
|
|
|
Transformer(reflect.Type) func(dst, src reflect.Value) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverses recursively both values, assigning src's fields values to dst.
|
|
|
|
// The map argument tracks comparisons that have already been seen, which allows
|
|
|
|
// short circuiting on recursive types.
|
|
|
|
func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) {
|
|
|
|
overwrite := config.Overwrite
|
2022-04-18 17:00:37 +00:00
|
|
|
typeCheck := config.TypeCheck
|
|
|
|
overwriteWithEmptySrc := config.overwriteWithEmptyValue
|
|
|
|
overwriteSliceWithEmptySrc := config.overwriteSliceWithEmptyValue
|
|
|
|
sliceDeepCopy := config.sliceDeepCopy
|
2019-01-18 01:50:10 +00:00
|
|
|
|
|
|
|
if !src.IsValid() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if dst.CanAddr() {
|
|
|
|
addr := dst.UnsafeAddr()
|
|
|
|
h := 17 * addr
|
|
|
|
seen := visited[h]
|
|
|
|
typ := dst.Type()
|
|
|
|
for p := seen; p != nil; p = p.next {
|
|
|
|
if p.ptr == addr && p.typ == typ {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remember, remember...
|
|
|
|
visited[h] = &visit{addr, typ, seen}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Transformers != nil && !isEmptyValue(dst) {
|
|
|
|
if fn := config.Transformers.Transformer(dst.Type()); fn != nil {
|
|
|
|
err = fn(dst, src)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch dst.Kind() {
|
|
|
|
case reflect.Struct:
|
2022-04-18 17:00:37 +00:00
|
|
|
if hasMergeableFields(dst) {
|
2019-01-18 01:50:10 +00:00
|
|
|
for i, n := 0, dst.NumField(); i < n; i++ {
|
|
|
|
if err = deepMerge(dst.Field(i), src.Field(i), visited, depth+1, config); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-18 17:00:37 +00:00
|
|
|
if (isReflectNil(dst) || overwrite) && (!isEmptyValue(src) || overwriteWithEmptySrc) {
|
2019-01-18 01:50:10 +00:00
|
|
|
dst.Set(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
if dst.IsNil() && !src.IsNil() {
|
|
|
|
dst.Set(reflect.MakeMap(dst.Type()))
|
|
|
|
}
|
2022-04-18 17:00:37 +00:00
|
|
|
|
|
|
|
if src.Kind() != reflect.Map {
|
|
|
|
if overwrite {
|
|
|
|
dst.Set(src)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
for _, key := range src.MapKeys() {
|
|
|
|
srcElement := src.MapIndex(key)
|
|
|
|
if !srcElement.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dstElement := dst.MapIndex(key)
|
|
|
|
switch srcElement.Kind() {
|
|
|
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Interface, reflect.Slice:
|
|
|
|
if srcElement.IsNil() {
|
2022-04-18 17:00:37 +00:00
|
|
|
if overwrite {
|
|
|
|
dst.SetMapIndex(key, srcElement)
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
if !srcElement.CanInterface() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch reflect.TypeOf(srcElement.Interface()).Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Ptr:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Map:
|
|
|
|
srcMapElm := srcElement
|
|
|
|
dstMapElm := dstElement
|
|
|
|
if srcMapElm.CanInterface() {
|
|
|
|
srcMapElm = reflect.ValueOf(srcMapElm.Interface())
|
|
|
|
if dstMapElm.IsValid() {
|
|
|
|
dstMapElm = reflect.ValueOf(dstMapElm.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = deepMerge(dstMapElm, srcMapElm, visited, depth+1, config); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case reflect.Slice:
|
|
|
|
srcSlice := reflect.ValueOf(srcElement.Interface())
|
|
|
|
|
|
|
|
var dstSlice reflect.Value
|
|
|
|
if !dstElement.IsValid() || dstElement.IsNil() {
|
|
|
|
dstSlice = reflect.MakeSlice(srcSlice.Type(), 0, srcSlice.Len())
|
|
|
|
} else {
|
|
|
|
dstSlice = reflect.ValueOf(dstElement.Interface())
|
|
|
|
}
|
|
|
|
|
2022-04-18 17:00:37 +00:00
|
|
|
if (!isEmptyValue(src) || overwriteWithEmptySrc || overwriteSliceWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice && !sliceDeepCopy {
|
|
|
|
if typeCheck && srcSlice.Type() != dstSlice.Type() {
|
|
|
|
return fmt.Errorf("cannot override two slices with different type (%s, %s)", srcSlice.Type(), dstSlice.Type())
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
dstSlice = srcSlice
|
|
|
|
} else if config.AppendSlice {
|
|
|
|
if srcSlice.Type() != dstSlice.Type() {
|
2022-04-18 17:00:37 +00:00
|
|
|
return fmt.Errorf("cannot append two slices with different type (%s, %s)", srcSlice.Type(), dstSlice.Type())
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
dstSlice = reflect.AppendSlice(dstSlice, srcSlice)
|
2022-04-18 17:00:37 +00:00
|
|
|
} else if sliceDeepCopy {
|
|
|
|
i := 0
|
|
|
|
for ; i < srcSlice.Len() && i < dstSlice.Len(); i++ {
|
|
|
|
srcElement := srcSlice.Index(i)
|
|
|
|
dstElement := dstSlice.Index(i)
|
|
|
|
|
|
|
|
if srcElement.CanInterface() {
|
|
|
|
srcElement = reflect.ValueOf(srcElement.Interface())
|
|
|
|
}
|
|
|
|
if dstElement.CanInterface() {
|
|
|
|
dstElement = reflect.ValueOf(dstElement.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = deepMerge(dstElement, srcElement, visited, depth+1, config); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
dst.SetMapIndex(key, dstSlice)
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 17:00:37 +00:00
|
|
|
if dstElement.IsValid() && !isEmptyValue(dstElement) && (reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Map || reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Slice) {
|
2019-01-18 01:50:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-18 17:00:37 +00:00
|
|
|
if srcElement.IsValid() && ((srcElement.Kind() != reflect.Ptr && overwrite) || !dstElement.IsValid() || isEmptyValue(dstElement)) {
|
2019-01-18 01:50:10 +00:00
|
|
|
if dst.IsNil() {
|
|
|
|
dst.Set(reflect.MakeMap(dst.Type()))
|
|
|
|
}
|
|
|
|
dst.SetMapIndex(key, srcElement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Slice:
|
|
|
|
if !dst.CanSet() {
|
|
|
|
break
|
|
|
|
}
|
2022-04-18 17:00:37 +00:00
|
|
|
if (!isEmptyValue(src) || overwriteWithEmptySrc || overwriteSliceWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice && !sliceDeepCopy {
|
2019-01-18 01:50:10 +00:00
|
|
|
dst.Set(src)
|
|
|
|
} else if config.AppendSlice {
|
|
|
|
if src.Type() != dst.Type() {
|
|
|
|
return fmt.Errorf("cannot append two slice with different type (%s, %s)", src.Type(), dst.Type())
|
|
|
|
}
|
|
|
|
dst.Set(reflect.AppendSlice(dst, src))
|
2022-04-18 17:00:37 +00:00
|
|
|
} else if sliceDeepCopy {
|
|
|
|
for i := 0; i < src.Len() && i < dst.Len(); i++ {
|
|
|
|
srcElement := src.Index(i)
|
|
|
|
dstElement := dst.Index(i)
|
|
|
|
if srcElement.CanInterface() {
|
|
|
|
srcElement = reflect.ValueOf(srcElement.Interface())
|
|
|
|
}
|
|
|
|
if dstElement.CanInterface() {
|
|
|
|
dstElement = reflect.ValueOf(dstElement.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = deepMerge(dstElement, srcElement, visited, depth+1, config); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Interface:
|
2022-04-18 17:00:37 +00:00
|
|
|
if isReflectNil(src) {
|
|
|
|
if overwriteWithEmptySrc && dst.CanSet() && src.Type().AssignableTo(dst.Type()) {
|
|
|
|
dst.Set(src)
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
break
|
|
|
|
}
|
2022-04-18 17:00:37 +00:00
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
if src.Kind() != reflect.Interface {
|
2022-04-18 17:00:37 +00:00
|
|
|
if dst.IsNil() || (src.Kind() != reflect.Ptr && overwrite) {
|
2019-01-18 01:50:10 +00:00
|
|
|
if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
|
|
|
|
dst.Set(src)
|
|
|
|
}
|
|
|
|
} else if src.Kind() == reflect.Ptr {
|
|
|
|
if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, config); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if dst.Elem().Type() == src.Type() {
|
|
|
|
if err = deepMerge(dst.Elem(), src, visited, depth+1, config); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ErrDifferentArgumentsTypes
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-04-18 17:00:37 +00:00
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
if dst.IsNil() || overwrite {
|
|
|
|
if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
|
|
|
|
dst.Set(src)
|
|
|
|
}
|
2022-04-18 17:00:37 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if dst.Elem().Kind() == src.Elem().Kind() {
|
|
|
|
if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, config); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
break
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
default:
|
2022-04-18 17:00:37 +00:00
|
|
|
mustSet := (isEmptyValue(dst) || overwrite) && (!isEmptyValue(src) || overwriteWithEmptySrc)
|
|
|
|
if mustSet {
|
|
|
|
if dst.CanSet() {
|
|
|
|
dst.Set(src)
|
|
|
|
} else {
|
|
|
|
dst = src
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-18 17:00:37 +00:00
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 Merge(dst, src interface{}, opts ...func(*Config)) error {
|
|
|
|
return merge(dst, src, opts...)
|
|
|
|
}
|
|
|
|
|
2022-04-18 17:00:37 +00:00
|
|
|
// MergeWithOverwrite will do the same as Merge except that non-empty dst attributes will be overridden by
|
2019-01-18 01:50:10 +00:00
|
|
|
// non-empty src attribute values.
|
|
|
|
// Deprecated: use Merge(…) with WithOverride
|
|
|
|
func MergeWithOverwrite(dst, src interface{}, opts ...func(*Config)) error {
|
|
|
|
return merge(dst, src, append(opts, WithOverride)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTransformers adds transformers to merge, allowing to customize the merging of some types.
|
|
|
|
func WithTransformers(transformers Transformers) func(*Config) {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.Transformers = transformers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOverride will make merge override non-empty dst attributes with non-empty src attributes values.
|
|
|
|
func WithOverride(config *Config) {
|
|
|
|
config.Overwrite = true
|
|
|
|
}
|
|
|
|
|
2022-04-18 17:00:37 +00:00
|
|
|
// WithOverwriteWithEmptyValue will make merge override non empty dst attributes with empty src attributes values.
|
|
|
|
func WithOverwriteWithEmptyValue(config *Config) {
|
|
|
|
config.Overwrite = true
|
|
|
|
config.overwriteWithEmptyValue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOverrideEmptySlice will make merge override empty dst slice with empty src slice.
|
|
|
|
func WithOverrideEmptySlice(config *Config) {
|
|
|
|
config.overwriteSliceWithEmptyValue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAppendSlice will make merge append slices instead of overwriting it.
|
2019-01-18 01:50:10 +00:00
|
|
|
func WithAppendSlice(config *Config) {
|
|
|
|
config.AppendSlice = true
|
|
|
|
}
|
|
|
|
|
2022-04-18 17:00:37 +00:00
|
|
|
// WithTypeCheck will make merge check types while overwriting it (must be used with WithOverride).
|
|
|
|
func WithTypeCheck(config *Config) {
|
|
|
|
config.TypeCheck = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSliceDeepCopy will merge slice element one by one with Overwrite flag.
|
|
|
|
func WithSliceDeepCopy(config *Config) {
|
|
|
|
config.sliceDeepCopy = true
|
|
|
|
config.Overwrite = true
|
|
|
|
}
|
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
func merge(dst, src interface{}, opts ...func(*Config)) error {
|
2022-04-18 17:00:37 +00:00
|
|
|
if dst != nil && reflect.ValueOf(dst).Kind() != reflect.Ptr {
|
|
|
|
return ErrNonPointerAgument
|
|
|
|
}
|
2019-01-18 01:50:10 +00:00
|
|
|
var (
|
|
|
|
vDst, vSrc reflect.Value
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
config := &Config{}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vDst, vSrc, err = resolveValues(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vDst.Type() != vSrc.Type() {
|
|
|
|
return ErrDifferentArgumentsTypes
|
|
|
|
}
|
|
|
|
return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, config)
|
|
|
|
}
|
2022-04-18 17:00:37 +00:00
|
|
|
|
|
|
|
// IsReflectNil is the reflect value provided nil
|
|
|
|
func isReflectNil(v reflect.Value) bool {
|
|
|
|
k := v.Kind()
|
|
|
|
switch k {
|
|
|
|
case reflect.Interface, reflect.Slice, reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr:
|
|
|
|
// Both interface and slice are nil if first word is 0.
|
|
|
|
// Both are always bigger than a word; assume flagIndir.
|
|
|
|
return v.IsNil()
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|