|
@@ -26,9 +26,10 @@ func hasExportedField(dst reflect.Value) (exported bool) {
|
|
|
}
|
|
|
|
|
|
type Config struct {
|
|
|
- Overwrite bool
|
|
|
- AppendSlice bool
|
|
|
- Transformers Transformers
|
|
|
+ Overwrite bool
|
|
|
+ AppendSlice bool
|
|
|
+ Transformers Transformers
|
|
|
+ overwriteWithEmptyValue bool
|
|
|
}
|
|
|
|
|
|
type Transformers interface {
|
|
@@ -40,6 +41,8 @@ type Transformers interface {
|
|
|
// short circuiting on recursive types.
|
|
|
func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) {
|
|
|
overwrite := config.Overwrite
|
|
|
+ overwriteWithEmptySrc := config.overwriteWithEmptyValue
|
|
|
+ config.overwriteWithEmptyValue = false
|
|
|
|
|
|
if !src.IsValid() {
|
|
|
return
|
|
@@ -74,7 +77,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
|
|
|
+ if dst.CanSet() && (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) {
|
|
|
dst.Set(src)
|
|
|
}
|
|
|
}
|
|
@@ -125,7 +128,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
|
|
dstSlice = reflect.ValueOf(dstElement.Interface())
|
|
|
}
|
|
|
|
|
|
- if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
|
|
|
+ if (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
|
|
|
dstSlice = srcSlice
|
|
|
} else if config.AppendSlice {
|
|
|
if srcSlice.Type() != dstSlice.Type() {
|
|
@@ -136,7 +139,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
|
|
dst.SetMapIndex(key, dstSlice)
|
|
|
}
|
|
|
}
|
|
|
- if dstElement.IsValid() && reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Map {
|
|
|
+ if dstElement.IsValid() && !isEmptyValue(dstElement) && (reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Map || reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Slice) {
|
|
|
continue
|
|
|
}
|
|
|
|
|
@@ -151,7 +154,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
|
|
if !dst.CanSet() {
|
|
|
break
|
|
|
}
|
|
|
- if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
|
|
|
+ if (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
|
|
|
dst.Set(src)
|
|
|
} else if config.AppendSlice {
|
|
|
if src.Type() != dst.Type() {
|
|
@@ -191,7 +194,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
|
|
|
return
|
|
|
}
|
|
|
default:
|
|
|
- if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
|
|
|
+ if dst.CanSet() && (!isEmptyValue(src) || overwriteWithEmptySrc) && (overwrite || isEmptyValue(dst)) {
|
|
|
dst.Set(src)
|
|
|
}
|
|
|
}
|