Skip to content

Commit 2472b7d

Browse files
committed
Merge branches 'gh-57/main/add-base64string-function' and 'main' of https://github.com/Gijsreyn/operation-methods into gh-57/main/add-base64string-function
2 parents 62175ce + c51be62 commit 2472b7d

File tree

9 files changed

+131
-56
lines changed

9 files changed

+131
-56
lines changed

docs/concepts/resources/capabilities.md

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ resource. DSC performs the synthetic test by:
7676
1. Invoking the **Get** operation on the resource to retrieve the actual state of the instance.
7777
1. Synthetically testing each property for the desired state of an instance against the actual
7878
state returned. The synthetic test:
79-
79+
8080
- Uses strict, case-sensitive equivalence for strings.
8181
- Uses simple equivalence for numerical, boolean, and null values.
8282
- For arrays, item order doesn't matter. Arrays are considered equivalent if both the desired
@@ -134,32 +134,59 @@ manifest.
134134
A resource with the `export` capability supports enumerating every instance of the resource with
135135
the **Export** operation.
136136

137-
You can use resources with this capability with the following commands:
137+
You can use resources with the `export` capability with the following commands:
138138

139-
- [dsc config export][15] to return a configuration document
140-
representing the actual state for every instance of each resource defined in the input document.
141-
- [dsc resource export][16] to return a configuration document
142-
representing the actual state for every instance of the input resource.
143-
- `dsc resource get` with the [--all][17] option to return
144-
the actual state of every instance of the input resource.
139+
- [dsc config export][15] to return a configuration document representing the actual state for
140+
every instance of each resource defined in the input document.
141+
- [dsc resource export][16] to return a configuration document representing the actual state for
142+
every instance of the input resource.
143+
- `dsc resource get` with the [--all][17] option to return the actual state of every instance of
144+
the input resource.
145145

146146
A command resource has this capability when it defines the [export][18] property in its resource
147147
manifest.
148148

149+
### Synthetic export
150+
151+
When a resource doesn't have the `export` capability, DSC uses a synthetic export for instances of
152+
the resource. DSC performs the synthetic export by:
153+
154+
1. Invoking the **Get** operation on the resource to retrieve the actual state of the instance.
155+
156+
Unlike non-synthetic export operations, users _must_ provide a filter for the resource if the
157+
resource instance JSON schema defines any [required properties][19].
158+
1. Exporting an instance of the resource with:
159+
160+
- The `properties` field populated by the actual state returned by the **Get** operation.
161+
- The `name` field populated by a string with the format `<short-type-name>-0`. The
162+
`<short-type-name>` is the last segment of the resource type name, like `Registry` for
163+
`Microsoft.Windows/Registry`.
164+
165+
Synthetic export only ever returns a single instance and always requires a filtering instance. You
166+
can use resources that rely on synthetic exporting with the following commands:
167+
168+
- [dsc config export][15] to return a configuration document representing the actual state for
169+
every instance of each resource defined in the input document. For resources that rely on
170+
synthetic export, you _must_ define a filtering instance in the input document. The resource
171+
can only export a single instance per filtering instance.
172+
- [dsc resource export][16] to return a configuration document representing the actual state for
173+
the required filtering instance. If you invoke this command without providing a filtering
174+
instance, the operation fails.
175+
149176
## resolve
150177

151178
A resource with the `resolve` capability supports resolving nested resource instances from an
152-
external source. This capability is primarily used by [importer resources][19] to enable users to
179+
external source. This capability is primarily used by [importer resources][20] to enable users to
153180
compose configuration documents.
154181

155-
A command resource has this capability when it defines the [resolve][20] property in its resource
182+
A command resource has this capability when it defines the [resolve][21] property in its resource
156183
manifest.
157184

158185
## See also
159186

160-
- [DSC resource operations][21]
161-
- [DSC resource kinds][22]
162-
- [DSC resource properties][23]
187+
- [DSC resource operations][22]
188+
- [DSC resource kinds][23]
189+
- [DSC resource properties][24]
163190

