Infer `allowed()` values for parameters
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 79
Description
**Is your feature request related to a problem? Please describe.**
I often find myself implementing the following pattern in bicep - an input parameter and a map to convert it to another value. In this scenario the values in the `@allowed()` attribute and the properties on the map need to remain in sync.
```bicep
@allowed([
'Prod'
'NonProd'
])
param environment string
var envShortNameMap = {
Prod: 'pd'
NonProd: 'np'
}
var envShortName = envShortNameMap[environment]
```
This pattern mostly works fine, until adding a new value - it's too easy to update either the parameter allow list or the map without updating hte other.
**Describe the solution you'd like**
It would be really nice if bicep could automatically infer the `@allowed()` values for the `environment` input string. This would reduce duplication in the code, ensuring the values remained in sync.
Fixing #5045 would partially help resolve some of the pain of keeping values in sync as it should at least result in a compiler error if I updated the `@allowed()` attribute but forgot to update `envShortNameMap`, but it wouldn't help if I updated `envShortNameMap` without updating the `@allowed()` attribute
Contributor guide
Research direction
No source files or tests are named. Start by tracing how Bicep handles @allowed(...) on a parameter and map property access, then determine the inference behavior for the Prod/NonProd example. Done means the allowed values remain synchronized without duplicate declarations.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100