From 0856b6066dabf523bf2d7e0ca582e7ee15891391 Mon Sep 17 00:00:00 2001 From: komalsinha-g <59949151+komalsinha-g@users.noreply.github.com> Date: Fri, 17 Jan 2020 14:36:55 +0530 Subject: [PATCH] Make "OverwriteWithEmptyValue" in config struct to be set by any method. (#133) Make "OverwriteWithEmptyValue" in config struct to be set by any method. this make the merge more customizable. In cases, where request toggles a boolean value and the the request message can change a "true" value to false we can pass the flag to merge accordingly, which is not possible otherwise. --- merge.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/merge.go b/merge.go index 3fb6c64..9281282 100644 --- a/merge.go +++ b/merge.go @@ -46,7 +46,6 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co typeCheck := config.TypeCheck overwriteWithEmptySrc := config.overwriteWithEmptyValue overwriteSliceWithEmptySrc := config.overwriteSliceWithEmptyValue - config.overwriteWithEmptyValue = false if !src.IsValid() { return @@ -246,6 +245,11 @@ func WithOverride(config *Config) { config.Overwrite = true } +// WithOverwriteWithEmptyValue will make merge override non empty dst attributes with empty src attributes values. +func WithOverwriteWithEmptyValue(config *Config) { + config.overwriteWithEmptyValue = true +} + // WithOverride will make merge override empty dst slice with empty src slice. func WithOverrideEmptySlice(config *Config) { config.overwriteSliceWithEmptyValue = true