Skip to content

Fix settings file array parsing when no commas are present #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 27 additions & 14 deletions Engine/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,24 +540,37 @@ private static object GetSafeValueFromExpressionAst(ExpressionAst exprAst)
return new object[0];
}

PipelineAst pipelineAst = arrExprAst.SubExpression.Statements[0] as PipelineAst;
if (pipelineAst == null)
var listComponents = new List<object>();
// Arrays can either be array expressions (1, 2, 3) or array literals with statements @(1 `n 2 `n 3)
// Or they can be a combination of these
// We go through each statement (line) in an array and read the whole subarray
// This will also mean that @(1; 2) is parsed as an array of two elements, but there's not much point defending against this
foreach (StatementAst statement in arrExprAst.SubExpression.Statements)
{
throw CreateInvalidDataExceptionFromAst(arrExprAst);
}
var pipelineAst = statement as PipelineAst;
if (pipelineAst == null)
{
throw CreateInvalidDataExceptionFromAst(arrExprAst);
}

ExpressionAst pipelineExpressionAst = pipelineAst.GetPureExpression();
if (pipelineExpressionAst == null)
{
throw CreateInvalidDataExceptionFromAst(arrExprAst);
var pipelineExpressionAst = pipelineAst.GetPureExpression();
if (pipelineExpressionAst == null)
{
throw CreateInvalidDataExceptionFromAst(arrExprAst);
}

object arrayValue = GetSafeValueFromExpressionAst(pipelineExpressionAst);
// We might hit arrays like @(\n1,2,3\n4,5,6), which the parser sees as two statements containing array expressions
if (arrayValue is object[] subArray)
{
listComponents.AddRange(subArray);
continue;
}

listComponents.Add(arrayValue);
}
return listComponents.ToArray();

// Array expressions may not really be arrays (like `@('a')`, which has no ArrayLiteralAst within)
// However, some rules depend on this always being an array
object arrayValue = GetSafeValueFromExpressionAst(pipelineExpressionAst);
return arrayValue.GetType().IsArray
? arrayValue
: new object[] { arrayValue };

case ArrayLiteralAst arrLiteralAst:
return GetSafeValuesFromArrayAst(arrLiteralAst);
Expand Down
5 changes: 5 additions & 0 deletions Tests/Engine/Settings.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ Describe "Settings Class" {
@{ Expr = '@("A","B","C")'; Count = 3 }
@{ Expr = '@()'; Count = 0 }
@{ Expr = '@(7)'; Count = 1 }
@{ Expr = "'1',`n'2',`n'3'"; Count = 3 }
@{ Expr = "@(1`n3`n5`n7)"; Count = 4 }
@{ Expr = "'1',`r`n'2',`r`n'3'"; Count = 3 }
@{ Expr = "@(1`r`n3`r`n5`r`n7)"; Count = 4 }
@{ Expr = "@(1,2,3`n7,8,9)"; Count = 6 }
)

$gsvHashtableTests = @(
Expand Down