Skip to content

Commit afa6af0

Browse files
Make map merge variadic (#23)
Can help with cases where you actually need to merge 3+ maps which does happen with helm-y stuff.
1 parent 7efbdc5 commit afa6af0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

algorithms/map.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package algorithms
22

3-
func Merge(m1, m2 map[string]interface{}) map[string]interface{} {
3+
func Merge(maps ...map[string]interface{}) map[string]interface{} {
4+
res := maps[0]
5+
for _, m := range maps[1:] {
6+
res = deepMerge(res, m)
7+
}
8+
9+
return res
10+
}
11+
12+
func deepMerge(m1, m2 map[string]interface{}) map[string]interface{} {
413
// lifted from helm's merge code
514
out := make(map[string]interface{}, len(m1))
615
for k, v := range m1 {
@@ -11,7 +20,7 @@ func Merge(m1, m2 map[string]interface{}) map[string]interface{} {
1120
if v, ok := v.(map[string]interface{}); ok {
1221
if bv, ok := out[k]; ok {
1322
if bv, ok := bv.(map[string]interface{}); ok {
14-
out[k] = Merge(bv, v)
23+
out[k] = deepMerge(bv, v)
1524
continue
1625
}
1726
}

0 commit comments

Comments
 (0)