Bicep 안전 역참조 연산자

안전 역참조 연산자는 안전한 방식으로 배열의 개체 또는 요소 속성에 액세스하는 방법을 제공합니다. 존재 또는 값에 대한 적절한 지식 없이 속성 또는 요소에 액세스하려고 할 때 발생할 수 있는 오류를 방지하는 데 도움이 됩니다.

safe-dereference

<base>.?<property> <base>[?<index>]

안전 역참조 연산자는 해당 피연산자가 null이 아닌 것으로 평가되는 경우에만 멤버 액세스 .?<property> 또는 요소 액세스 [?<index>], 작업을 해당 피연산자에 적용할 수 있습니다. 그렇지 않으면 null을 반환합니다. 즉, 다음과 같습니다.

  • anull로 평가되면 a.?x 또는 a[?x]의 결과는 null입니다.
  • ax 속성이 없는 개체인 경우 a.?xnull입니다.
  • a가 길이가 x보다 작거나 같은 배열인 경우 a[?x]null입니다.
  • a가 null이 아니고 x라는 속성이 있는 경우 a.?x의 결과는 a.x의 결과와 동일합니다.
  • a가 null이 아니고 인덱스 x에 요소가 있는 경우 a[?x]의 결과는 a[x]의 결과와 동일합니다.

안전 역참조 연산자는 단락입니다. 즉 조건부 멤버나 요소 액세스 작업의 한 체인의 작업에서 null을 반환하면 나머지 체인은 실행되지 않습니다. 다음 예제에서는 storageAccountsettings[?i]null로 평가되면 .?name이 평가되지 않습니다.

param storageAccountSettings array = []
param storageCount int
param location string = resourceGroup().location

resource storage 'Microsoft.Storage/storageAccounts@2022-09-01' = [for i in range(0, storageCount): {
  name: storageAccountSettings[?i].?name ?? 'defaultname'
  location: storageAccountSettings[?i].?location ?? location
  kind: storageAccountSettings[?i].?kind ?? 'StorageV2'
  sku: {
    name: storageAccountSettings[?i].?sku ?? 'Standard_GRS'
  }
}]

다음 단계