config: change Default to an interface{}

This commit is contained in:
Mitchell Hashimoto 2014-07-21 07:32:36 -07:00
parent 9c164c6015
commit b0ce89b805
4 changed files with 13 additions and 24 deletions

View File

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

View File

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

View File

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

View File

@ -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"},
},