2017-11-20 09:30:52 -05: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
|
|
|
|
|
2018-04-05 04:37:35 -04:00
|
|
|
import (
|
2018-07-31 03:57:56 -04:00
|
|
|
"fmt"
|
2018-04-05 04:37:35 -04:00
|
|
|
"reflect"
|
|
|
|
)
|
2017-09-29 08:21:40 -04:00
|
|
|
|
|
|
|
func hasExportedField(dst reflect.Value) (exported bool) {
|
|
|
|
for i, n := 0, dst.NumField(); i < n; i++ {
|
|
|
|
field := dst.Type().Field(i)
|
|
|
|
if field.Anonymous && dst.Field(i).Kind() == reflect.Struct {
|
|
|
|
exported = exported || hasExportedField(dst.Field(i))
|
|
|
|
} else {
|
|
|
|
exported = exported || len(field.PkgPath) == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-05 04:37:35 -04:00
|
|
|
type Config struct {
|
|
|
|
Overwrite bool
|
|
|
|
AppendSlice bool
|
|
|
|
Transformers Transformers
|
2017-09-29 08:21:40 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:37:35 -04:00
|
|
|
type Transformers interface {
|
2017-09-29 08:21:40 -04:00
|
|
|
Transformer(reflect.Type) func(dst, src reflect.Value) error
|
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
|
|
|
|
// 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.
|
2018-04-05 04:37:35 -04:00
|
|
|
func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) {
|
|
|
|
overwrite := config.Overwrite
|
2017-09-29 08:21:40 -04:00
|
|
|
|
2017-11-20 09:30:52 -05: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}
|
|
|
|
}
|
2017-09-29 08:21:40 -04:00
|
|
|
|
2018-04-05 04:37:35 -04:00
|
|
|
if config.Transformers != nil && !isEmptyValue(dst) {
|
|
|
|
if fn := config.Transformers.Transformer(dst.Type()); fn != nil {
|
2017-09-29 08:21:40 -04:00
|
|
|
err = fn(dst, src)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 09:30:52 -05:00
|
|
|
switch dst.Kind() {
|
|
|
|
case reflect.Struct:
|
2017-09-29 08:21:40 -04:00
|
|
|
if hasExportedField(dst) {
|
|
|
|
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 {
|
|
|
|
if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
|
|
|
|
dst.Set(src)
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
2018-04-05 04:37:35 -04:00
|
|
|
if dst.IsNil() && !src.IsNil() {
|
2017-09-29 08:21:40 -04:00
|
|
|
dst.Set(reflect.MakeMap(dst.Type()))
|
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
for _, key := range src.MapKeys() {
|
|
|
|
srcElement := src.MapIndex(key)
|
|
|
|
if !srcElement.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dstElement := dst.MapIndex(key)
|
2017-09-29 08:21:40 -04:00
|
|
|
switch srcElement.Kind() {
|
|
|
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Interface, reflect.Slice:
|
|
|
|
if srcElement.IsNil() {
|
|
|
|
continue
|
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
fallthrough
|
2017-09-29 08:21:40 -04:00
|
|
|
default:
|
|
|
|
if !srcElement.CanInterface() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch reflect.TypeOf(srcElement.Interface()).Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Ptr:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Map:
|
2018-06-08 11:15:12 -04:00
|
|
|
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 {
|
2017-09-29 08:21:40 -04:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2018-06-08 11:15:12 -04:00
|
|
|
if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
|
|
|
|
dstSlice = srcSlice
|
|
|
|
} else if config.AppendSlice {
|
2018-07-31 03:57:56 -04:00
|
|
|
if srcSlice.Type() != dstSlice.Type() {
|
|
|
|
return fmt.Errorf("cannot append two slice with different type (%s, %s)", srcSlice.Type(), dstSlice.Type())
|
|
|
|
}
|
2018-06-08 11:15:12 -04:00
|
|
|
dstSlice = reflect.AppendSlice(dstSlice, srcSlice)
|
|
|
|
}
|
2017-09-29 08:21:40 -04:00
|
|
|
dst.SetMapIndex(key, dstSlice)
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-29 08:21:40 -04:00
|
|
|
if dstElement.IsValid() && reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Map {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-08 11:15:12 -04:00
|
|
|
if srcElement.IsValid() && (overwrite || (!dstElement.IsValid() || isEmptyValue(dstElement))) {
|
2017-09-29 08:21:40 -04:00
|
|
|
if dst.IsNil() {
|
|
|
|
dst.Set(reflect.MakeMap(dst.Type()))
|
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
dst.SetMapIndex(key, srcElement)
|
|
|
|
}
|
|
|
|
}
|
2017-09-29 08:21:40 -04:00
|
|
|
case reflect.Slice:
|
2018-04-05 04:37:35 -04:00
|
|
|
if !dst.CanSet() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
|
|
|
|
dst.Set(src)
|
2018-06-08 11:15:12 -04:00
|
|
|
} else if config.AppendSlice {
|
2018-07-31 03:57:56 -04:00
|
|
|
if src.Type() != dst.Type() {
|
|
|
|
return fmt.Errorf("cannot append two slice with different type (%s, %s)", src.Type(), dst.Type())
|
|
|
|
}
|
2018-04-05 04:37:35 -04:00
|
|
|
dst.Set(reflect.AppendSlice(dst, src))
|
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
case reflect.Ptr:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Interface:
|
|
|
|
if src.IsNil() {
|
|
|
|
break
|
2017-09-29 08:21:40 -04:00
|
|
|
}
|
|
|
|
if src.Kind() != reflect.Interface {
|
|
|
|
if dst.IsNil() || overwrite {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if dst.IsNil() || overwrite {
|
|
|
|
if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
|
2017-11-20 09:30:52 -05:00
|
|
|
dst.Set(src)
|
|
|
|
}
|
2017-09-29 08:21:40 -04:00
|
|
|
} else if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, config); err != nil {
|
2017-11-20 09:30:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2017-09-29 08:21:40 -04:00
|
|
|
if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
|
2017-11-20 09:30:52 -05:00
|
|
|
dst.Set(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-29 08:21:40 -04:00
|
|
|
// 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.
|
2018-04-05 04:37:35 -04:00
|
|
|
func Merge(dst, src interface{}, opts ...func(*Config)) error {
|
2017-09-29 08:21:40 -04:00
|
|
|
return merge(dst, src, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2018-04-05 04:37:35 -04:00
|
|
|
func MergeWithOverwrite(dst, src interface{}, opts ...func(*Config)) error {
|
2017-09-29 08:21:40 -04:00
|
|
|
return merge(dst, src, append(opts, WithOverride)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTransformers adds transformers to merge, allowing to customize the merging of some types.
|
2018-04-05 04:37:35 -04:00
|
|
|
func WithTransformers(transformers Transformers) func(*Config) {
|
|
|
|
return func(config *Config) {
|
|
|
|
config.Transformers = transformers
|
2017-09-29 08:21:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOverride will make merge override non-empty dst attributes with non-empty src attributes values.
|
2018-04-05 04:37:35 -04:00
|
|
|
func WithOverride(config *Config) {
|
|
|
|
config.Overwrite = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAppendSlice will make merge append slices instead of overwriting it
|
|
|
|
func WithAppendSlice(config *Config) {
|
|
|
|
config.AppendSlice = true
|
2017-09-29 08:21:40 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:37:35 -04:00
|
|
|
func merge(dst, src interface{}, opts ...func(*Config)) error {
|
2017-11-20 09:30:52 -05:00
|
|
|
var (
|
|
|
|
vDst, vSrc reflect.Value
|
|
|
|
err error
|
|
|
|
)
|
2017-09-29 08:21:40 -04:00
|
|
|
|
2018-04-05 04:37:35 -04:00
|
|
|
config := &Config{}
|
2017-09-29 08:21:40 -04:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(config)
|
|
|
|
}
|
|
|
|
|
2017-11-20 09:30:52 -05:00
|
|
|
if vDst, vSrc, err = resolveValues(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vDst.Type() != vSrc.Type() {
|
|
|
|
return ErrDifferentArgumentsTypes
|
|
|
|
}
|
2017-09-29 08:21:40 -04:00
|
|
|
return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, config)
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|