forked from darccio/mergo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sliceDeepMerge feature, as seen in darccio#180
Added validations to some reflection calls that were causing panic: "reflect: call of reflect.Value.IsZero on zero Value"
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
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
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'") | ||
} | ||
} |