Commit fbda5b11 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

template: variable parsing

parent 4583ed61
......@@ -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))
......
......@@ -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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment