Skip to content

Commit 09e7606

Browse files
Merge pull request #1011 from Gijsreyn/reference-doc-compare-and-object
Reference doc null, createObject and coalesce
2 parents c9c4359 + 9584cf4 commit 09e7606

File tree

4 files changed

+525
-1
lines changed

4 files changed

+525
-1
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
description: Reference for the 'coalesce' DSC configuration document function
3+
ms.date: 07/24/2025
4+
ms.topic: reference
5+
title: coalesce
6+
---
7+
8+
# coalesce
9+
10+
## Synopsis
11+
12+
Returns the first non-null value from a list of arguments.
13+
14+
## Syntax
15+
16+
```Syntax
17+
coalesce(<value1>, <value2>, ...)
18+
```
19+
20+
## Description
21+
22+
The `coalesce()` function evaluates arguments from left to right and returns the first argument that
23+
isn't null. This function is useful for providing fallback values when dealing with potentially
24+
null data.
25+
26+
If all arguments are null, the function returns null.
27+
28+
## Examples
29+
30+
### Example 1 - Basic coalesce with strings
31+
32+
The following example shows how to use the function with string values.
33+
34+
```yaml
35+
# coalesce.example.1.dsc.config.yaml
36+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
37+
resources:
38+
- name: Coalesce strings
39+
type: Microsoft.DSC.Debug/Echo
40+
properties:
41+
output:
42+
firstNonNull: "[coalesce(null(), 'DSC', 'landscape')]"
43+
allNull: "[coalesce(null(), null(), null())]"
44+
noneNull: "[coalesce('first', 'second', 'third')]"
45+
```
46+
47+
```bash
48+
dsc config get --file coalesce.example.1.dsc.config.yaml
49+
```
50+
51+
```yaml
52+
results:
53+
- name: Coalesce strings
54+
type: Microsoft.DSC.Debug/Echo
55+
result:
56+
actualState:
57+
output:
58+
firstNonNull: DSC
59+
allNull: null
60+
noneNull: first
61+
messages: []
62+
hadErrors: false
63+
```
64+
65+
### Example 2 - Mixed data types
66+
67+
The following example shows how the function works with different data types.
68+
69+
```yaml
70+
# coalesce.example.2.dsc.config.yaml
71+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
72+
resources:
73+
- name: Coalesce mixed types
74+
type: Microsoft.DSC.Debug/Echo
75+
properties:
76+
output:
77+
numberFallback: "[coalesce(null(), 42)]"
78+
booleanFallback: "[coalesce(null(), null(), true)]"
79+
stringToNumber: "[coalesce(null(), 123, 'fallback')]"
80+
```
81+
82+
```bash
83+
dsc config get --file coalesce.example.2.dsc.config.yaml
84+
```
85+
86+
```yaml
87+
results:
88+
- name: Coalesce mixed types
89+
type: Microsoft.DSC.Debug/Echo
90+
result:
91+
actualState:
92+
output:
93+
numberFallback: 42
94+
booleanFallback: true
95+
stringToNumber: 123
96+
messages: []
97+
hadErrors: false
98+
```
99+
100+
## Parameters
101+
102+
### value1, value2
103+
104+
The `coalesce()` function accepts one or more arguments of any type.
105+
Arguments are evaluated from left to right, and the function returns the first non-null
106+
value encountered.
107+
108+
```yaml
109+
Type: [any]
110+
Required: true
111+
MinimumCount: 1
112+
MaximumCount: unlimited
113+
```
114+
115+
## Output
116+
117+
The `coalesce()` function returns the first non-null argument, or null if all arguments are null.
118+
The return type matches the type of the first non-null argument.
119+
120+
```yaml
121+
Type: [any]
122+
```
123+
124+
## Related functions
125+
126+
- [`null()`][00] - Returns a simple JSON null value.
127+
128+
<!-- Link reference definitions -->
129+
[00]: ./null.md

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function returns an array of arrays.
2929

3030
### Example 1 - Create an array of integers
3131

32-
example synopsis
32+
The following example shows how to create a simple array with integers.
3333

