diff --git a/config/config.go b/config/config.go index 51b0deabf..7ea4d5632 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,7 @@ type Config struct { ProviderConfigs map[string]*ProviderConfig Resources []*Resource Variables []*Variable - Outputs map[string]*Output + Outputs []*Output // The fields below can be filled in by loaders for validation // purposes. diff --git a/config/loader_libucl.go b/config/loader_libucl.go index 9eea02a21..81f9f5068 100644 --- a/config/loader_libucl.go +++ b/config/loader_libucl.go @@ -183,7 +183,7 @@ func loadFileLibucl(root string) (configurable, []string, error) { // LoadOutputsLibucl recurses into the given libucl object and turns // it into a mapping of outputs. -func loadOutputsLibucl(o *libucl.Object) (map[string]*Output, error) { +func loadOutputsLibucl(o *libucl.Object) ([]*Output, error) { objects := make(map[string]*libucl.Object) // Iterate over all the "output" blocks and get the keys along with @@ -201,8 +201,13 @@ func loadOutputsLibucl(o *libucl.Object) (map[string]*Output, error) { } iter.Close() + // If we have none, just return nil + if len(objects) == 0 { + return nil, nil + } + // Go through each object and turn it into an actual result. - result := make(map[string]*Output) + result := make([]*Output, 0, len(objects)) for n, o := range objects { var config map[string]interface{} @@ -218,10 +223,10 @@ func loadOutputsLibucl(o *libucl.Object) (map[string]*Output, error) { err) } - result[n] = &Output{ + result = append(result, &Output{ Name: n, RawConfig: rawConfig, - } + }) } return result, nil diff --git a/config/loader_test.go b/config/loader_test.go index c05e55843..2266ee401 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -156,16 +156,18 @@ func TestLoadDir_noConfigs(t *testing.T) { } } -func outputsStr(os map[string]*Output) string { +func outputsStr(os []*Output) string { ns := make([]string, 0, len(os)) - for n, _ := range os { - ns = append(ns, n) + m := make(map[string]*Output) + for _, o := range os { + ns = append(ns, o.Name) + m[o.Name] = o } sort.Strings(ns) result := "" for _, n := range ns { - o := os[n] + o := m[n] result += fmt.Sprintf("%s\n", n) diff --git a/config/merge.go b/config/merge.go index e8c8dff5c..a1df4dbfa 100644 --- a/config/merge.go +++ b/config/merge.go @@ -52,12 +52,18 @@ func Merge(c1, c2 *Config) (*Config, error) { // 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 + if len(c1.Outputs) > 0 || len(c2.Outputs) > 0 { + c.Outputs = make([]*Output, 0, len(c1.Outputs)+len(c2.Outputs)) + m := make(map[string]*Output) + for _, v := range c1.Outputs { + m[v.Name] = v + } + for _, v := range c2.Outputs { + m[v.Name] = v + } + for _, v := range m { + c.Outputs = append(c.Outputs, v) + } } // Merge provider configs: If they collide, we just take the latest one