Skip to content

Commit c3ada13

Browse files
committed
Update examples in dataUri and dataUriToString documentation for clarity and accuracy
1 parent 45b8d90 commit c3ada13

File tree

2 files changed

+116
-49
lines changed

2 files changed

+116
-49
lines changed

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

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ especially when the content needs to be passed through systems that expect URI-f
2828

2929
## Examples
3030

31-
### Example 1 - Convert a string to data URI
31+
### Example 1 - Encode a script for transport
3232

33-
The configuration converts a basic string value with the `dataUri()` function.
33+
Encoding a PowerShell script as a data URI ensures safe transport through systems that may have
34+
issues with special characters or line breaks.
3435

3536
```yaml
3637
# dataUri.example.1.dsc.config.yaml
3738
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
39+
parameters:
40+
scriptContent:
41+
type: string
42+
defaultValue: "Write-Host 'Hello, World!'"
3843
resources:
39-
- name: Convert 'Hello' to data URI
44+
- name: Encode script as data URI
4045
type: Microsoft.DSC.Debug/Echo
4146
properties:
42-
output: "[dataUri('Hello')]"
47+
output:
48+
originalScript: "[parameters('scriptContent')]"
49+
encodedScript: "[dataUri(parameters('scriptContent'))]"
4350
```
4451
4552
```bash
@@ -48,28 +55,30 @@ dsc config get --file dataUri.example.1.dsc.config.yaml
4855

4956
```yaml
5057
results:
51-
- name: Convert 'Hello' to data URI
58+
- name: Encode script as data URI
5259
type: Microsoft.DSC.Debug/Echo
5360
result:
5461
actualState:
55-
output: data:text/plain;charset=utf8;base64,SGVsbG8=
62+
output:
63+
originalScript: Write-Host 'Hello, World!'
64+
encodedScript: data:text/plain;charset=utf8;base64,V3JpdGUtSG9zdCAnSGVsbG8sIFdvcmxkISc=
5665
messages: []
5766
hadErrors: false
5867
```
5968
60-
### Example 2 - Convert a concatenated string to data URI
69+
### Example 2 - Encode JSON configuration for embedding
6170
62-
The configuration uses the [concat()][02] function inside the `dataUri()` function to combine
63-
strings before converting to a data URI.
71+
The configuration encodes a JSON configuration string as a data URI, which is useful when passing
72+
structured data through systems that expect URI-formatted content.
6473
6574
```yaml
6675
# dataUri.example.2.dsc.config.yaml
6776
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
6877
resources:
69-
- name: Convert concatenated string to data URI
78+
- name: Encode JSON config as data URI
7079
type: Microsoft.DSC.Debug/Echo
7180
properties:
72-
output: "[dataUri(concat('Hello', ', World!'))]"
81+
output: "[dataUri('{\"setting\":\"value\",\"enabled\":true}')]"
7382
```
7483
7584
```bash
@@ -78,28 +87,31 @@ dsc config get --file dataUri.example.2.dsc.config.yaml
7887

7988
```yaml
8089
results:
81-
- name: Convert concatenated string to data URI
90+
- name: Encode JSON config as data URI
8291
type: Microsoft.DSC.Debug/Echo
8392
result:
8493
actualState:
85-
output: data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkIQ==
94+
output: data:text/plain;charset=utf8;base64,eyJzZXR0aW5nIjoidmFsdWUiLCJlbmFibGVkIjp0cnVlfQ==
8695
messages: []
8796
hadErrors: false
8897
```
8998
90-
### Example 3 - Round-trip encoding and decoding
99+
### Example 3 - Compare base64 and dataUri encoding
91100
92-
The configuration demonstrates encoding a string to a data URI and then decoding it back using the
93-
[dataUriToString()][03] function.
101+
Unlike the [`base64()`][02] function which returns only the encoded content, `dataUri()` adds
102+
the data URI prefix with media type information. Use `dataUri()` when the target system expects
103+
the full data URI format.
94104

95105
```yaml
96106
# dataUri.example.3.dsc.config.yaml
97107
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
98108
resources:
99-
- name: Round-trip data URI conversion
109+
- name: Compare encoding methods
100110
type: Microsoft.DSC.Debug/Echo
101111
properties:
102-
output: "[dataUriToString(dataUri('Configuration Data'))]"
112+
output:
113+
base64Only: "[base64('Hello')]"
114+
fullDataUri: "[dataUri('Hello')]"
103115
```
104116