164191
<!-- Link reference definitions -->
165192
[01]: operations.md#get-operation
@@ -180,8 +207,9 @@ manifest.
180207
[16]: ../../reference/cli/resource/export.md
181208
[17]: ../../reference/cli/resource/get.md#--all
182209
[18]: ../../reference/schemas/resource/manifest/export.md
183-
[19]: ../resources/kinds.md#importer-resources
184-
[20]: ../../reference/schemas/resource/manifest/resolve.md
185-
[21]: operations.md
186-
[22]: kinds.md
187-
[23]: ../../concepts/resources/properties.md
210+
[19]: ./properties.md#required-resource-properties
211+
[20]: ../resources/kinds.md#importer-resources
212+
[21]: ../../reference/schemas/resource/manifest/resolve.md
213+
[22]: operations.md
214+
[23]: kinds.md
215+
[24]: ../../concepts/resources/properties.md

docs/concepts/resources/operations.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ DSC invokes the **Delete** operation when you use the following commands:
6767
The **Export** operation retrieves the actual state for every instance of the resource on a system.
6868
The result is a configuration document that includes the exported instances.
6969

70-
This operation is only available for resources with the [export capability][05].
70+
If a resource doesn't have the [`export` capability][05], DSC synthetically exports the resource.
71+
For more information, see [Synthetic export][06].
7172

7273
DSC invokes the **Export** operation when you use the following commands:
7374

@@ -83,7 +84,7 @@ DSC invokes the **Export** operation when you use the following commands:
8384
The **List** operation retrieves the available adapted resources for a specific DSC adapter
8485
resource.
8586

86-
This operation is only available for [adapter resources][06].
87+
This operation is only available for [adapter resources][07].
8788

8889
## Validate operation
8990

@@ -101,26 +102,27 @@ invoke an adapted resource.
101102
The **Resolve** operation processes an importer resource instance to return a configuration
102103
document.
103104

104-
This operation is only available for resources with the [resolve capability][07]. This operation
105-
is primarily useful for [importer resources][08].
105+
This operation is only available for resources with the [resolve capability][08]. This operation
106+
is primarily useful for [importer resources][09].
106107

107108
## See also
108109

109-
- [DSC resource capabilities][09]
110-
- [DSC resource kinds][10]
111-
- [DSC resource properties][11]
112-
- [DSC command reference][12]
110+
- [DSC resource capabilities][10]
111+
- [DSC resource kinds][11]
112+
- [DSC resource properties][12]
113+
- [DSC command reference][13]
113114

114115
<!-- Link reference definitions -->
115116
[01]: ./capabilities.md#get
116117
[02]: ./capabilities.md#test
117118
[03]: ./capabilities.md#set
118119
[04]: ./capabilities.md#delete
119120
[05]: ./capabilities.md#export
120-
[06]: ./kinds.md#adapter-resources
121-
[07]: ./capabilities.md#resolve
122-
[08]: ./kinds.md#importer-resources
123-
[09]: ./capabilities.md
124-
[10]: ./kinds.md
125-
[11]: ./properties.md
126-
[12]: ../../reference/cli/index.md
121+
[06]: ./capabilities.md#synthetic-export
122+
[07]: ./kinds.md#adapter-resources
123+
[08]: ./capabilities.md#resolve
124+
[09]: ./kinds.md#importer-resources
125+
[10]: ./capabilities.md
126+
[11]: ./kinds.md
127+
[12]: ./properties.md
128+
[13]: ../../reference/cli/index.md

docs/concepts/resources/overview.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ Use the `dsc resource export` command to invoke the operation. When you invoke t
264264
operation, DSC returns an array of resources instance definitions you can copy into a configuration
265265
document.
266266

267+
You can specify an input instance for the **Export** operation to filter the exported instances. If
268+
a resource doesn't implement **Export**, you can still invoke the `dsc resource export` command.
269+
When you invoke the **Export** operation for a resource that doesn't implement **Export**, DSC
270+
provides a _synthetic export_. When using DSC's synthetic export feature, you must provide a
271+
filtering instance of the resource. For more information, see [Synthetic export][04].
272+
267273
## Declaring resource instances
268274

269275
DSC configuration documents enable managing more than one resource or resource instance at a time.
@@ -298,14 +304,15 @@ resources:
298304

