template: variable parsing
This commit is contained in:
parent
4583ed6108
commit
fbda5b119a
|
@ -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 {
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"variables": {
|
||||
"foo": "foo"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"variables": {
|
||||
"foo": null
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue