Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dsc/tests/dsc_expressions.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Describe 'Expressions tests' {
@{ text = "[parameters('test').objectArray[0].name]"; expected = 'one' }
@{ text = "[parameters('test').objectArray[1].value[0]]"; expected = '2' }
@{ text = "[parameters('test').objectArray[1].value[1].name]"; expected = 'three' }
@{ text = "[parameters('test').index]"; expected = '1' }
@{ text = "[parameters('test').objectArray[parameters('test').index].name]"; expected = 'two' }
) {
param($text, $expected)
$yaml = @"
Expand All @@ -18,6 +20,7 @@ parameters:
test:
type: object
defaultValue:
index: 1
hello:
world: there
array:
Expand All @@ -38,9 +41,9 @@ resources:
properties:
output: "$text"
"@
$debug = $yaml | dsc -l debug config get -f yaml 2>&1 | Out-String
$debug = $yaml | dsc -l trace config get -f yaml 2>&1 | Out-String
$out = $yaml | dsc config get | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$LASTEXITCODE | Should -Be 0 -Because $debug
$out.results[0].result.actualState.output | Should -Be $expected -Because $debug
}

Expand Down
42 changes: 28 additions & 14 deletions dsc_lib/src/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::parser::functions::Function;
pub enum Accessor {
Member(String),
Index(Value),
IndexExpression(Expression),
}

#[derive(Clone)]
Expand Down Expand Up @@ -73,7 +74,8 @@ impl Expression {
Accessor::Index(value)
},
"expression" => {
return Err(DscError::Parser("Expression index not supported".to_string()));
let expression = Expression::new(statement_bytes, &index_value)?;
Accessor::IndexExpression(expression)
},
_ => {
return Err(DscError::Parser(format!("Invalid accessor kind: '{accessor_kind}'")));
Expand Down Expand Up @@ -118,6 +120,7 @@ impl Expression {
debug!("Evaluating accessors");
let mut value = result;
for accessor in &self.accessors {
let mut index = Value::Null;
match accessor {
Accessor::Member(member) => {
if let Some(object) = value.as_object() {
Expand All @@ -129,20 +132,31 @@ impl Expression {
return Err(DscError::Parser("Member access on non-object value".to_string()));
}
},
Accessor::Index(index) => {
if let Some(array) = value.as_array() {
let Some(index) = index.as_u64() else {
return Err(DscError::Parser("Index is not a valid number".to_string()));
};
let index = usize::try_from(index)?;
if index >= array.len() {
return Err(DscError::Parser("Index out of bounds".to_string()));
}
value = array[index].clone();
} else {
return Err(DscError::Parser("Index access on non-array value".to_string()));
}
Accessor::Index(index_value) => {
index = index_value.clone();
},
Accessor::IndexExpression(expression) => {
index = expression.invoke(function_dispatcher, context)?;
trace!("Expression result: '{:?}'", index);
},
}

if index.is_number() {
if let Some(array) = value.as_array() {
let Some(index) = index.as_u64() else {
return Err(DscError::Parser("Index is not a valid number".to_string()));
};
let index = usize::try_from(index)?;
if index >= array.len() {
return Err(DscError::Parser("Index out of bounds".to_string()));
}
value = array[index].clone();
} else {
return Err(DscError::Parser("Index access on non-array value".to_string()));
}
}
else if !index.is_null() {
return Err(DscError::Parser("Invalid index type".to_string()));
}
}

Expand Down