-
-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new WithSliceDeepMerge config option #222
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,8 @@ type Config struct { | |
overwriteWithEmptyValue bool | ||
overwriteSliceWithEmptyValue bool | ||
sliceDeepCopy bool | ||
sliceDeepMerge bool | ||
OverwriteNilInMaps bool | ||
debug bool | ||
} | ||
|
||
|
@@ -57,10 +59,12 @@ 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 | ||
overwriteNilInMaps := config.OverwriteNilInMaps | ||
typeCheck := config.TypeCheck | ||
overwriteWithEmptySrc := config.overwriteWithEmptyValue | ||
overwriteSliceWithEmptySrc := config.overwriteSliceWithEmptyValue | ||
sliceDeepCopy := config.sliceDeepCopy | ||
sliceDeepMerge := config.sliceDeepMerge | ||
|
||
if !src.IsValid() { | ||
return | ||
|
@@ -125,7 +129,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co | |
switch srcElement.Kind() { | ||
case reflect.Chan, reflect.Func, reflect.Map, reflect.Interface, reflect.Slice: | ||
if srcElement.IsNil() { | ||
if overwrite { | ||
if overwrite || overwriteNilInMaps { | ||
dst.SetMapIndex(key, srcElement) | ||
} | ||
continue | ||
|
@@ -189,7 +193,39 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co | |
return | ||
} | ||
} | ||
} else if sliceDeepMerge { | ||
if srcSlice.Len() > dstSlice.Len() { | ||
newSlice := reflect.MakeSlice(srcSlice.Type(), srcSlice.Len(), srcSlice.Len()) | ||
|
||
for i := 0; i < dstSlice.Len(); i++ { | ||
newSlice.Index(i).Set(dstSlice.Index(i)) | ||
} | ||
|
||
dstSlice = newSlice | ||
} | ||
|
||
for i := 0; i < srcSlice.Len(); i++ { | ||
srcElem := srcSlice.Index(i) | ||
dstElem := dstSlice.Index(i) | ||
|
||
if srcElem.CanInterface() { | ||
srcElem = reflect.ValueOf(srcElem.Interface()) | ||
} | ||
|
||
if dstElem.CanInterface() { | ||
dstElem = reflect.ValueOf(dstElem.Interface()) | ||
} | ||
|
||
if dstSlice.Index(i).IsZero() && dstSlice.Index(i).IsValid() && srcElem.IsValid() { | ||
dstSlice.Index(i).Set(srcElem) | ||
} else { | ||
if err = deepMerge(dstElem, srcElem, visited, depth+1, config); err != nil { | ||
return | ||
} else if dstSlice.Index(i).IsValid() && dstElem.IsValid() { | ||
dstSlice.Index(i).Set(dstElem) | ||
} | ||
} | ||
} | ||
} | ||
dst.SetMapIndex(key, dstSlice) | ||
} | ||
|
@@ -231,6 +267,38 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co | |
return | ||
} | ||
} | ||
} else if sliceDeepMerge { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid deeply nested control flow statements. |
||
if src.Len() > dst.Len() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
newSlice := reflect.MakeSlice(dst.Type(), src.Len(), src.Len()) | ||
|
||
for i := 0; i < dst.Len(); i++ { | ||
newSlice.Index(i).Set(dst.Index(i)) | ||
} | ||
|
||
dst = newSlice | ||
} | ||
|
||
for i := 0; i < src.Len(); i++ { | ||
srcElem := src.Index(i) | ||
dstElem := dst.Index(i) | ||
|
||
if srcElem.CanInterface() { | ||
srcElem = reflect.ValueOf(srcElem.Interface()) | ||
} | ||
|
||
if dstElem.CanInterface() { | ||
dstElem = reflect.ValueOf(dstElem.Interface()) | ||
} | ||
|
||
if dst.Index(i).IsZero() { | ||
dst.Index(i).Set(srcElem) | ||
} else { | ||
if err = deepMerge(dstElem, srcElem, visited, depth+1, config); err != nil { | ||
return | ||
} | ||
dst.Index(i).Set(dstElem) | ||
} | ||
} | ||
} | ||
case reflect.Ptr: | ||
fallthrough | ||
|
@@ -342,6 +410,11 @@ func WithSliceDeepCopy(config *Config) { | |
config.Overwrite = true | ||
} | ||
|
||
// WithSliceDeepMerge will make merge deep merge slice elements pairwise (resizing dst slice if needed) | ||
func WithSliceDeepMerge(config *Config) { | ||
config.sliceDeepMerge = true | ||
} | ||
|
||
func merge(dst, src interface{}, opts ...func(*Config)) error { | ||
if dst != nil && reflect.ValueOf(dst).Kind() != reflect.Ptr { | ||
return ErrNonPointerAgument | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package mergo_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/imdario/mergo" | ||
) | ||
|
||
func pp(i interface{}) string { | ||
b, _ := json.MarshalIndent(i, "", " ") | ||
return string(b) | ||
} | ||
|
||
func TestIssue121WithSliceDeepMerge(t *testing.T) { | ||
dst := map[string]interface{}{ | ||
"a": "1", | ||
"b": []map[string]interface{}{ | ||
{"c": "2"}, | ||
}, | ||
} | ||
|
||
src := map[string]interface{}{ | ||
"b": []map[string]interface{}{ | ||
{"c": "3", "d": "1"}, | ||
{"e": "1", "f": "1", "g": []string{"1", "2"}}, | ||
}, | ||
} | ||
|
||
if err := mergo.Merge(&dst, src, mergo.WithSliceDeepMerge); err != nil { | ||
t.Fatalf("Error during the merge: %v", err) | ||
} | ||
|
||
fmt.Println(pp(dst)) | ||
|
||
if dst["a"].(string) != "1" { | ||
t.Error("a should equal '1'") | ||
} | ||
|
||
if dst["b"].([]map[string]interface{})[0]["c"] != "2" { | ||
t.Error("b.[0].c should equal '2'") | ||
} | ||
|
||
if dst["b"].([]map[string]interface{})[0]["d"] != "1" { | ||
t.Error("b.[0].d should equal '2'") | ||
} | ||
|
||
if dst["b"].([]map[string]interface{})[1]["e"] != "1" { | ||
t.Error("b.[1].e should equal '1'") | ||
} | ||
|
||
if dst["b"].([]map[string]interface{})[1]["g"].([]string)[0] != "1" { | ||
t.Error("b.[1].g[0] should equal '1'") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar blocks of code found in 2 locations. Consider refactoring.