Skip to content

Commit de3ad6c

Browse files
Merge pull request #275008 from mumian/0510-function-shallowMerge
arm/bicep shallowMerge function
2 parents d66f0af + 57564e7 commit de3ad6c

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

articles/azure-resource-manager/bicep/bicep-functions-object.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,47 @@ The output from the preceding example with the default values is:
385385
| stringLength | Int | 13 |
386386
| objectLength | Int | 4 |
387387

388+
## shallowMerge
389+
390+
`shallowMerge(inputArray)`
391+
392+
Combines an array of objects, where only the top-level objects are merged. This means that if the objects being merged contain nested objects, those nested object aren't deeply merged; instead, they're replaced entirely by the corresponding property from the merging object.
393+
394+
Namespace: [sys](bicep-functions.md#namespaces-for-functions).
395+
396+
### Parameters
397+
398+
| Parameter | Required | Type | Description |
399+
|:--- |:--- |:--- |:--- |
400+
| inputArray |Yes |array |An array of objects. |
401+
402+
### Return value
403+
404+
An object.
405+
406+
### Example
407+
408+
The following example shows how to use `shallowMerge`:
409+
410+
```bicep
411+
var firstArray = [{ one: 'a' }, { two: 'b' }, { two: 'c'}]
412+
var secondArray = [{ one: 'a', nested: {a: 1, nested: {c: 3}} }, { two: 'b', nested: {b: 2}}]
413+
414+
output firstOutput object = shallowMerge(firstArray)
415+
output secondOutput object = shallowMerge(secondArray)
416+
```
417+
418+
The output from the preceding example with the default values is:
419+
420+
| Name | Type | Value |
421+
| ---- | ---- | ----- |
422+
| firstOutput | object | {"one":"a","two":"c"}|
423+
| secondOutput | object | {"one":"a","nested":{"b":2},"two":"b"} |
424+
425+
**firstOutput** shows the properties from the merging objects are combined into a new object. If there are conflicting properties (i.e., properties with the same name), the property from the last object being merged usually takes precedence.
426+
427+
**secondOutput** shows the shallow merge doesn't recursively merge these nested objects. Instead, the entire nested object is replaced by the corresponding property from the merging object.
428+
388429
## union
389430

390431
`union(arg1, arg2, arg3, ...)`
@@ -413,7 +454,7 @@ For arrays, the function iterates through each element in the first parameter an
413454

414455
For objects, property names and values from the first parameter are added to the result. For later parameters, any new names are added to the result. If a later parameter has a property with the same name, that value overwrites the existing value. The order of the properties isn't guaranteed.
415456

416-
The union function merges not only the top-level elements but also recursively merges any nested objects within them. Nested array values are not merged. See the second example in the following section.
457+
The union function merges not only the top-level elements but also recursively merges any nested objects within them. Nested array values aren't merged. See the second example in the following section.
417458

418459
### Example
419460

articles/azure-resource-manager/bicep/bicep-functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ The following functions are available for working with objects. All of these fun
120120
* [items](./bicep-functions-object.md#items)
121121
* [json](./bicep-functions-object.md#json)
122122
* [length](./bicep-functions-object.md#length)
123+
* [shallowMerge](./bicep-functions-object.md#shallowmerge)
123124
* [union](./bicep-functions-object.md#union)
124125

125126
## Parameters file functions

articles/azure-resource-manager/templates/template-functions-object.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,87 @@ The output from the preceding example is:
409409
| ---- | ---- | ----- |
410410
| emptyOutput | Bool | True |
411411

412+
## shallowMerge
413+
414+
`shallowMerge(inputArray)`
415+
416+
Combines an array of objects, where only the top-level objects are merged. This means that if the objects being merged contain nested objects, those nested object aren't deeply merged; instead, they're replaced entirely by the corresponding property from the merging object.
417+
418+
In Bicep, use the [shallowMerge](../bicep/bicep-functions-object.md#shallowmerge) function.
419+
420+
### Parameters
421+
422+
| Parameter | Required | Type | Description |
423+
|:--- |:--- |:--- |:--- |
424+
| inputArray |Yes |array |An array of objects. |
425+
426+
### Return value
427+
428+
An object.
429+
430+
### Example
431+
432+
The following example shows how to use `shallowMerge`:
433+
434+
```json
435+
{
436+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
437+
"contentVersion": "1.0.0.0",
438+
"variables": {
439+
"firstArray": [
440+
{
441+
"one": "a"
442+
},
443+
{
444+
"two": "b"
445+
},
446+
{
447+
"two": "c"
448+
}
449+
],
450+
"secondArray": [
451+
{
452+
"one": "a",
453+
"nested": {
454+
"a": 1,
455+
"nested": {
456+
"c": 3
457+
}
458+
}
459+
},
460+
{
461+
"two": "b",
462+
"nested": {
463+
"b": 2
464+
}
465+
}
466+
]
467+
},
468+
"resources": [],
469+
"outputs": {
470+
"firstOutput": {
471+
"type": "object",
472+
"value": "[shallowMerge(variables('firstArray'))]"
473+
},
474+
"secondOutput": {
475+
"type": "object",
476+
"value": "[shallowMerge(variables('secondArray'))]"
477+
}
478+
}
479+
}
480+
```
481+
482+
The output from the preceding example with the default values is:
483+
484+
| Name | Type | Value |
485+
| ---- | ---- | ----- |
486+
| firstOutput | object | {"one":"a","two":"c"}|
487+
| secondOutput | object | {"one":"a","nested":{"b":2},"two":"b"} |
488+
489+
**firstOutput** shows the properties from the merging objects are combined into a new object. If there are conflicting properties (i.e., properties with the same name), the property from the last object being merged usually takes precedence.
490+
491+
**secondOutput** shows the shallow merge doesn't recursively merge these nested objects. Instead, the entire nested object is replaced by the corresponding property from the merging object.
492+
412493
## union
413494

414495
`union(arg1, arg2, arg3, ...)`

articles/azure-resource-manager/templates/template-functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Resource Manager provides several functions for working with objects.
217217
* [json](template-functions-object.md#json)
218218
* [length](template-functions-object.md#length)
219219
* [null](template-functions-object.md#null)
220+
* [shallowMerge](template-functions-object.md#shallowmerge)
220221
* [union](template-functions-object.md#union)
221222

222223
For Bicep files, use the [object](../bicep/bicep-functions-object.md) functions.

0 commit comments

Comments
 (0)