Allow map variables from json

A JSON object will be decoded as a list with a single map value. This
will be properly coerced later, so let it through the initial config
semantic checks.
This commit is contained in:
James Bardin 2016-09-27 13:04:40 -04:00
parent e12f42a8b5
commit 3297a460c7
3 changed files with 28 additions and 13 deletions

View File

@ -108,31 +108,26 @@ func smcUserVariables(c *config.Config, vs map[string]interface{}) []error {
switch proposedValue.(type) { switch proposedValue.(type) {
case string: case string:
continue continue
default:
errs = append(errs, fmt.Errorf("variable %s should be type %s, got %s",
name, declaredType.Printable(), hclTypeName(proposedValue)))
} }
case config.VariableTypeMap: case config.VariableTypeMap:
switch proposedValue.(type) { switch v := proposedValue.(type) {
case map[string]interface{}: case map[string]interface{}:
continue continue
default: case []map[string]interface{}:
errs = append(errs, fmt.Errorf("variable %s should be type %s, got %s", // if we have a list of 1 map, it will get coerced later as needed
name, declaredType.Printable(), hclTypeName(proposedValue))) if len(v) == 1 {
continue
}
} }
case config.VariableTypeList: case config.VariableTypeList:
switch proposedValue.(type) { switch proposedValue.(type) {
case []interface{}: case []interface{}:
continue continue
default: }
}
errs = append(errs, fmt.Errorf("variable %s should be type %s, got %s", errs = append(errs, fmt.Errorf("variable %s should be type %s, got %s",
name, declaredType.Printable(), hclTypeName(proposedValue))) name, declaredType.Printable(), hclTypeName(proposedValue)))
} }
default:
errs = append(errs, fmt.Errorf("variable %s should be type %s, got %s",
name, declaredType.Printable(), hclTypeName(proposedValue)))
}
}
// TODO(mitchellh): variables that are unknown // TODO(mitchellh): variables that are unknown

View File

@ -38,3 +38,20 @@ func TestSMCUserVariables(t *testing.T) {
} }
} }
func TestSMCUserVariables_mapFromJSON(t *testing.T) {
c := testConfig(t, "uservars-map")
// ensure that a single map in a list can satisfy a map variable, since it
// will be coerced later to a map
err := smcUserVariables(c, map[string]interface{}{
"test_map": []map[string]interface{}{
map[string]interface{}{
"foo": "bar",
},
},
})
if err != nil {
t.Fatal(err)
}
}

View File

@ -0,0 +1,3 @@
variable "test_map" {
type = "map"
}