Skip to content

Commit 275a53e

Browse files
committed
Add null and createObject
1 parent eb87e54 commit 275a53e

File tree

3 files changed

+423
-10
lines changed

3 files changed

+423
-10
lines changed

dsc_lib/src/functions/coalesce.md renamed to docs/reference/schemas/config/functions/coalesce.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ coalesce(<value1>, <value2>, ...)
1919

2020
## Description
2121

22-
The `coalesce()` function evaluates arguments from left to right and returns the first argument that is not null. This function is useful for providing fallback values when dealing with potentially null data.
22+
The `coalesce()` function evaluates arguments from left to right and returns the first argument that
23+
is not null. This function is useful for providing fallback values when dealing with potentially
24+
null data.
2325

2426
If all arguments are null, the function returns null.
2527

@@ -37,8 +39,8 @@ resources:
3739
type: Microsoft.DSC.Debug/Echo
3840
properties:
3941
output:
40-
firstNonNull: "[coalesce(null, 'hello', 'world')]"
41-
allNull: "[coalesce(null, null, null)]"
42+
firstNonNull: "[coalesce(null(), 'DSC', 'landscape')]"
43+
allNull: "[coalesce(null(), null(), null())]"
4244
firstNotNull: "[coalesce('first', 'second', 'third')]"
4345
```
4446
@@ -53,7 +55,7 @@ results:
5355
result:
5456
actualState:
5557
output:
56-
firstNonNull: hello
58+
firstNonNull: DSC
5759
allNull: null
5860
firstNotNull: first
5961
messages: []
@@ -72,9 +74,9 @@ resources:
7274
type: Microsoft.DSC.Debug/Echo
7375
properties:
7476
output:
75-
numberFallback: "[coalesce(null, 42)]"
76-
booleanFallback: "[coalesce(null, null, true)]"
77-
stringToNumber: "[coalesce(null, 123, 'fallback')]"
77+
numberFallback: "[coalesce(null(), 42)]"
78+
booleanFallback: "[coalesce(null(), null(), true)]"
79+
stringToNumber: "[coalesce(null(), 123, 'fallback')]"
7880
```
7981
8082
```bash
@@ -105,7 +107,7 @@ $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
105107
parameters:
106108
customValue:
107109
type: string
108-
defaultValue: null
110+
defaultValue: 'customValue'
109111
timeout:
110112
type: int
111113
defaultValue: 0
@@ -129,15 +131,15 @@ results:
129131
result:
130132
actualState:
131133
output:
132-
configValue: default-config
134+
configValue: customValue
133135
timeout: 0
134136
messages: []
135137
hadErrors: false
136138
```
137139
138140
## Parameters
139141
140-
### value1, value2, ...
142+
### value1, value2
141143
142144
The `coalesce()` function accepts one or more arguments of any type.
143145
Arguments are evaluated from left to right, and the function returns the first non-null
@@ -159,4 +161,9 @@ The return type matches the type of the first non-null argument.
159161
Type: [any]
160162
```
161163

164+
## Related functions
165+
166+
- [`null()`][00] - Returns a simple JSON null value.
167+
162168
<!-- Link reference definitions -->
169+
[00]: ./null.md
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('Yoda', 'Darth Vader', 'Palpetine'))]"
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+
- Yoda
134+
- 'Darth Vader'
135+
- Palpetine
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; numeric or other types are not allowed as keys
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)