config: Merge for modules works

This commit is contained in:
Mitchell Hashimoto 2014-09-11 19:54:02 -07:00
parent e7fe5aa452
commit dd6f536fab
3 changed files with 46 additions and 0 deletions

View File

@ -309,6 +309,25 @@ func (c *Config) allVariables() map[string][]InterpolatedVariable {
return result
}
func (m *Module) mergerName() string {
return m.Id()
}
func (m *Module) mergerMerge(other merger) merger {
m2 := other.(*Module)
result := *m
result.Name = m2.Name
result.Type = m2.Type
result.RawConfig = result.RawConfig.merge(m2.RawConfig)
if m2.Source != "" {
result.Source = m2.Source
}
return &result
}
func (o *Output) mergerName() string {
return o.Name
}

View File

@ -35,6 +35,23 @@ func Merge(c1, c2 *Config) (*Config, error) {
var m1, m2, mresult []merger
// Modules
m1 = make([]merger, 0, len(c1.Modules))
m2 = make([]merger, 0, len(c2.Modules))
for _, v := range c1.Modules {
m1 = append(m1, v)
}
for _, v := range c2.Modules {
m2 = append(m2, v)
}
mresult = mergeSlice(m1, m2)
if len(mresult) > 0 {
c.Modules = make([]*Module, len(mresult))
for i, v := range mresult {
c.Modules[i] = v.(*Module)
}
}
// Outputs
m1 = make([]merger, 0, len(c1.Outputs))
m2 = make([]merger, 0, len(c2.Outputs))

View File

@ -13,6 +13,9 @@ func TestMerge(t *testing.T) {
// Normal good case.
{
&Config{
Modules: []*Module{
&Module{Name: "foo"},
},
Outputs: []*Output{
&Output{Name: "foo"},
},
@ -30,6 +33,9 @@ func TestMerge(t *testing.T) {
},
&Config{
Modules: []*Module{
&Module{Name: "bar"},
},
Outputs: []*Output{
&Output{Name: "bar"},
},
@ -47,6 +53,10 @@ func TestMerge(t *testing.T) {
},
&Config{
Modules: []*Module{
&Module{Name: "foo"},
&Module{Name: "bar"},
},
Outputs: []*Output{
&Output{Name: "foo"},
&Output{Name: "bar"},