Skip to content

Commit 2688ec5

Browse files
committed
arm/bicep shallowMerge function
1 parent e6aec10 commit 2688ec5

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
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 are not deeply merged; instead, they are 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 does not 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, ...)`

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

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

124125
## 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 are not deeply merged; instead, they are 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 does not 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
@@ -177,6 +177,7 @@ Resource Manager provides several functions for working with objects.
177177
* [json](template-functions-object.md#json)
178178
* [length](template-functions-object.md#length)
179179
* [null](template-functions-object.md#null)
180+
* [shallowMerge](template-functions-object.md#shallowmerge)
180181
* [union](template-functions-object.md#union)
181182

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

0 commit comments

Comments
 (0)