Skip to content

Commit 889d3dd

Browse files
committed
Update test and doc example
1 parent edc87e1 commit 889d3dd

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

docs/reference/schemas/config/functions/shallowMerge.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ entirely with the value defined by the last object with that property in the inp
4848

4949
### Example 1 - Merge configuration objects
5050

51-
The following example demonstrates merging two configuration objects using [`createArray()`][02]
52-
and [`createObject()`][03], where the second object overrides properties from the first.
51+
The following example demonstrates merging two configuration objects where the second object
52+
overrides properties from the first.
5353

5454
```yaml
5555
# shallowMerge.example.1.dsc.config.yaml
5656
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
57+
variables:
58+
configArray:
59+
- host: localhost
60+
port: 8080
61+
- port: 9000
62+
ssl: true
5763
resources:
5864
- name: Echo
5965
type: Microsoft.DSC.Debug/Echo
6066
properties:
61-
output: >-
62-
[shallowMerge(
63-
createArray(
64-
createObject('host', 'localhost', 'port', 8080),
65-
createObject('port', 9000, 'ssl', true())
66-
)
67-
)]
67+
output: "[shallowMerge(variables('configArray'))]"
6868
```
6969
7070
```bash

lib/dsc-lib/src/functions/shallow_merge.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ impl Function for ShallowMerge {
3030
let mut result = Map::new();
3131

3232
for item in array {
33-
if let Some(obj) = item.as_object() {
34-
for (key, value) in obj {
35-
// Shallow merge: replace the entire value, even if it's a nested object
36-
result.insert(key.clone(), value.clone());
37-
}
33+
let obj = item.as_object().ok_or_else(|| {
34+
DscError::Parser(format!(
35+
"shallowMerge requires all array elements to be objects, but found: {}",
36+
item
37+
))
38+
})?;
39+
40+
for (key, value) in obj {
41+
// Shallow merge: replace the entire value, even if it's a nested object
42+
result.insert(key.clone(), value.clone());
3843
}
3944
}
4045

@@ -159,4 +164,26 @@ mod tests {
159164

160165
assert!(result.is_err());
161166
}
167+
168+
#[test]
169+
fn shallow_merge_array_with_non_object_elements_error() {
170+
let mut parser = Statement::new().unwrap();
171+
let result = parser.parse_and_execute(
172+
"[shallowMerge(createArray('string', 'another'))]",
173+
&Context::new()
174+
);
175+
assert!(result.is_err());
176+
177+
let result = parser.parse_and_execute(
178+
"[shallowMerge(createArray(1, 2, 3))]",
179+
&Context::new()
180+
);
181+
assert!(result.is_err());
182+
183+
let result = parser.parse_and_execute(
184+
"[shallowMerge(createArray(createObject('a', 1), 'string', createObject('b', 2)))]",
185+
&Context::new()
186+
);
187+
assert!(result.is_err());
188+
}
162189
}

0 commit comments

Comments
 (0)