Skip to content

Commit

Permalink
address Tess' feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveL-MSFT committed Aug 9, 2024
1 parent 3bf8fbe commit 3f8c6d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 5 additions & 0 deletions dsc/tests/dsc_expressions.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Describe 'Expressions tests' {
It 'Accessors work: <text>' -TestCases @(
@{ text = "[parameters('test').hello]"; expected = '@{world=there}' }
@{ text = "[parameters('test').hello.world]"; expected = 'there' }
@{ text = "[parameters('test').array[0]]"; expected = 'one' }
@{ text = "[parameters('test').array[1][1]]"; expected = 'three' }
@{ 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' }
) {
param($text, $expected)
$yaml = @"
Expand Down
10 changes: 4 additions & 6 deletions dsc_lib/src/parser/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,16 @@ impl Expression {
for accessor in &self.accessors {
match accessor {
Accessor::Member(member) => {
if !value.is_object() {
return Err(DscError::Parser("Member access on non-object value".to_string()));
}
if let Some(object) = value.as_object() {
if !object.contains_key(member) {
return Err(DscError::Parser(format!("Member '{member}' not found")));
}
value = object[member].clone();
} else {
return Err(DscError::Parser("Member access on non-object value".to_string()));
}
},
Accessor::Index(index) => {
if !value.is_array() {
return Err(DscError::Parser("Index access on non-array value".to_string()));
}
if let Some(array) = value.as_array() {
if !index.is_number() {
return Err(DscError::Parser("Index is not a number".to_string()));
Expand All @@ -146,6 +142,8 @@ impl Expression {
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()));
}
},
}
Expand Down

0 comments on commit 3f8c6d3

Please sign in to comment.