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

Add groupBy & keys functions #13560

Closed
anthony-c-martin opened this issue Mar 8, 2024 · 0 comments · Fixed by #13658
Closed

Add groupBy & keys functions #13560

anthony-c-martin opened this issue Mar 8, 2024 · 0 comments · Fixed by #13658
Labels
enhancement New feature or request

Comments

@anthony-c-martin
Copy link
Member

Looking at #13555, I would like to be able to offer the following workaround, using some made up functions (groupBy & keys). Feels like these would be useful capabilities:

var grouped = groupBy(policyAssignments, x => x.policyDefinitionId)
var policyDefinitionIds = keys(grouped)

resource tenantPolicyDefinitions 'Microsoft.Authorization/policyDefinitions@2023-04-01' existing = [for policyDefinitionId in policyDefinitionIds: {
  name: policyDefinitionId
  scope: tenant()
}]

resource policyAssignmentsRes 'Microsoft.Authorization/policyAssignments@2023-04-01' = [for policyAssignment in policyAssignments: {
  name: policyAssignment.id
  location: deployment().location
  properties: {
    displayName: policyAssignment.name
    policyDefinitionId: tenantPolicyDefinitions[indexOf(policyDefinitionIds, policyAssignment.policyDefinitionId)].id
    metadata: {}
    parameters: policyAssignment.parameters
  }
}]
@anthony-c-martin anthony-c-martin added the enhancement New feature or request label Mar 8, 2024
@stephaniezyen stephaniezyen added this to the Committed Backlog milestone Mar 20, 2024
anthony-c-martin added a commit that referenced this issue Apr 23, 2024
… on lambdas (#13658)

Adds the spread operator `...` as well as various new functions +
indexes on lambdas:
1. Spread operator - usage is as follows:
    * In an object:
        ```bicep
        var objA = { bar: 'bar' }
var objB = { foo: 'foo', ...objA } // equivalent to { foo: 'foo', bar:
'bar' }
        ```
    * In an array:
        ```bicep
        var arrA = [ 2, 3 ]
        var arrB = [ 1, ...arrA, 4 ] // equivalent to [ 1, 2, 3, 4 ]
        ```
1. New functions + usage:
    * `objectKeys`: Returns the keys of an object parameter:
        ```bicep
        var example = objectKeys({ a: 1, b: 2 }) // returns [ 'a', 'b' ]
        ```
* `mapValues`: Create an object from an input object, using a custom
lambda to map values:
        ```bicep
var example = mapValues({ foo: 'foo' }, val => toUpper(val)) // returns
{ foo: 'FOO' }
        ```
* `groupBy`: Create an object with array values from an array, using a
grouping condition:
        ```bicep
var example = groupBy(['foo', 'bar', 'baz'], x => substring(x, 0, 1)) //
returns { f: [ 'foo' ], b: [ 'bar', 'baz' ]
        ```
* `shallowMerge`: Perform a shallow merge of input object parameters:
        ```bicep
var example = shallowMerge([{ foo: 'foo' }, { bar: 'bar' }]) // returns
{ foo: 'foo', bar: 'bar' }
        ```
1. Optional indices on lambdas + usage:
    * `map`:
        ```bicep
var example = map(['a', 'b'], (x, i) => { index: i, val: x }) // returns
[ { index: 0, val: 'a' }, { index: 1 val: 'b' } ]
        ```
    * `reduce`:
        ```bicep
var example = reduce([ 2, 3, 7 ], (cur, next, i) => (i % 2 == 0) ? cur +
next : cur) // returns 9
        ```
    * `filter`:
        ```bicep
var example = filter([ 'foo', 'bar', 'baz' ], (val, i) => i < 2 &&
substring(val, 0, 1) == 'b') // returns [ 'bar' ]
        ```
Closes #13560
Closes #9244
Closes #1560
Addresses some of the issues described under the following: #2082,
#1853, #387

###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/13658)
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
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants