@@ -19,25 +19,30 @@ shallowMerge(<inputArray>)
1919
2020The ` shallowMerge() ` function takes an array of objects and combines them into a single
2121object by merging their properties. When the same property name appears in multiple objects,
22- the value from the last object in the array takes precedence.
22+ the value from the last object in the array with that property takes precedence.
2323
24- This is a ** shallow merge ** , meaning :
24+ This is a _ shallow merge _ , which applies the following rules :
2525
26- - Top-level properties are merged from all objects
27- - If a property value is an object, it replaces the entire object from previous objects
28- rather than merging the nested properties
29- - Arrays and other complex types are also replaced entirely, not combined
26+ - The first object in the array defines the base value for the merged object.
27+ - The function processes each object in the array in the order they're defined.
28+ - When processing each object, the function iterates over every top-level property defined for that
29+ object and:
30+
31+ - If the merged object doesn't already have the property, the function adds that property to the
32+ merged object with the value from the current object.
33+ - If the merged object does have the property, the function _ replaces_ the existing value with
34+ the value from the current object, even when the value is an object or array.
3035
3136This function is useful for:
3237
33- - Building composite configuration objects from multiple sources
34- - Applying configuration overrides where later values take precedence
35- - Combining default settings with user-specified customizations
36- - Merging environment-specific configurations
38+ - Building composite configuration objects from multiple sources.
39+ - Applying configuration overrides where later values take precedence.
40+ - Combining default settings with user-specified customizations.
41+ - Merging environment-specific configurations.
3742
38- The shallow merge behavior differs from a deep merge (like [ ` union() ` ] [ 00 ] ) where nested
39- objects would be recursively merged. With ` shallowMerge() ` , nested structures are replaced
40- entirely by the last object's value .
43+ The shallow merge behavior differs from a deep merge (like [ ` union() ` ] [ 00 ] ) where nested
44+ objects are recursively merged. The ` shallowMerge() ` function replaces nested structures
45+ entirely with the value defined by the last object with that property in the input array .
4146
4247## Examples
4348
@@ -53,7 +58,13 @@ resources:
5358- name : Echo
5459 type : Microsoft.DSC.Debug/Echo
5560 properties :
56- output : " [shallowMerge(createArray(createObject('host', 'localhost', 'port', 8080), createObject('port', 9000, 'ssl', true())))]"
61+ output : >-
62+ [shallowMerge(
63+ createArray(
64+ createObject('host', 'localhost', 'port', 8080),
65+ createObject('port', 9000, 'ssl', true())
66+ )
67+ )]
5768` ` `
5869
5970` ` ` bash
@@ -74,9 +85,8 @@ messages: []
7485hadErrors : false
7586` ` `
7687
77- Notice how the ` port` value from the second object (9000) replaces the value from the first
78- object (8080), while properties that only exist in one object (`host` and `ssl`) are
79- preserved.
88+ In this example, the ` port` value from the second object (`9000`) replaces the value from the first
89+ object (`8080`), while properties that only exist in one object (`host` and `ssl`) are preserved.
8090
8191# ## Example 2 - Apply multiple configuration layers
8292
@@ -105,7 +115,14 @@ resources:
105115- name: Echo
106116 type: Microsoft.DSC.Debug/Echo
107117 properties:
108- output: "[shallowMerge(createArray(parameters('defaults'), parameters('environment'), parameters('userPrefs')))]"
118+ output: >-
119+ [shallowMerge(
120+ createArray(
121+ parameters('defaults'),
122+ parameters('environment'),
123+ parameters('userPrefs')
124+ )
125+ )]
109126` ` `
110127
111128` ` ` bash
@@ -141,7 +158,19 @@ resources:
141158- name: Echo
142159 type: Microsoft.DSC.Debug/Echo
143160 properties:
144- output: "[shallowMerge(createArray(createObject('database', createObject('host', 'localhost', 'port', 5432, 'ssl', true())), createObject('database', createObject('host', 'prod.db.local'))))]"
161+ output: >-
162+ [shallowMerge(
163+ createArray(
164+ createObject(
165+ 'database',
166+ createObject('host', 'localhost', 'port', 5432, 'ssl', true())
167+ ),
168+ createObject(
169+ 'database',
170+ createObject('host', 'prod.db.local')
171+ )
172+ )
173+ )]
145174` ` `
146175
147176` ` ` bash
@@ -175,7 +204,14 @@ resources:
175204- name: Echo
176205 type: Microsoft.DSC.Debug/Echo
177206 properties:
178- output: "[shallowMerge(createArray(createObject('name', 'Service1', 'enabled', true()), createObject(), createObject('version', '2.0')))]"
207+ output: >-
208+ [shallowMerge(
209+ createArray(
210+ createObject('name', 'Service1', 'enabled', true()),
211+ createObject(),
212+ createObject('version', '2.0')
213+ )
214+ )]
179215` ` `
180216
181217` ` ` bash
@@ -210,7 +246,13 @@ resources:
210246- name: Echo
211247 type: Microsoft.DSC.Debug/Echo
212248 properties:
213- output: "[shallowMerge(createArray(createObject('newUI', false(), 'darkMode', true(), 'beta', false()), createObject('newUI', true()), createObject('beta', true())))]"
249+ output: >-
250+ [shallowMerge(
251+ createArray(
252+ createObject('newUI', false(), 'darkMode', true(), 'beta', false()),
253+ createObject('newUI', true()), createObject('beta', true())
254+ )
255+ )]
214256` ` `
215257
216258` ` ` bash
@@ -302,9 +344,34 @@ resources:
302344 type: Microsoft.DSC.Debug/Echo
303345 properties:
304346 output:
305- merged: "[shallowMerge(createArray(parameters('baseConfig'), parameters('overrides')))]"
306- keys: "[objectKeys(shallowMerge(createArray(parameters('baseConfig'), parameters('overrides'))))]"
307- hasRetries: "[contains(objectKeys(shallowMerge(createArray(parameters('baseConfig'), parameters('overrides')))), 'retries')]"
347+ merged: >-
348+ [shallowMerge(
349+ createArray(
350+ parameters('baseConfig'),
351+ parameters('overrides')
352+ )
353+ )]
354+ keys: >-
355+ [objectKeys(
356+ shallowMerge(
357+ createArray(
358+ parameters('baseConfig'),
359+ parameters('overrides')
360+ )
361+ )
362+ )]
363+ hasRetries: >-
364+ [contains(
365+ objectKeys(
366+ shallowMerge(
367+ createArray(
368+ parameters('baseConfig'),
369+ parameters('overrides')
370+ )
371+ )
372+ ),
373+ 'retries'
374+ )]
308375` ` `
309376
310377` ` ` bash
@@ -381,8 +448,9 @@ Position: 1
381448
382449# # Output
383450
384- Returns a single object containing all properties from the input objects. When the same
385- property appears in multiple objects, the value from the last object in the array is used.
451+ Returns a single object containing all properties from the input objects. When the same property
452+ appears in multiple objects, the value from the last object in the array with that property is
453+ retained, replacing all prior values for the property.
386454
387455` ` ` yaml
388456Type: object
@@ -396,13 +464,15 @@ The function will return an error in the following cases:
396464
397465# # Notes
398466
399- - This is a **shallow merge** - nested objects are replaced, not merged recursively
400- - Properties from objects later in the array override properties from earlier objects
401- - Empty objects in the array don't affect the merge
402- - Non-object elements in the array are ignored
403- - An empty array returns an empty object
404- - The function processes objects in array order, so the last object has highest precedence
405- - For recursive/deep merging of nested objects, consider using [`union()`][00] instead
467+ - This function performs a _shallow merge_ - the function replaces nested objects, it doesn't merge
468+ them recursively.
469+ - The function replaces the value for properties defined by earlier objects in the input array with
470+ the value from objects later in the array.
471+ - The function ignores empty objects in the input array.
472+ - The function ignores non-object elements in the input array.
473+ - The function returns an empty object when the input is an empty array.
474+ - The function processes objects in array order, so the last object has highest precedence
475+ - For recursive/deep merging of nested objects, consider using [`union()`][00] instead.
406476
407477# # Related functions
408478
0 commit comments