3434
```yaml
3535
# createArray.example.1.dsc.config.yaml
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
---
2+
description: Reference for the 'createObject' DSC configuration document function
3+
ms.date: 07/28/2025
4+
ms.topic: reference
5+
title: createObject
6+
---
7+
8+
## Synopsis
9+
10+
Creates a JSON object from key-value pairs.
11+
12+
## Syntax
13+
14+
```Syntax
15+
createObject(<key1>, <value1>, <key2>, <value2>, ...)
16+
```
17+
18+
## Description
19+
20+
The `createObject()` function creates a JSON object from the provided key-value pairs.
21+
Arguments must be provided in pairs where the first argument of each pair is a string key,
22+
and the second argument is the value of any type.
23+
24+
If no arguments are provided, the function returns an empty object. The number of arguments
25+
must be even, as they represent key-value pairs.
26+
27+
## Examples
28+
29+
### Example 1 - Basic object creation
30+
31+
The following example shows how to create simple objects with string and numeric values.
32+
33+
```yaml
34+
# createObject.example.1.dsc.config.yaml
35+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
36+
resources:
37+
- name: Basic object creation
38+
type: Microsoft.DSC.Debug/Echo
39+
properties:
40+
output:
41+
simpleObject: "[createObject('name', 'test')]"
42+
multipleProps: "[createObject('key1', 'value1', 'key2', 42)]"
43+
emptyObject: "[createObject()]"
44+
```
45+
46+
```bash
47+
dsc config get --file createObject.example.1.dsc.config.yaml
48+
```
49+
50+
```yaml
51+
results:
52+
- name: Basic object creation
53+
type: Microsoft.DSC.Debug/Echo
54+
result:
55+
actualState:
56+
output:
57+
simpleObject:
58+
name: test
59+
multipleProps:
60+
key1: value1
61+
key2: 42
62+
emptyObject: {}
63+
messages: []
64+
hadErrors: false
65+
```
66+
67+
### Example 2 - Mixed data types
68+
69+
The following example shows how to create objects with different value types.
70+
71+
```yaml
72+
# createObject.example.2.dsc.config.yaml
73+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
74+
resources:
75+
- name: Mixed data types
76+
type: Microsoft.DSC.Debug/Echo
77+
properties:
78+
output: "[createObject('string', 'hello', 'number', 123, 'boolean', true, 'nullValue', null())]"
79+
```
80+
81+
```bash
82+
dsc config get --file createObject.example.2.dsc.config.yaml
83+
```
84+
85+
```yaml
86+
results:
87+
- name: Mixed data types
88+
type: Microsoft.DSC.Debug/Echo
89+
result:
90+
actualState:
91+
output:
92+
string: hello
93+
number: 123
94+
boolean: true
95+
nullValue: null
96+
messages: []
97+
hadErrors: false
98+
```
99+
100+
### Example 3 - Nested objects and arrays
101+
102+
The following example shows how to create objects containing other objects and arrays.
103+
104+
```yaml
105+
# createObject.example.3.dsc.config.yaml
106+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
107+
resources:
108+
- name: Nested structures
109+
type: Microsoft.DSC.Debug/Echo
110+
properties:
111+
output:
112+
nestedObject: "[createObject('config', createObject('timeout', 30, 'enabled', true))]"
113+
objectWithArray: "[createObject('items', createArray('foo', 'bar', 'baz'))]"
114+
```
115+
116+
```bash
117+
dsc config get --file createObject.example.3.dsc.config.yaml
118+
```
119+
120+
```yaml
121+
results:
122+
- name: Nested structures
123+
type: Microsoft.DSC.Debug/Echo
124+
result:
125+
actualState:
126+
output:
127+
nestedObject:
128+
config:
129+
timeout: 30
130+
enabled: true
131+
objectWithArray:
132+
items:
133+
- foo
134+
- bar
135+
- baz
136+
messages: []
137+
hadErrors: false
138+
```
139+
140+
### Example 4 - Using with other functions
141+
142+
The following example shows how to use `createObject()` with other DSC functions.
143+
144+
```yaml
145+
# createObject.example.4.dsc.config.yaml
146+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
147+
parameters:
148+
userName:
149+
type: string
150+
defaultValue: guest
151+
resources:
152+
- name: Function integration
153+
type: Microsoft.DSC.Debug/Echo
154+
properties:
155+
output:
156+
userConfig: "[createObject('user', parameters('userName'), 'role', coalesce(null(), 'default'))]"
157+
fallbackObject: "[createObject('result', coalesce(null(), createObject('status', 'success')))]"
158+
```
159+
160+
```bash
161+
dsc config get --file createObject.example.4.dsc.config.yaml
162+
```
163+
164+
```yaml
165+
results:
166+
- name: Function integration
167+
type: Microsoft.DSC.Debug/Echo
168+
result:
169+
actualState:
170+
output:
171+
userConfig:
172+
user: guest
173+
role: default
174+
fallbackObject:
175+
result:
176+
status: success
177+
messages: []
178+
hadErrors: false
179+
```
180+
181+
## Parameters
182+
183+
### Key-value pairs
184+
185+
The `createObject()` function accepts zero or more key-value pairs. Each key must be a string,
186+
and values can be of any type.
187+
188+
```yaml
189+
Type: key: [string], value: [any]
190+
Required: false
191+
MinimumCount: 0
192+
MaximumCount: unlimited (must be even number)
193+
```
194+
195+
#### key
196+
197+
The object property name. Must be a string value.
198+
199+
```yaml
200+
Type: [string]
201+
Required: true (when providing values)
202+
```
203+
204+
#### value
205+
206+
The object property value. Can be any valid JSON type including strings, numbers, booleans, null, arrays, or other objects.
207+
208+
```yaml
209+
Type: [any]
210+
Required: true (when providing keys)
211+
```
212+
213+
## Output
214+
215+
The `createObject()` function returns a JSON object containing the specified key-value pairs.
216+
217+
```yaml
218+
Type: [object]
219+
```
220+
221+
## Error conditions
222+
223+
The function will return an error in the following cases:
224+
225+
- **Odd number of arguments**: Arguments must be provided in key-value pairs
226+
- **Non-string keys**: All keys must be string values
227+
- **Invalid argument types**: Arguments must be valid JSON types
228+
229+
## Notes
230+
231+
- Keys must be strings. If you specify numeric or other types, DSC raises an error.
232+
- Values can be any valid JSON type including null, arrays, and nested objects.
233+
- Duplicate keys will result in the last value overwriting previous values.
234+
- Empty object creation (`createObject()` with no arguments) is supported.
235+
- The function preserves the order of properties as specified.
236+
237+
## Related functions
238+
239+
- [`createArray()`][00] - Creates arrays that can be used as object values
240+
- [`coalesce()`][01] - Provides fallback values for object properties
241+
- [`null()`][02] - Creates explicit null values for object properties
242+
243+
<!-- Link reference definitions -->
244+
[00]: ./createArray.md
245+
[01]: ./coalesce.md
246+
[02]: ./null.md

0 commit comments

Comments
 (0)