Azure / Azure/bicep

Format Function use with an array of Strings

Open
#7,893 2 comments 1 reaction 0 assignees View on GitHub
enhancement intermediate language
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 21m
Merged PRs (30d)
79

Description

Trying to write a generic module where you can pass in an array of strings to format another string based on environment being deployed to, but keep getting input string not incorrect format.

Question:
1. Does the format function take an array?
2. Does the input string have to have formatting?

Here is the actual scenario:
1. Parameter File would contain the following:
```json
"appConfigurationKey": "Some config key",
"appConfigurationValue": "https://{est.com/{0}",
"appConfigurationValueContentType": null,
"appConfigurationName": "app config name",
"appConfigurationResourceGroup": "app config resource group",
"appConfigurationValueEnvReplacements": {
"dev": [
"dev value"
],
"qa": [
"qa value"
],
"uat": [
"uat value"
],
"prd": [
"prd value"
]
}
```

```bicep
@allowed([
''
'dev'
'qa'
'uat'
'prd'
])
@description('The environment in which the resource(s) will be deployed')
param environment string = ''

@description('The region prefix or suffix for the resource name, if applicable.')
param region string = ''

@description('The name of the app configuration')
param appConfigurationName string

@description('The name of the Key Vaule')
param appConfigurationKey string

@secure()
@description('The value of the App configuration to create or update')
param appConfigurationValue string

@description('Labels to append to App Configuration Key')
param appConfigurationLabels array = []

@description('The content type of the configuration value')
param appConfigurationContentType string = ''

@description('')
param appConfigurationValueEnvReplacements object = {}

// if empty then yes (env agnostic config value) or
var appConfigReplacements = !empty(appConfigurationValueEnvReplacements) ? appConfigurationValueEnvReplacements : {
dev: []
qa: []
uat: []
prd: []
}
var appConfig = environment == 'dev' ? {
value: format(appConfigurationValue, appConfigReplacements.dev)
} : environment == 'qa' ? {
value: format(appConfigurationValue, appConfigReplacements.qa)
} : environment == 'uat' ? {
value: format(appConfigurationValue, appConfigReplacements.uat)
} : environment == 'prd' ? {
value: format(appConfigurationValue, appConfigReplacements.dev)
} : {
value: appConfigurationValue
}

resource azAppConfigurationKeyValuesDeployment 'Microsoft.AppConfiguration/configurationStores/keyValues@2022-05-01' = if (empty(appConfigurationLabels) || contains(appConfigurationLabels, 'default')) {
name: replace(replace('${appConfigurationName}/${appConfigurationKey}', '@environment', environment), '@region', region)
properties: {
value: replace(replace(appConfig.value, '@environment', environment), '@region', region)
contentType: empty(appConfigurationContentType) ? json('null') : appConfigurationContentType
}
}

// Deploys the same configuration value with different labels
resource azAppConfigurationKeyValuesWithLabelsDeployment 'Microsoft.AppConfiguration/configurationStores/keyValues@2022-05-01' = [for label in appConfigurationLabels: if (label != 'default' && !empty(appConfigurationLabels)) {
name: replace(replace('${appConfigurationName}/${appConfigurationKey}$${label}', '@environment', environment), '@region', region)
properties: {
value: replace(replace(appConfig.value, '@environment', environment), '@region', region)
contentType: empty(appConfigurationContentType) ? json('null') : appConfigurationContentType
}
}]

```

Contributor guide

Open the contributing guide

Research direction

Start with the Bicep format function and the supplied template; reproduce the reported incorrect-format error using the shown appConfigurationValue and replacement arrays. Confirm whether arrays are supported and whether the example’s formatting syntax is valid, then document the expected behavior or identify the precise correction needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.