299305
## See also
300306

301-
- [Anatomy of a DSC command resource][04] to learn about authoring resources in your language
307+
- [Anatomy of a DSC command resource][05] to learn about authoring resources in your language
302308
of choice.
303-
- [DSC configuration documents][05] to learn about using resources in a configuration document.
304-
- [Command line reference for the 'dsc resource' command][06]
309+
- [DSC configuration documents][06] to learn about using resources in a configuration document.
310+
- [Command line reference for the 'dsc resource' command][07]
305311

306312
[01]: ../../reference/schemas/definitions/resourceType.md
307313
[02]: ../../reference/schemas/resource/properties/overview.md
308314
[03]: ../../reference/cli/resource/list.md
309-
[04]: ./anatomy.md
310-
[05]: ../configuration-documents/overview.md
311-
[06]: ../../reference/cli/resource/index.md
315+
[04]: capabilities.md#synthetic-export
316+
[05]: ./anatomy.md
317+
[06]: ../configuration-documents/overview.md
318+
[07]: ../../reference/cli/resource/index.md

dsc/tests/dsc_functions.tests.ps1

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Describe 'tests for function expressions' {
120120
@{ expression = "[intersection(parameters('firstObject'), parameters('firstArray'))]"; isError = $true }
121121
@{ expression = "[intersection(parameters('firstArray'), parameters('secondArray'), parameters('fifthArray'))]"; expected = @('cd') }
122122
@{ expression = "[intersection(parameters('firstObject'), parameters('secondObject'), parameters('sixthObject'))]"; expected = [pscustomobject]@{ two = 'b' } }
123-
@{ expression = "[intersection(parameters('nestedObject1'), parameters('nestedObject2'))]"; expected = [pscustomobject]@{
123+
@{ expression = "[intersection(parameters('nestedObject1'), parameters('nestedObject2'))]"; expected = [pscustomobject]@{
124124
shared = [pscustomobject]@{ value = 42; flag = $true }
125125
level = 1
126126
} }
@@ -711,13 +711,34 @@ Describe 'tests for function expressions' {
711711
properties:
712712
output: `"$expression`"
713713
"@
714-
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log
714+
$null = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log
715715
$LASTEXITCODE | Should -Not -Be 0
716716
$errorContent = Get-Content $TestDrive/error.log -Raw
717717
$errorContent | Should -Match ([regex]::Escape($expectedError))
718718
}
719719

720-
It 'base64ToString function works for: <expression>' -TestCases @(
720+
It 'mixed booleans with functions works' -TestCases @(
721+
@{ expression = "[and(true(), false, not(false))]"; expected = $false }
722+
@{ expression = "[or(false, false(), not(false()))]"; expected = $true }
723+
@{ expression = "[and(true(), true, not(false))]"; expected = $true }
724+
@{ expression = "[or(false, false(), not(true()))]"; expected = $false }
725+
) {
726+
param($expression, $expected)
727+
728+
$config_yaml = @"
729+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
730+
resources:
731+
- name: Echo
732+
type: Microsoft.DSC.Debug/Echo
733+
properties:
734+
output: "$expression"
735+
"@
736+
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
737+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
738+
$out.results[0].result.actualState.output | Should -BeExactly $expected
739+
}
740+
741+
It 'base64ToString function works for: <expression>' -TestCases @(
721742
@{ expression = "[base64ToString('aGVsbG8gd29ybGQ=')]"; expected = 'hello world' }
722743
@{ expression = "[base64ToString('')]"; expected = '' }
723744
@{ expression = "[base64ToString('aMOpbGxv')]"; expected = 'héllo' }

dsc_lib/src/functions/concat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Function for Concat {
1616
FunctionMetadata {
1717
name: "concat".to_string(),
1818
description: t!("functions.concat.description").to_string(),
19-
category: vec![FunctionCategory::String, FunctionCategory::Array],
19+
category: vec![FunctionCategory::Array, FunctionCategory::String],
2020
min_args: 2,
2121
max_args: usize::MAX,
2222
accepted_arg_ordered_types: vec![

dsc_lib/src/functions/contains.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Function for Contains {
1616
FunctionMetadata {
1717
name: "contains".to_string(),
1818
description: t!("functions.contains.description").to_string(),
19-
category: vec![FunctionCategory::String, FunctionCategory::Array],
19+
category: vec![FunctionCategory::Array, FunctionCategory::String],
2020
min_args: 2,
2121
max_args: 2,
2222
accepted_arg_ordered_types: vec![

tree-sitter-dscexpression/grammar.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const PREC = {
22
ESCAPEDSTRING: 2,
33
EXPRESSIONSTRING: 1,
4+
FUNCTION: 1,
5+
BOOLEAN: 0,
46
STRINGLITERAL: -11,
57
}
68

@@ -20,16 +22,20 @@ module.exports = grammar({
2022
expression: $ => seq(field('function', $.function), optional(field('accessor',$.accessor))),
2123
stringLiteral: $ => token(prec(PREC.STRINGLITERAL, /[^\[](.|\n)*?/)),
2224

23-
function: $ => seq(field('name', $.functionName), '(', field('args', optional($.arguments)), ')'),
24-
functionName: $ => /[a-zA-Z][a-zA-Z0-9]*(\.[a-zA-Z0-9]+)?/,
25+
function: $ => prec(PREC.FUNCTION, seq(field('name', $.functionName), '(', field('args', optional($.arguments)), ')')),
26+
functionName: $ => choice(
27+
/[a-zA-Z][a-zA-Z0-9]*(\.[a-zA-Z0-9]+)?/,
28+
$._booleanLiteral
29+
),
2530
arguments: $ => seq($._argument, repeat(seq(',', $._argument))),
2631
_argument: $ => choice($.expression, $._quotedString, $.number, $.boolean),
2732

2833
_quotedString: $ => seq('\'', $.string, '\''),
2934
// ARM strings are not allowed to contain single-quote characters unless escaped
3035
string: $ => /([^']|''|\n)*/,
3136
number: $ => /-?\d+/,
32-
boolean: $ => choice('true', 'false'),
37+
boolean: $ => prec(PREC.BOOLEAN, $._booleanLiteral),
38+
_booleanLiteral: $ => choice('true', 'false'),
3339

3440
accessor: $ => repeat1(choice($.memberAccess, $.index)),
3541

tree-sitter-dscexpression/test/corpus/invalid_expressions.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ String parameter not in quotes
1818
(function
1919
(functionName)
2020
(ERROR
21-
(functionName)
2221
(functionName)))))
2322

2423
=====
@@ -79,8 +78,6 @@ String starting with bracket
7978
---
8079

8180
(ERROR
82-
(ERROR
83-
(functionName))
8481
(functionName))
8582

8683
=====
@@ -179,8 +176,7 @@ Expression with member accessor outside
179176
(functionName)
180177
(arguments
181178
(number))))
182-
(ERROR
183-
(functionName)))
179+
(ERROR))
184180

185181
=====
186182
Expression with index accessor outside
@@ -209,6 +205,4 @@ String with un-escaped single-quote
209205
(functionName)
210206
(arguments
211207
(string))
212-
(ERROR
213-
(functionName)
214-
(functionName)))))
208+
(ERROR))))

tree-sitter-dscexpression/test/corpus/valid_expressions.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Simple expression
3434
=====
3535
Multiple arguments
3636
=====
37-
[myFunction('argString', 1, -1, true)]
37+
[myFunction('argString', 1, -1, true, false)]
3838
---
3939

4040
(statement
@@ -45,6 +45,7 @@ Multiple arguments
4545
(string)
4646
(number)
4747
(number)
48+
(boolean)
4849
(boolean)))))
4950

5051
=====
@@ -335,3 +336,19 @@ User Function
335336
(arguments
336337
(number)
337338
(number)))))
339+
340+
=====
341+
User Function with nested function
342+
=====
343+
[Environment.getEnvironmentConfig(true(), false)]
344+
---
345+
346+
(statement
347+
(expression
348+
(function
349+
(functionName)
350+
(arguments
351+
(expression
352+
(function
353+
(functionName)))
354+
(boolean)))))

0 commit comments

Comments
 (0)