Merge configs section by section

Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
Maksym Pavlenko 2019-08-23 15:49:02 -07:00
parent 8ebffecbc3
commit 24b9e2c1a0
2 changed files with 34 additions and 12 deletions

View File

@ -297,8 +297,27 @@ func resolveImports(parent string, imports []string) ([]string, error) {
// 0 1 1 // 0 1 1
// []{"1"} []{"2"} []{"1","2"} // []{"1"} []{"2"} []{"1","2"}
// []{"1"} []{} []{"1"} // []{"1"} []{} []{"1"}
// Maps merged by keys, but values are replaced entirely.
func mergeConfig(to, from *Config) error { func mergeConfig(to, from *Config) error {
return mergo.Merge(to, from, mergo.WithOverride, mergo.WithAppendSlice) err := mergo.Merge(to, from, mergo.WithOverride, mergo.WithAppendSlice)
if err != nil {
return err
}
// Replace entire sections instead of merging map's values.
for k, v := range from.Plugins {
to.Plugins[k] = v
}
for k, v := range from.StreamProcessors {
to.StreamProcessors[k] = v
}
for k, v := range from.ProxyPlugins {
to.ProxyPlugins[k] = v
}
return nil
} }
// V1DisabledFilter matches based on ID // V1DisabledFilter matches based on ID

View File

@ -29,20 +29,22 @@ import (
func TestMergeConfigs(t *testing.T) { func TestMergeConfigs(t *testing.T) {
a := &Config{ a := &Config{
Version: 2, Version: 2,
Root: "old_root", Root: "old_root",
RequiredPlugins: []string{"old_plugin"}, RequiredPlugins: []string{"old_plugin"},
DisabledPlugins: []string{"old_plugin"}, DisabledPlugins: []string{"old_plugin"},
State: "old_state", State: "old_state",
OOMScore: 1, OOMScore: 1,
Timeouts: map[string]string{"a": "1"}, Timeouts: map[string]string{"a": "1"},
StreamProcessors: map[string]StreamProcessor{"1": {Path: "2", Returns: "4"}, "2": {Path: "5"}},
} }
b := &Config{ b := &Config{
Root: "new_root", Root: "new_root",
RequiredPlugins: []string{"new_plugin1", "new_plugin2"}, RequiredPlugins: []string{"new_plugin1", "new_plugin2"},
OOMScore: 2, OOMScore: 2,
Timeouts: map[string]string{"b": "2"}, Timeouts: map[string]string{"b": "2"},
StreamProcessors: map[string]StreamProcessor{"1": {Path: "3"}},
} }
err := mergeConfig(a, b) err := mergeConfig(a, b)
@ -55,6 +57,7 @@ func TestMergeConfigs(t *testing.T) {
assert.DeepEqual(t, a.RequiredPlugins, []string{"old_plugin", "new_plugin1", "new_plugin2"}) assert.DeepEqual(t, a.RequiredPlugins, []string{"old_plugin", "new_plugin1", "new_plugin2"})
assert.DeepEqual(t, a.DisabledPlugins, []string{"old_plugin"}) assert.DeepEqual(t, a.DisabledPlugins, []string{"old_plugin"})
assert.DeepEqual(t, a.Timeouts, map[string]string{"a": "1", "b": "2"}) assert.DeepEqual(t, a.Timeouts, map[string]string{"a": "1", "b": "2"})
assert.DeepEqual(t, a.StreamProcessors, map[string]StreamProcessor{"1": {Path: "3"}, "2": {Path: "5"}})
} }
func TestResolveImports(t *testing.T) { func TestResolveImports(t *testing.T) {