template: variable parsing

This commit is contained in:
Mitchell Hashimoto 2015-05-21 13:40:33 -06:00
parent 4583ed6108
commit fbda5b119a
4 changed files with 54 additions and 0 deletions

View File

@ -30,6 +30,26 @@ func (r *rawTemplate) Template() (*Template, error) {
var result Template
var errs error
// Gather the variables
if len(r.Variables) > 0 {
result.Variables = make(map[string]*Variable, len(r.Variables))
}
for k, rawV := range r.Variables {
var v Variable
// Variable is required if the value is exactly nil
v.Required = rawV == nil
// Weak decode the default if we have one
if err := r.decoder(&v.Default, nil).Decode(rawV); err != nil {
errs = multierror.Append(errs, fmt.Errorf(
"variable %s: %s", k, err))
continue
}
result.Variables[k] = &v
}
// Let's start by gathering all the builders
if len(r.Builders) > 0 {
result.Builders = make(map[string]*Builder, len(r.Builders))

View File

@ -117,6 +117,30 @@ func TestParse(t *testing.T) {
nil,
true,
},
{
"parse-variable-default.json",
&Template{
Variables: map[string]*Variable{
"foo": &Variable{
Default: "foo",
},
},
},
false,
},
{
"parse-variable-required.json",
&Template{
Variables: map[string]*Variable{
"foo": &Variable{
Required: true,
},
},
},
false,
},
}
for _, tc := range cases {

View File

@ -0,0 +1,5 @@
{
"variables": {
"foo": "foo"
}
}

View File

@ -0,0 +1,5 @@
{
"variables": {
"foo": null
}
}