Return an error for setting a non-map to a map

Setting variables happens before context validation, so it's possible
that the user could be trying to set an incorrect variable type to a
map. Return a useful error rather than panicking.
This commit is contained in:
James Bardin 2016-11-17 09:52:01 -05:00
parent 8f6811da0c
commit e331f05870
1 changed files with 11 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
}