-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from arbourd/move-merge-maps
Move MergeMaps from helm-controller
- Loading branch information
Showing
2 changed files
with
118 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,41 @@ | ||
/* | ||
Copyright 2020 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package transform | ||
|
||
// MergeMaps merges map b into given map a and returns the result. | ||
// It allows overwrites of map values with flat values, and vice versa. | ||
// This is copied from https://github.com/helm/helm/blob/v3.3.0/pkg/cli/values/options.go#L88, | ||
// as the public chartutil.CoalesceTables function does not allow | ||
// overwriting maps with flat values. | ||
func MergeMaps(a, b map[string]interface{}) map[string]interface{} { | ||
out := make(map[string]interface{}, len(a)) | ||
for k, v := range a { | ||
out[k] = v | ||
} | ||
for k, v := range b { | ||
if v, ok := v.(map[string]interface{}); ok { | ||
if bv, ok := out[k]; ok { | ||
if bv, ok := bv.(map[string]interface{}); ok { | ||
out[k] = MergeMaps(bv, v) | ||
continue | ||
} | ||
} | ||
} | ||
out[k] = v | ||
} | ||
return out | ||
} |
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,77 @@ | ||
/* | ||
Copyright 2020 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package transform | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestMergeMaps(t *testing.T) { | ||
nestedMap := map[string]interface{}{ | ||
"foo": "bar", | ||
"baz": map[string]string{ | ||
"cool": "stuff", | ||
}, | ||
} | ||
anotherNestedMap := map[string]interface{}{ | ||
"foo": "bar", | ||
"baz": map[string]string{ | ||
"cool": "things", | ||
"awesome": "stuff", | ||
}, | ||
} | ||
flatMap := map[string]interface{}{ | ||
"foo": "bar", | ||
"baz": "stuff", | ||
} | ||
anotherFlatMap := map[string]interface{}{ | ||
"testing": "fun", | ||
} | ||
|
||
testMap := MergeMaps(flatMap, nestedMap) | ||
equal := reflect.DeepEqual(testMap, nestedMap) | ||
if !equal { | ||
t.Errorf("Expected a nested map to overwrite a flat value. Expected: %v, got %v", nestedMap, testMap) | ||
} | ||
|
||
testMap = MergeMaps(nestedMap, flatMap) | ||
equal = reflect.DeepEqual(testMap, flatMap) | ||
if !equal { | ||
t.Errorf("Expected a flat value to overwrite a map. Expected: %v, got %v", flatMap, testMap) | ||
} | ||
|
||
testMap = MergeMaps(nestedMap, anotherNestedMap) | ||
equal = reflect.DeepEqual(testMap, anotherNestedMap) | ||
if !equal { | ||
t.Errorf("Expected a nested map to overwrite another nested map. Expected: %v, got %v", anotherNestedMap, testMap) | ||
} | ||
|
||
testMap = MergeMaps(anotherFlatMap, anotherNestedMap) | ||
expectedMap := map[string]interface{}{ | ||
"testing": "fun", | ||
"foo": "bar", | ||
"baz": map[string]string{ | ||
"cool": "things", | ||
"awesome": "stuff", | ||
}, | ||
} | ||
equal = reflect.DeepEqual(testMap, expectedMap) | ||
if !equal { | ||
t.Errorf("Expected a map with different keys to merge properly with another map. Expected: %v, got %v", expectedMap, testMap) | ||
} | ||
} |