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 ProviderConfigs map[string]*ProviderConfig
Resources []*Resource Resources []*Resource
Variables []*Variable Variables []*Variable
Outputs map[string]*Output Outputs []*Output
// The fields below can be filled in by loaders for validation // The fields below can be filled in by loaders for validation
// purposes. // purposes.

View File

@ -183,7 +183,7 @@ func loadFileLibucl(root string) (configurable, []string, error) {
// LoadOutputsLibucl recurses into the given libucl object and turns // LoadOutputsLibucl recurses into the given libucl object and turns
// it into a mapping of outputs. // 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) objects := make(map[string]*libucl.Object)
// Iterate over all the "output" blocks and get the keys along with // 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() 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. // 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 { for n, o := range objects {
var config map[string]interface{} var config map[string]interface{}
@ -218,10 +223,10 @@ func loadOutputsLibucl(o *libucl.Object) (map[string]*Output, error) {
err) err)
} }
result[n] = &Output{ result = append(result, &Output{
Name: n, Name: n,
RawConfig: rawConfig, RawConfig: rawConfig,
} })
} }
return result, nil 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)) ns := make([]string, 0, len(os))
for n, _ := range os { m := make(map[string]*Output)
ns = append(ns, n) for _, o := range os {
ns = append(ns, o.Name)
m[o.Name] = o
} }
sort.Strings(ns) sort.Strings(ns)
result := "" result := ""
for _, n := range ns { for _, n := range ns {
o := os[n] o := m[n]
result += fmt.Sprintf("%s\n", 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 // Merge outputs: If they collide, just take the latest one for now. In
// the future, we might provide smarter merge functionality. // the future, we might provide smarter merge functionality.
c.Outputs = make(map[string]*Output) if len(c1.Outputs) > 0 || len(c2.Outputs) > 0 {
for k, v := range c1.Outputs { c.Outputs = make([]*Output, 0, len(c1.Outputs)+len(c2.Outputs))
c.Outputs[k] = v 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)
} }
for k, v := range c2.Outputs {
c.Outputs[k] = v
} }
// Merge provider configs: If they collide, we just take the latest one // Merge provider configs: If they collide, we just take the latest one