105117
```bash
@@ -108,11 +120,43 @@ dsc config get --file dataUri.example.3.dsc.config.yaml
108120

109121
```yaml
110122
results:
111-
- name: Round-trip data URI conversion
123+
- name: Compare encoding methods
112124
type: Microsoft.DSC.Debug/Echo
113125
result:
114126
actualState:
115-
output: Configuration Data
127+
output:
128+
base64Only: SGVsbG8=
129+
fullDataUri: data:text/plain;charset=utf8;base64,SGVsbG8=
130+
messages: []
131+
hadErrors: false
132+
```
133+
134+
### Example 4 - Encode multiline content
135+
136+
Multiline content like configuration files or scripts can be encoded as a data URI to preserve
137+
line breaks during transport.
138+
139+
```yaml
140+
# dataUri.example.4.dsc.config.yaml
141+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
142+
resources:
143+
- name: Encode multiline content
144+
type: Microsoft.DSC.Debug/Echo
145+
properties:
146+
output: "[dataUri('line1\nline2\nline3')]"
147+
```
148+
149+
```bash
150+
dsc config get --file dataUri.example.4.dsc.config.yaml
151+
```
152+
153+
```yaml
154+
results:
155+
- name: Encode multiline content
156+
type: Microsoft.DSC.Debug/Echo
157+
result:
158+
actualState:
159+
output: data:text/plain;charset=utf8;base64,bGluZTEKbGluZTIKbGluZTM=
116160
messages: []
117161
hadErrors: false
118162
```
@@ -144,5 +188,4 @@ Type: string
144188

145189
<!-- Link reference definitions -->
146190
[01]: https://en.wikipedia.org/wiki/Data_URI_scheme
147-
[02]: concat.md
148-
[03]: dataUriToString.md
191+
[02]: base64.md

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

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@ and URL-encoded data URIs. It automatically detects the encoding method and deco
2828

2929
## Examples
3030

31-
### Example 1 - Decode a base64-encoded data URI
31+
### Example 1 - Decode embedded script content
3232

33-
The configuration decodes a data URI back to its original string value.
33+
Decoding a PowerShell script from a data URI is useful when receiving commands from external
34+
systems that transmit data in this format.
3435

3536
```yaml
3637
# dataUriToString.example.1.dsc.config.yaml
3738
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
39+
parameters:
40+
encodedScript:
41+
type: string
42+
defaultValue: "data:text/plain;charset=utf8;base64,V3JpdGUtSG9zdCAnSGVsbG8sIFdvcmxkISc="
3843
resources:
39-
- name: Decode data URI
44+
- name: Decode and display script
4045
type: Microsoft.DSC.Debug/Echo
4146
properties:
42-
output: "[dataUriToString('data:text/plain;charset=utf8;base64,SGVsbG8=')]"
47+
output: "[dataUriToString(parameters('encodedScript'))]"
4348
```
4449
4550
```bash
@@ -48,31 +53,34 @@ dsc config get --file dataUriToString.example.1.dsc.config.yaml
4853

4954
```yaml
5055
results:
51-
- name: Decode data URI
56+
- name: Decode and display script
5257
type: Microsoft.DSC.Debug/Echo
5358
result:
5459
actualState:
55-
output: Hello
60+
output: Write-Host 'Hello, World!'
5661
messages: []
5762
hadErrors: false
5863
```
5964
60-
### Example 2 - Decode a data URI from parameter
65+
### Example 2 - Extract JSON configuration from data URI
6166
62-
This example shows decoding a data URI value passed through a parameter.
67+
The configuration decodes a JSON configuration that was transmitted as a data URI, then parses it
68+
using the [`json()`][06] function to access its properties.
6369

6470
```yaml
6571
# dataUriToString.example.2.dsc.config.yaml
6672
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
6773
parameters:
68-
dataFormattedString:
74+
configDataUri:
6975
type: string
70-
defaultValue: "data:;base64,SGVsbG8sIFdvcmxkIQ=="
76+
defaultValue: "data:;base64,eyJzZXR0aW5nIjoidmFsdWUiLCJlbmFibGVkIjp0cnVlfQ=="
7177
resources:
72-
- name: Decode data URI from parameter
78+
- name: Decode and parse JSON config
7379
type: Microsoft.DSC.Debug/Echo
7480
properties:
75-
output: "[dataUriToString(parameters('dataFormattedString'))]"
81+
output:
82+
rawJson: "[dataUriToString(parameters('configDataUri'))]"
83+
parsedSetting: "[json(dataUriToString(parameters('configDataUri'))).setting]"
7684
```
7785

