mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-27 08:56:25 -06:00
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:
parent
8f6811da0c
commit
e331f05870
@ -90,7 +90,9 @@ func Variables(
|
|||||||
|
|
||||||
switch varType {
|
switch varType {
|
||||||
case config.VariableTypeMap:
|
case config.VariableTypeMap:
|
||||||
varSetMap(result, k, varVal)
|
if err := varSetMap(result, k, varVal); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
result[k] = varVal
|
result[k] = varVal
|
||||||
}
|
}
|
||||||
@ -108,7 +110,9 @@ func Variables(
|
|||||||
case config.VariableTypeList:
|
case config.VariableTypeList:
|
||||||
result[k] = v
|
result[k] = v
|
||||||
case config.VariableTypeMap:
|
case config.VariableTypeMap:
|
||||||
varSetMap(result, k, v)
|
if err := varSetMap(result, k, v); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case config.VariableTypeString:
|
case config.VariableTypeString:
|
||||||
// Convert to a string and set. We don't catch any errors
|
// Convert to a string and set. We don't catch any errors
|
||||||
// here because the validation step later should catch
|
// 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
|
// 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
|
// "current" set of variables. This is just a private function to remove
|
||||||
// duplicate logic in Variables
|
// 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]
|
existing, ok := current[k]
|
||||||
if !ok {
|
if !ok {
|
||||||
current[k] = v
|
current[k] = v
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
existingMap, ok := existing.(map[string]interface{})
|
existingMap, ok := existing.(map[string]interface{})
|
||||||
if !ok {
|
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) {
|
switch typedV := v.(type) {
|
||||||
@ -156,6 +160,7 @@ func varSetMap(current map[string]interface{}, k string, v interface{}) {
|
|||||||
existingMap[newKey] = newVal
|
existingMap[newKey] = newVal
|
||||||
}
|
}
|
||||||
default:
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user