config: Outputs is now a slice

This commit is contained in:
Mitchell Hashimoto 2014-07-18 22:21:52 -07:00
parent a19e2983de
commit 3834846418
4 changed files with 28 additions and 15 deletions

View File

@ -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.

View File

@ -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

View File

@ -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)

View File

@ -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