Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer allowed() values for parameters #5046

Open
afscrome opened this issue Nov 1, 2021 · 3 comments
Open

Infer allowed() values for parameters #5046

afscrome opened this issue Nov 1, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@afscrome
Copy link
Contributor

afscrome commented Nov 1, 2021

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.

@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

@afscrome afscrome added the enhancement New feature or request label Nov 1, 2021
@ghost ghost added the Needs: Triage 🔍 label Nov 1, 2021
@alex-frankel
Copy link
Collaborator

Related to #4158, but it would be interesting to leverage something similar keyof in TypeScript.

@shenglol
Copy link
Contributor

Adding an example showing how keyof can be used:

param environment keyof evnShortNameMap // 'Prod' | 'NonProd'

var envShortNameMap = {
  Prod: 'pd'
  NonProd: 'np'
}

var envShortName = envShortNameMap[environment]

@brwilkinson
Copy link
Collaborator

Assuming this could also work with:

var envShortNameMap = json(loadTextContent('environmentInfo.json'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants