hashicorp / hashicorp/web-unified-docs
Unquoted integer keys in Map/Object will experience leading zero truncation
- Dominant language
- MDX
- Stars
- 86
- Forks
- 275
- Avg merge
- 3d 18h
- Merged PRs (30d)
- 199
Description
Based on documentation [here](https://www.terraform.io/docs/language/expressions/types.html#more-about-complex-types) and [here](https://www.terraform.io/docs/language/expressions/type-constraints.html) the keys for a map/object must be strings and if the key begins with a number it must be quoted. However the existence of unquoted numeric keys does not generate an error or warning and the numeric keys are currently accepted.
A complication arises when unquoted integers are provided as keys with the intent that they should be treated as string literals (as the documentation for maps/objects indicates that the keys are strings). If the integer has leading zeroes then they will be truncated and the key in the map/object will represent the truncated value even though in the terraform config it shows with a leading zero.
Technically this could be considered user error as the documentation does state that quotes must be used in this case when the key starts with a digit. Due to the confusion this could cause I would recommend also generating a warning or error when using unquoted numbers as keys.
### Terraform Version
```
Terraform v0.15.2
on linux_amd64
```
### Terraform Configuration Files
```terraform
locals {
foo = {
123 = "abc"
012 = "def"
}
}
output "a" {
value = lookup(local.foo, "012")
}
```
### Expected Behavior
```
Plan: 0 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ a = "def"
```
### Actual Behavior
```
╷
│ Error: Invalid function argument
│
│ on main.tf line 10, in output "a":
│ 10: value = lookup(local.foo, "012")
│ ├────────────────
│ │ local.foo is object with 2 attributes
│
│ Invalid value for "inputMap" parameter: the given object has no attribute "012".
╵
```
### Steps to Reproduce
1. Create main.tf with the above contents
2. Run terraform plan with either the 123 or 012 keys in the lookup
Contributor guide
Research direction
Start with the linked complex-types and type-constraints documentation, then reproduce the behavior using the Terraform configuration and lookup steps in the report. Confirm whether the intended outcome is a warning or error for unquoted numeric keys, or corrected lookup behavior, and document the chosen behavior when the issue is resolved.
Written by the indexing model from the issue text.
Assessment
- Domain
- documentation
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100