From 3d67c967aaab65477137753f3c12be17feddbcd0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 18 Jul 2014 17:05:22 -0700 Subject: [PATCH] config: make Merge an exported func --- config/config_tree.go | 68 ++----------------------------------------- config/loader.go | 2 +- config/merge.go | 65 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 67 deletions(-) create mode 100644 config/merge.go diff --git a/config/config_tree.go b/config/config_tree.go index 765910e8c..08dc0fe90 100644 --- a/config/config_tree.go +++ b/config/config_tree.go @@ -1,9 +1,5 @@ package config -import ( - "fmt" -) - // configTree represents a tree of configurations where the root is the // first file and its children are the configurations it has imported. type configTree struct { @@ -36,72 +32,12 @@ func (t *configTree) Flatten() (*Config, error) { childConfigs = childConfigs[1:] for _, config2 := range childConfigs { var err error - config, err = mergeConfig(config, config2) + config, err = Merge(config, config2) if err != nil { return nil, err } } // Merge the final merged child config with our own - return mergeConfig(config, t.Config) -} - -func mergeConfig(c1, c2 *Config) (*Config, error) { - c := new(Config) - - // Merge variables: Variable merging is quite simple. Set fields in - // later set variables override those earlier. - c.Variables = c1.Variables - for k, v2 := range c2.Variables { - v1, ok := c.Variables[k] - if ok { - if v2.Default == "" { - v2.Default = v1.Default - } - if v2.Description == "" { - v2.Description = v1.Description - } - } - - c.Variables[k] = v2 - } - - // Merge outputs: If they collide, just take the latest one for now. In - // the future, we might provide smarter merge functionality. - c.Outputs = make(map[string]*Output) - for k, v := range c1.Outputs { - c.Outputs[k] = v - } - for k, v := range c2.Outputs { - c.Outputs[k] = v - } - - // Merge provider configs: If they collide, we just take the latest one - // for now. In the future, we might provide smarter merge functionality. - c.ProviderConfigs = make(map[string]*ProviderConfig) - for k, v := range c1.ProviderConfigs { - c.ProviderConfigs[k] = v - } - for k, v := range c2.ProviderConfigs { - c.ProviderConfigs[k] = v - } - - // Merge resources: If they collide, we just take the latest one - // for now. In the future, we might provide smarter merge functionality. - resources := make(map[string]*Resource) - for _, r := range c1.Resources { - id := fmt.Sprintf("%s[%s]", r.Type, r.Name) - resources[id] = r - } - for _, r := range c2.Resources { - id := fmt.Sprintf("%s[%s]", r.Type, r.Name) - resources[id] = r - } - - c.Resources = make([]*Resource, 0, len(resources)) - for _, r := range resources { - c.Resources = append(c.Resources, r) - } - - return c, nil + return Merge(config, t.Config) } diff --git a/config/loader.go b/config/loader.go index a8cbb5adf..5f0e8bd7e 100644 --- a/config/loader.go +++ b/config/loader.go @@ -50,7 +50,7 @@ func LoadDir(path string) (*Config, error) { } if result != nil { - result, err = mergeConfig(result, c) + result, err = Merge(result, c) if err != nil { return nil, err } diff --git a/config/merge.go b/config/merge.go new file mode 100644 index 000000000..29c68a5f6 --- /dev/null +++ b/config/merge.go @@ -0,0 +1,65 @@ +package config + +import ( + "fmt" +) + +func Merge(c1, c2 *Config) (*Config, error) { + c := new(Config) + + // Merge variables: Variable merging is quite simple. Set fields in + // later set variables override those earlier. + c.Variables = c1.Variables + for k, v2 := range c2.Variables { + v1, ok := c.Variables[k] + if ok { + if v2.Default == "" { + v2.Default = v1.Default + } + if v2.Description == "" { + v2.Description = v1.Description + } + } + + c.Variables[k] = v2 + } + + // Merge outputs: If they collide, just take the latest one for now. In + // the future, we might provide smarter merge functionality. + c.Outputs = make(map[string]*Output) + for k, v := range c1.Outputs { + c.Outputs[k] = v + } + for k, v := range c2.Outputs { + c.Outputs[k] = v + } + + // Merge provider configs: If they collide, we just take the latest one + // for now. In the future, we might provide smarter merge functionality. + c.ProviderConfigs = make(map[string]*ProviderConfig) + for k, v := range c1.ProviderConfigs { + c.ProviderConfigs[k] = v + } + for k, v := range c2.ProviderConfigs { + c.ProviderConfigs[k] = v + } + + // Merge resources: If they collide, we just take the latest one + // for now. In the future, we might provide smarter merge functionality. + resources := make(map[string]*Resource) + for _, r := range c1.Resources { + id := fmt.Sprintf("%s[%s]", r.Type, r.Name) + resources[id] = r + } + for _, r := range c2.Resources { + id := fmt.Sprintf("%s[%s]", r.Type, r.Name) + resources[id] = r + } + + c.Resources = make([]*Resource, 0, len(resources)) + for _, r := range resources { + c.Resources = append(c.Resources, r) + } + + return c, nil +}