From b0ce89b80525f8c58b5102db5e4be6638d73d227 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 21 Jul 2014 07:32:36 -0700 Subject: [PATCH] config: change Default to an interface{} --- config/config.go | 8 +++----- config/loader_libucl.go | 11 +---------- config/loader_test.go | 14 +++++++------- config/merge_test.go | 4 ++-- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/config/config.go b/config/config.go index bb7ab76bf..2083c7fc7 100644 --- a/config/config.go +++ b/config/config.go @@ -53,9 +53,8 @@ type Provisioner struct { // Variable is a variable defined within the configuration. type Variable struct { Name string - Default string + Default interface{} Description string - defaultSet bool } // Output is an output defined within the configuration. An output is @@ -310,9 +309,8 @@ func (v *Variable) Merge(v2 *Variable) *Variable { // The names should be the same, but the second name always wins. result.Name = v2.Name - if v2.defaultSet { + if v2.Default != nil { result.Default = v2.Default - result.defaultSet = true } if v2.Description != "" { result.Description = v2.Description @@ -331,7 +329,7 @@ func (v *Variable) mergerMerge(m merger) merger { // Required tests whether a variable is required or not. func (v *Variable) Required() bool { - return !v.defaultSet + return v.Default == nil } func NewResourceVariable(key string) (*ResourceVariable, error) { diff --git a/config/loader_libucl.go b/config/loader_libucl.go index c6050d922..398c2f5fd 100644 --- a/config/loader_libucl.go +++ b/config/loader_libucl.go @@ -30,7 +30,7 @@ func (t *libuclConfigurable) Config() (*Config, error) { } type LibuclVariable struct { - Default string + Default interface{} Description string Fields []string `libucl:",decodedFields"` } @@ -51,19 +51,10 @@ func (t *libuclConfigurable) Config() (*Config, error) { if len(rawConfig.Variable) > 0 { config.Variables = make([]*Variable, 0, len(rawConfig.Variable)) for k, v := range rawConfig.Variable { - defaultSet := false - for _, f := range v.Fields { - if f == "Default" { - defaultSet = true - break - } - } - config.Variables = append(config.Variables, &Variable{ Name: k, Default: v.Default, Description: v.Description, - defaultSet: defaultSet, }) } } diff --git a/config/loader_test.go b/config/loader_test.go index 658e1c248..0cb8183cf 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -437,20 +437,20 @@ func variablesStr(vs []*Variable) string { for _, k := range ks { v := m[k] - if v.Default == "" { + required := "" + if v.Required() { + required = " (required)" + } + + if v.Default == nil || v.Default == "" { v.Default = "<>" } if v.Description == "" { v.Description = "<>" } - required := "" - if v.Required() { - required = " (required)" - } - result += fmt.Sprintf( - "%s%s\n %s\n %s\n", + "%s%s\n %v\n %s\n", k, required, v.Default, diff --git a/config/merge_test.go b/config/merge_test.go index fb52677b5..10ef5f37a 100644 --- a/config/merge_test.go +++ b/config/merge_test.go @@ -103,7 +103,7 @@ func TestMerge(t *testing.T) { &Resource{Name: "bar"}, }, Variables: []*Variable{ - &Variable{Name: "foo", Default: "bar", defaultSet: true}, + &Variable{Name: "foo", Default: "bar"}, &Variable{Name: "bar"}, }, @@ -124,7 +124,7 @@ func TestMerge(t *testing.T) { &Resource{Name: "bar"}, }, Variables: []*Variable{ - &Variable{Name: "foo", Default: "bar", defaultSet: true}, + &Variable{Name: "foo", Default: "bar"}, &Variable{Name: "foo"}, &Variable{Name: "bar"}, },