|
| 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 is not null. This function is useful for providing fallback values when dealing with potentially null data. |
| 23 | + |
| 24 | +If all arguments are null, the function returns null. |
| 25 | + |
| 26 | +## Examples |
| 27 | + |
| 28 | +### Example 1 - Basic coalesce with strings |
| 29 | + |
| 30 | +The following example shows how to use the function with string values. |
| 31 | + |
| 32 | +```yaml |
| 33 | +# coalesce.example.1.dsc.config.yaml |
| 34 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 35 | +resources: |
| 36 | +- name: Coalesce strings |
| 37 | + type: Microsoft.DSC.Debug/Echo |
| 38 | + properties: |
| 39 | + output: |
| 40 | + firstNonNull: "[coalesce(null, 'hello', 'world')]" |
| 41 | + allNull: "[coalesce(null, null, null)]" |
| 42 | + firstNotNull: "[coalesce('first', 'second', 'third')]" |
| 43 | +``` |
| 44 | +
|
| 45 | +```bash |
| 46 | +dsc config get --file coalesce.example.1.dsc.config.yaml |
| 47 | +``` |
| 48 | + |
| 49 | +```yaml |
| 50 | +results: |
| 51 | +- name: Coalesce strings |
| 52 | + type: Microsoft.DSC.Debug/Echo |
| 53 | + result: |
| 54 | + actualState: |
| 55 | + output: |
| 56 | + firstNonNull: hello |
| 57 | + allNull: null |
| 58 | + firstNotNull: first |
| 59 | +messages: [] |
| 60 | +hadErrors: false |
| 61 | +``` |
| 62 | +
|
| 63 | +### Example 2 - Mixed data types |
| 64 | +
|
| 65 | +The following example shows how the function works with different data types. |
| 66 | +
|
| 67 | +```yaml |
| 68 | +# coalesce.example.2.dsc.config.yaml |
| 69 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 70 | +resources: |
| 71 | +- name: Coalesce mixed types |
| 72 | + type: Microsoft.DSC.Debug/Echo |
| 73 | + properties: |
| 74 | + output: |
| 75 | + numberFallback: "[coalesce(null, 42)]" |
| 76 | + booleanFallback: "[coalesce(null, null, true)]" |
| 77 | + stringToNumber: "[coalesce(null, 123, 'fallback')]" |
| 78 | +``` |
| 79 | +
|
| 80 | +```bash |
| 81 | +dsc config get --file coalesce.example.2.dsc.config.yaml |
| 82 | +``` |
| 83 | + |
| 84 | +```yaml |
| 85 | +results: |
| 86 | +- name: Coalesce mixed types |
| 87 | + type: Microsoft.DSC.Debug/Echo |
| 88 | + result: |
| 89 | + actualState: |
| 90 | + output: |
| 91 | + numberFallback: 42 |
| 92 | + booleanFallback: true |
| 93 | + stringToNumber: 123 |
| 94 | +messages: [] |
| 95 | +hadErrors: false |
| 96 | +``` |
| 97 | +
|
| 98 | +### Example 3 - Configuration fallbacks |
| 99 | +
|
| 100 | +The following example shows a practical use case for providing configuration defaults. |
| 101 | +
|
| 102 | +```yaml |
| 103 | +# coalesce.example.3.dsc.config.yaml |
| 104 | +$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json |
| 105 | +parameters: |
| 106 | + customValue: |
| 107 | + type: string |
| 108 | + defaultValue: null |
| 109 | + timeout: |
| 110 | + type: int |
| 111 | + defaultValue: 0 |
| 112 | +resources: |
| 113 | +- name: Configuration with fallbacks |
| 114 | + type: Microsoft.DSC.Debug/Echo |
| 115 | + properties: |
| 116 | + output: |
| 117 | + configValue: "[coalesce(parameters('customValue'), 'default-config')]" |
| 118 | + timeout: "[coalesce(parameters('timeout'), 30)]" |
| 119 | +``` |
| 120 | +
|
| 121 | +```bash |
| 122 | +dsc config get --file coalesce.example.3.dsc.config.yaml |
| 123 | +``` |
| 124 | + |
| 125 | +```yaml |
| 126 | +results: |
| 127 | +- name: Configuration with fallbacks |
| 128 | + type: Microsoft.DSC.Debug/Echo |
| 129 | + result: |
| 130 | + actualState: |
| 131 | + output: |
| 132 | + configValue: default-config |
| 133 | + timeout: 0 |
| 134 | +messages: [] |
| 135 | +hadErrors: false |
| 136 | +``` |
| 137 | +
|
| 138 | +## Parameters |
| 139 | +
|
| 140 | +### value1, value2, ... |
| 141 | +
|
| 142 | +The `coalesce()` function accepts one or more arguments of any type. |
| 143 | +Arguments are evaluated from left to right, and the function returns the first non-null |
| 144 | +value encountered. |
| 145 | + |
| 146 | +```yaml |
| 147 | +Type: [any] |
| 148 | +Required: true |
| 149 | +MinimumCount: 1 |
| 150 | +MaximumCount: unlimited |
| 151 | +``` |
| 152 | + |
| 153 | +## Output |
| 154 | + |
| 155 | +The `coalesce()` function returns the first non-null argument, or null if all arguments are null. |
| 156 | +The return type matches the type of the first non-null argument. |
| 157 | + |
| 158 | +```yaml |
| 159 | +Type: [any] |
| 160 | +``` |
| 161 | + |
| 162 | +<!-- Link reference definitions --> |
0 commit comments