Skip to content

Commit

Permalink
Make map merge variadic (#23)
Browse files Browse the repository at this point in the history
Can help with cases where you actually need to merge 3+ maps which does happen with helm-y stuff.
  • Loading branch information
michaeljguarino authored Mar 12, 2024
1 parent 7efbdc5 commit afa6af0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions algorithms/map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package algorithms

func Merge(m1, m2 map[string]interface{}) map[string]interface{} {
func Merge(maps ...map[string]interface{}) map[string]interface{} {
res := maps[0]
for _, m := range maps[1:] {
res = deepMerge(res, m)
}

return res
}

func deepMerge(m1, m2 map[string]interface{}) map[string]interface{} {
// lifted from helm's merge code
out := make(map[string]interface{}, len(m1))
for k, v := range m1 {
Expand All @@ -11,7 +20,7 @@ func Merge(m1, m2 map[string]interface{}) map[string]interface{} {
if v, ok := v.(map[string]interface{}); ok {
if bv, ok := out[k]; ok {
if bv, ok := bv.(map[string]interface{}); ok {
out[k] = Merge(bv, v)
out[k] = deepMerge(bv, v)
continue
}
}
Expand Down

0 comments on commit afa6af0

Please sign in to comment.