2016-07-20 09:46:01 +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 (
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2017-02-01 00:45:59 +00:00
|
|
|
func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, overwrite bool) (err error) {
|
2016-07-20 09:46:01 +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}
|
|
|
|
}
|
|
|
|
switch dst.Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
for i, n := 0, dst.NumField(); i < n; i++ {
|
2017-02-01 00:45:59 +00:00
|
|
|
if err = deepMerge(dst.Field(i), src.Field(i), visited, depth+1, overwrite); err != nil {
|
2016-07-20 09:46:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
for _, key := range src.MapKeys() {
|
|
|
|
srcElement := src.MapIndex(key)
|
|
|
|
if !srcElement.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dstElement := dst.MapIndex(key)
|
2017-02-01 00:45:59 +00:00
|
|
|
switch srcElement.Kind() {
|
|
|
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
|
|
|
|
if srcElement.IsNil() {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-20 09:46:01 +00:00
|
|
|
fallthrough
|
2017-02-01 00:45:59 +00:00
|
|
|
default:
|
|
|
|
if !srcElement.CanInterface() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch reflect.TypeOf(srcElement.Interface()).Kind() {
|
|
|
|
case reflect.Struct:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Ptr:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Map:
|
|
|
|
if err = deepMerge(dstElement, srcElement, visited, depth+1, overwrite); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
if !isEmptyValue(srcElement) && (overwrite || (!dstElement.IsValid() || isEmptyValue(dst))) {
|
|
|
|
if dst.IsNil() {
|
|
|
|
dst.Set(reflect.MakeMap(dst.Type()))
|
|
|
|
}
|
2016-07-20 09:46:01 +00:00
|
|
|
dst.SetMapIndex(key, srcElement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Interface:
|
|
|
|
if src.IsNil() {
|
|
|
|
break
|
2016-09-17 13:50:35 +00:00
|
|
|
} else if dst.IsNil() {
|
2017-02-01 00:45:59 +00:00
|
|
|
if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
|
2016-07-20 09:46:01 +00:00
|
|
|
dst.Set(src)
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
} else if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, overwrite); err != nil {
|
2016-07-20 09:46:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2017-02-01 00:45:59 +00:00
|
|
|
if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
|
2016-07-20 09:46:01 +00:00
|
|
|
dst.Set(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:45:59 +00: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.
|
2016-07-20 09:46:01 +00:00
|
|
|
func Merge(dst, src interface{}) error {
|
2017-02-01 00:45:59 +00:00
|
|
|
return merge(dst, src, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeWithOverwrite will do the same as Merge except that non-empty dst attributes will be overriden by
|
|
|
|
// non-empty src attribute values.
|
|
|
|
func MergeWithOverwrite(dst, src interface{}) error {
|
|
|
|
return merge(dst, src, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func merge(dst, src interface{}, overwrite bool) error {
|
2016-07-20 09:46:01 +00:00
|
|
|
var (
|
|
|
|
vDst, vSrc reflect.Value
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if vDst, vSrc, err = resolveValues(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if vDst.Type() != vSrc.Type() {
|
|
|
|
return ErrDifferentArgumentsTypes
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, overwrite)
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|