forked from darccio/mergo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Respect overwriteWithEmptySrc when merging maps
Fixes darccio/issues/230.
- Loading branch information
Showing
2 changed files
with
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package mergo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/imdario/mergo" | ||
) | ||
|
||
var testDataM = []struct { | ||
M1 mapTest | ||
M2 mapTest | ||
WithOverrideEmptyValue bool | ||
ExpectedMap map[int]int | ||
}{ | ||
{ | ||
M1: mapTest{ | ||
M: map[int]int{1: 1, 3: 3}, | ||
}, | ||
M2: mapTest{ | ||
M: map[int]int{1: 2, 2: 2}, | ||
}, | ||
WithOverrideEmptyValue: true, | ||
ExpectedMap: map[int]int{1: 1, 3: 3}, | ||
}, | ||
{ | ||
M1: mapTest{ | ||
M: map[int]int{1: 1, 3: 3}, | ||
}, | ||
M2: mapTest{ | ||
M: map[int]int{1: 2, 2: 2}, | ||
}, | ||
WithOverrideEmptyValue: false, | ||
ExpectedMap: map[int]int{1: 1, 2: 2, 3: 3}, | ||
}, | ||
{ | ||
M1: mapTest{ | ||
M: map[int]int{}, | ||
}, | ||
M2: mapTest{ | ||
M: map[int]int{1: 2, 2: 2}, | ||
}, | ||
WithOverrideEmptyValue: true, | ||
ExpectedMap: map[int]int{}, | ||
}, | ||
{ | ||
M1: mapTest{ | ||
M: map[int]int{}, | ||
}, | ||
M2: mapTest{ | ||
M: map[int]int{1: 2, 2: 2}, | ||
}, | ||
WithOverrideEmptyValue: false, | ||
ExpectedMap: map[int]int{1: 2, 2: 2}, | ||
}, | ||
} | ||
|
||
func withOverrideEmptyValue(enable bool) func(*mergo.Config) { | ||
if enable { | ||
return mergo.WithOverwriteWithEmptyValue | ||
} | ||
|
||
return mergo.WithOverride | ||
} | ||
|
||
func TestMergeMapWithOverride(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, data := range testDataM { | ||
err := mergo.Merge(&data.M2, data.M1, withOverrideEmptyValue(data.WithOverrideEmptyValue)) | ||
if err != nil { | ||
t.Errorf("Error while merging %s", err) | ||
} | ||
|
||
if len(data.M2.M) != len(data.ExpectedMap) { | ||
t.Errorf("Got %d elements in map, but expected %d", len(data.M2.M), len(data.ExpectedMap)) | ||
return | ||
} | ||
|
||
for i, val := range data.M2.M { | ||
if val != data.ExpectedMap[i] { | ||
t.Errorf("Expected value: %d, but got %d while merging map", data.ExpectedMap[i], val) | ||
} | ||
} | ||
} | ||
} |
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