From 70f83abc1e4b18226e2965db26415d3de588174c Mon Sep 17 00:00:00 2001 From: komalsinha-g <59949151+komalsinha-g@users.noreply.github.com> Date: Thu, 16 Jan 2020 15:28:25 +0530 Subject: [PATCH 1/6] 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 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/merge.go b/merge.go index 3fb6c64..50e7918 100644 --- a/merge.go +++ b/merge.go @@ -30,7 +30,7 @@ type Config struct { AppendSlice bool TypeCheck bool Transformers Transformers - overwriteWithEmptyValue bool + OverwriteWithEmptyValue bool overwriteSliceWithEmptyValue bool } @@ -44,9 +44,8 @@ type Transformers interface { func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) { overwrite := config.Overwrite typeCheck := config.TypeCheck - overwriteWithEmptySrc := config.overwriteWithEmptyValue + 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 From f1251f82b9e209e6688236ba2745f69acb94fbc0 Mon Sep 17 00:00:00 2001 From: komalsinha-g <59949151+komalsinha-g@users.noreply.github.com> Date: Thu, 16 Jan 2020 15:36:11 +0530 Subject: [PATCH 2/6] Update map.go --- map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/map.go b/map.go index 3f5afa8..769cace 100644 --- a/map.go +++ b/map.go @@ -72,7 +72,7 @@ func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, conf case reflect.Struct: srcMap := src.Interface().(map[string]interface{}) for key := range srcMap { - config.overwriteWithEmptyValue = true + config.OverwriteWithEmptyValue = true srcValue := srcMap[key] fieldName := changeInitialCase(key, unicode.ToUpper) dstElement := dst.FieldByName(fieldName) From b04021a25a492aee94d3b32978061d80ee7bedda Mon Sep 17 00:00:00 2001 From: komalsinha-g <59949151+komalsinha-g@users.noreply.github.com> Date: Thu, 16 Jan 2020 16:46:19 +0530 Subject: [PATCH 3/6] Update merge.go --- merge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/merge.go b/merge.go index 50e7918..d4ead40 100644 --- a/merge.go +++ b/merge.go @@ -30,7 +30,7 @@ type Config struct { AppendSlice bool TypeCheck bool Transformers Transformers - OverwriteWithEmptyValue bool + overwriteWithEmptyValue bool overwriteSliceWithEmptyValue bool } From fbd898a1f3602c3ee1539e270287d807bd8a4116 Mon Sep 17 00:00:00 2001 From: komalsinha-g <59949151+komalsinha-g@users.noreply.github.com> Date: Thu, 16 Jan 2020 16:46:44 +0530 Subject: [PATCH 4/6] Update map.go --- map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/map.go b/map.go index 769cace..3f5afa8 100644 --- a/map.go +++ b/map.go @@ -72,7 +72,7 @@ func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, conf case reflect.Struct: srcMap := src.Interface().(map[string]interface{}) for key := range srcMap { - config.OverwriteWithEmptyValue = true + config.overwriteWithEmptyValue = true srcValue := srcMap[key] fieldName := changeInitialCase(key, unicode.ToUpper) dstElement := dst.FieldByName(fieldName) From b94700457f4f8876ecfe0298c3ef8e134c530cdd Mon Sep 17 00:00:00 2001 From: komalsinha-g <59949151+komalsinha-g@users.noreply.github.com> Date: Thu, 16 Jan 2020 16:47:50 +0530 Subject: [PATCH 5/6] Update merge.go --- merge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/merge.go b/merge.go index d4ead40..baf8ced 100644 --- a/merge.go +++ b/merge.go @@ -44,7 +44,7 @@ type Transformers interface { func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) { overwrite := config.Overwrite typeCheck := config.TypeCheck - overwriteWithEmptySrc := config.OverwriteWithEmptyValue + overwriteWithEmptySrc := config.overwriteWithEmptyValue overwriteSliceWithEmptySrc := config.overwriteSliceWithEmptyValue if !src.IsValid() { From 4f81537b885ccaa478fc508cae6ead3006663943 Mon Sep 17 00:00:00 2001 From: komalsinha-g <59949151+komalsinha-g@users.noreply.github.com> Date: Thu, 16 Jan 2020 16:51:16 +0530 Subject: [PATCH 6/6] Update merge.go --- merge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/merge.go b/merge.go index baf8ced..9281282 100644 --- a/merge.go +++ b/merge.go @@ -247,7 +247,7 @@ func WithOverride(config *Config) { // WithOverwriteWithEmptyValue will make merge override non empty dst attributes with empty src attributes values. func WithOverwriteWithEmptyValue(config *Config) { - config.OverwriteWithEmptyValue = true + config.overwriteWithEmptyValue = true } // WithOverride will make merge override empty dst slice with empty src slice.