Merge pull request #10199 from hashicorp/jbardin/GH-10155

Catch map type errors on variable assignment
This commit is contained in:
James Bardin 2016-11-21 09:13:16 -05:00 committed by GitHub
commit 5108182690
2 changed files with 23 additions and 6 deletions

View File

@ -90,7 +90,9 @@ func Variables(
switch varType {
case config.VariableTypeMap:
varSetMap(result, k, varVal)
if err := varSetMap(result, k, varVal); err != nil {
return nil, err
}
default:
result[k] = varVal
}
@ -108,7 +110,9 @@ func Variables(
case config.VariableTypeList:
result[k] = v
case config.VariableTypeMap:
varSetMap(result, k, v)
if err := varSetMap(result, k, v); err != nil {
return nil, err
}
case config.VariableTypeString:
// Convert to a string and set. We don't catch any errors
// here because the validation step later should catch
@ -134,16 +138,16 @@ func Variables(
// varSetMap sets or merges the map in "v" with the key "k" in the
// "current" set of variables. This is just a private function to remove
// duplicate logic in Variables
func varSetMap(current map[string]interface{}, k string, v interface{}) {
func varSetMap(current map[string]interface{}, k string, v interface{}) error {
existing, ok := current[k]
if !ok {
current[k] = v
return
return nil
}
existingMap, ok := existing.(map[string]interface{})
if !ok {
panic(fmt.Sprintf("%s is not a map, this is a bug in Terraform.", k))
panic(fmt.Sprintf("%q is not a map, this is a bug in Terraform.", k))
}
switch typedV := v.(type) {
@ -156,6 +160,7 @@ func varSetMap(current map[string]interface{}, k string, v interface{}) {
existingMap[newKey] = newVal
}
default:
panic(fmt.Sprintf("%s is not a map, this is a bug in Terraform.", k))
return fmt.Errorf("variable %q should be type map, got %s", k, hclTypeName(v))
}
return nil
}

View File

@ -136,6 +136,18 @@ func TestVariables(t *testing.T) {
"b": "1",
},
},
"override map with string": {
"vars-basic",
map[string]string{
"TF_VAR_c": `{"foo" = "a", "bar" = "baz"}`,
},
map[string]interface{}{
"c": "bar",
},
true,
nil,
},
}
for name, tc := range cases {