-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Allow {} to be coerced to object of any type #33303
Copy link
Copy link
Closed
Labels
Description
Terraform Version
Terraform v1.3.9
on darwin_amd64Use Cases
I would like to be able to provide an empty object value that can be used in code rather than have to use conditionals in multiple places.
Attempted Solutions
What I would like to work:
This is, of course, a simplified example. In practice, the objects are more complex, and local.enabled is further refined in additional statements.
locals {
coin_flip = false // random_integer.coin.result > 1
# Simulate a data source with
# count = local.coin_flip ? 1 : 0
datasource = local.coin_flip ? null : {
enabled = true
obj = {
disabled = {
enabled = false
list = []
}
enabled = {
enabled = true
}
}
}
obj = local.coin_flip ? {} : local.datasource.obj
enabled = { for k, v in local.obj : k => v if v.enabled }
}
output "grr" {
value = local.enabled
}But it fails:
$ terraform apply
╷
│ Error: Inconsistent conditional result types
│
│ on objects.tf line 18, in locals:
│ 18: obj = local.coin_flip ? {} : local.datasource.obj
│ ├────────────────
│ │ local.coin_flip is false
│ │ local.datasource.obj is object with 2 attributes
│
│ The true and false result expressions must have consistent types. The 'false'
│ value includes object attribute "disabled", which is absent in the 'true'
│ value.
╵
What I have had to resort to:
// Workaround for inconsistent types in ternary
// obj = local.coin_flip ? {} : local.datasource.obj
obj_map = {
true = {}
false = local.coin_flip ? null : local.datasource.obj
}
obj = local.obj_map[local.coin_flip]Proposal
Allow {} to be cast to map or any type of object.
References
Reactions are currently unavailable