7886
```bash
@@ -81,28 +89,34 @@ dsc config get --file dataUriToString.example.2.dsc.config.yaml
8189

8290
```yaml
8391
results:
84-
- name: Decode data URI from parameter
92+
- name: Decode and parse JSON config
8593
type: Microsoft.DSC.Debug/Echo
8694
result:
8795
actualState:
88-
output: Hello, World!
96+
output:
97+
rawJson: '{"setting":"value","enabled":true}'
98+
parsedSetting: value
8999
messages: []
90100
hadErrors: false
91101
```
92102

93-
### Example 3 - Round-trip encoding and decoding
103+
### Example 3 - Process data from Azure ARM template output
94104

95-
The configuration demonstrates encoding a string to a data URI and then decoding it
96-
back using the [`dataUri()`][02] function inside the `dataUriToString()` function.
105+
Azure ARM templates and similar systems often use data URIs for content encoding. Use
106+
`dataUriToString()` to decode this content back to its original form.
97107

98108
```yaml
99109
# dataUriToString.example.3.dsc.config.yaml
100110
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
111+
parameters:
112+
armTemplateOutput:
113+
type: string
114+
defaultValue: "data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkIQ=="
101115
resources:
102-
- name: Round-trip data URI conversion
116+
- name: Process ARM template data URI output
103117
type: Microsoft.DSC.Debug/Echo
104118
properties:
105-
output: "[dataUriToString(dataUri('Configuration Data'))]"
119+
output: "[dataUriToString(parameters('armTemplateOutput'))]"
106120
```
107121

108122
```bash
@@ -111,27 +125,34 @@ dsc config get --file dataUriToString.example.3.dsc.config.yaml
111125

112126
```yaml
113127
results:
114-
- name: Round-trip data URI conversion
128+
- name: Process ARM template data URI output
115129
type: Microsoft.DSC.Debug/Echo
116130
result:
117131
actualState:
118-
output: Configuration Data
132+
output: Hello, World!
119133
messages: []
120134
hadErrors: false
121135
```
122136

123-
### Example 4 - Decode URL-encoded data URI
137+
### Example 4 - Round-trip verification
124138

125-
This example demonstrates decoding a data URI that uses URL encoding instead of base64.
139+
Encoding a string to a data URI and decoding it back verifies that data survives the
140+
transformation correctly. Combine `dataUriToString()` with [`dataUri()`][02] to test this.
126141

127142
```yaml
128143
# dataUriToString.example.4.dsc.config.yaml
129144
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
145+
parameters:
146+
originalContent:
147+
type: string
148+
defaultValue: "Configuration with special chars: <>&\""
130149
resources:
131-
- name: Decode URL-encoded data URI
150+
- name: Verify round-trip encoding
132151
type: Microsoft.DSC.Debug/Echo
133152
properties:
134-
output: "[dataUriToString('data:text/plain,Hello%20World')]"
153+
output:
154+
original: "[parameters('originalContent')]"
155+
afterRoundTrip: "[dataUriToString(dataUri(parameters('originalContent')))]"
135156
```
136157

137158
```bash
@@ -140,11 +161,13 @@ dsc config get --file dataUriToString.example.4.dsc.config.yaml
140161

141162
```yaml
142163
results:
143-
- name: Decode URL-encoded data URI
164+
- name: Verify round-trip encoding
144165
type: Microsoft.DSC.Debug/Echo
145166
result:
146167
actualState:
147-
output: Hello World
168+
output:
169+
original: 'Configuration with special chars: <>&"'
170+
afterRoundTrip: 'Configuration with special chars: <>&"'
148171
messages: []
149172
hadErrors: false
150173
```
@@ -196,3 +219,4 @@ The `dataUriToString()` function raises errors for the following conditions:
196219
[03]: ./base64.md
197220
[04]: ./base64ToString.md
198221
[05]: ./parameters.md
222+
[06]: ./json.md

0 commit comments

Comments
 (0)