func readConfig(bytes []byte, files ...string) (map[interface{}]interface{}, error) { // You can't just overlay yaml bytes on to maps, it won't merge, but instead // just override the keys and not merge the map values. left := make(map[interface{}]interface{}) for _, conf := range files { content, err := readConfigFile(conf) if err != nil { return nil, err } right := make(map[interface{}]interface{}) err = yaml.Unmarshal(content, &right) if err != nil { return nil, err } left = util.MapsUnion(left, right, util.Replace) } if bytes != nil && len(bytes) > 0 { right := make(map[interface{}]interface{}) if err := yaml.Unmarshal(bytes, &right); err != nil { return nil, err } left = util.MapsUnion(left, right, util.Replace) } return left, nil }
func filterDottedKeys(data map[interface{}]interface{}, keys []string) (filtered, rest map[interface{}]interface{}) { filtered = map[interface{}]interface{}{} rest = util.MapCopy(data) for _, key := range keys { f, r := filterKey(data, strings.Split(key, ".")) filtered = util.MapsUnion(filtered, f) rest = util.MapsIntersection(rest, r) } return }
func TestMapsUnion(t *testing.T) { assert := require.New(t) m0 := map[interface{}]interface{}{"a": 1, "b": map[interface{}]interface{}{"c": 3}, "d": "4"} m1 := util.MapCopy(m0) m1["e"] = "added" m1["d"] = "replaced" delete(m0, "a") b1 := m1["b"].(map[interface{}]interface{}) delete(b1, "c") expected := map[interface{}]interface{}{"a": 1, "b": map[interface{}]interface{}{"c": 3}, "d": "replaced", "e": "added"} assert.Equal(expected, util.MapsUnion(m0, m1, util.Replace)) }
func readConfig(bytes []byte, substituteMetadataVars bool, files ...string) (map[interface{}]interface{}, error) { // You can't just overlay yaml bytes on to maps, it won't merge, but instead // just override the keys and not merge the map values. left := make(map[interface{}]interface{}) metadata := readMetadata() for _, file := range files { content, err := readConfigFile(file) if err != nil { return nil, err } if substituteMetadataVars { content = substituteVars(content, metadata) } right := make(map[interface{}]interface{}) err = yaml.Unmarshal(content, &right) if err != nil { return nil, err } left = util.MapsUnion(left, right) } if bytes != nil && len(bytes) > 0 { right := make(map[interface{}]interface{}) if substituteMetadataVars { bytes = substituteVars(bytes, metadata) } if err := yaml.Unmarshal(bytes, &right); err != nil { return nil, err } left = util.MapsUnion(left, right) } return left, nil }