-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add support for case function #4147
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
Changes from all commits
1ff3fb7
2e17c78
c217d5f
089a03d
7d6ee77
b78dee1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #nullable disable // Consider removing in the future to minimize likelihood of NullReferenceException; refer https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references | ||
|
|
||
| using System; | ||
| using GitHub.Actions.Expressions.Data; | ||
|
|
||
| namespace GitHub.DistributedTask.Expressions2.Sdk.Functions | ||
| { | ||
| internal sealed class Case : Function | ||
| { | ||
| protected sealed override Object EvaluateCore( | ||
| EvaluationContext context, | ||
| out ResultMemory resultMemory) | ||
| { | ||
| resultMemory = null; | ||
| // Validate argument count - must be odd (pairs of predicate-result plus default) | ||
| if (Parameters.Count % 2 == 0) | ||
| { | ||
| throw new InvalidOperationException("case requires an odd number of arguments"); | ||
| } | ||
|
|
||
| // Evaluate predicate-result pairs | ||
| for (var i = 0; i < Parameters.Count - 1; i += 2) | ||
| { | ||
| var predicate = Parameters[i].Evaluate(context); | ||
|
|
||
| // Predicate must be a boolean | ||
| if (predicate.Kind != ValueKind.Boolean) | ||
| { | ||
| throw new InvalidOperationException("case predicate must evaluate to a boolean value"); | ||
| } | ||
|
|
||
| // If predicate is true, return the corresponding result | ||
| if ((Boolean)predicate.Value) | ||
| { | ||
| var result = Parameters[i + 1].Evaluate(context); | ||
| return result.Value; | ||
| } | ||
| } | ||
|
|
||
| // No predicate matched, return default (last argument) | ||
| var defaultResult = Parameters[Parameters.Count - 1].Evaluate(context); | ||
| return defaultResult.Value; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,7 @@ protected StringToken EvaluateStringToken( | |
| var originalBytes = context.Memory.CurrentBytes; | ||
| try | ||
| { | ||
| var tree = new ExpressionParser().CreateTree(expression, null, context.GetExpressionNamedValues(), context.ExpressionFunctions); | ||
| var tree = new ExpressionParser().CreateTree(expression, null, context.GetExpressionNamedValues(), context.ExpressionFunctions, allowCaseFunction: context.AllowCaseFunction); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this for the evaluator? Do we hardcode to true for the evaluator? Basically I just want to make sure we set to true for evaluation. If evaluation works E2E then 👍
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, this is defaulted to true but will allow the manifest manager code paths to disable it. |
||
| var options = new EvaluationOptions | ||
| { | ||
| MaxMemory = context.Memory.MaxBytes, | ||
|
|
@@ -94,7 +94,7 @@ protected SequenceToken EvaluateSequenceToken( | |
| var originalBytes = context.Memory.CurrentBytes; | ||
| try | ||
| { | ||
| var tree = new ExpressionParser().CreateTree(expression, null, context.GetExpressionNamedValues(), context.ExpressionFunctions); | ||
| var tree = new ExpressionParser().CreateTree(expression, null, context.GetExpressionNamedValues(), context.ExpressionFunctions, allowCaseFunction: context.AllowCaseFunction); | ||
| var options = new EvaluationOptions | ||
| { | ||
| MaxMemory = context.Memory.MaxBytes, | ||
|
|
@@ -123,7 +123,7 @@ protected MappingToken EvaluateMappingToken( | |
| var originalBytes = context.Memory.CurrentBytes; | ||
| try | ||
| { | ||
| var tree = new ExpressionParser().CreateTree(expression, null, context.GetExpressionNamedValues(), context.ExpressionFunctions); | ||
| var tree = new ExpressionParser().CreateTree(expression, null, context.GetExpressionNamedValues(), context.ExpressionFunctions, allowCaseFunction: context.AllowCaseFunction); | ||
| var options = new EvaluationOptions | ||
| { | ||
| MaxMemory = context.Memory.MaxBytes, | ||
|
|
@@ -152,7 +152,7 @@ protected TemplateToken EvaluateTemplateToken( | |
| var originalBytes = context.Memory.CurrentBytes; | ||
| try | ||
| { | ||
| var tree = new ExpressionParser().CreateTree(expression, null, context.GetExpressionNamedValues(), context.ExpressionFunctions); | ||
| var tree = new ExpressionParser().CreateTree(expression, null, context.GetExpressionNamedValues(), context.ExpressionFunctions, allowCaseFunction: context.AllowCaseFunction); | ||
| var options = new EvaluationOptions | ||
| { | ||
| MaxMemory = context.Memory.MaxBytes, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #nullable disable // Consider removing in the future to minimize likelihood of NullReferenceException; refer https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references | ||
|
|
||
| using System; | ||
| using GitHub.Actions.Expressions.Data; | ||
|
|
||
| namespace GitHub.Actions.Expressions.Sdk.Functions | ||
| { | ||
| internal sealed class Case : Function | ||
| { | ||
| protected sealed override Object EvaluateCore( | ||
| EvaluationContext context, | ||
| out ResultMemory resultMemory) | ||
| { | ||
| resultMemory = null; | ||
| // Validate argument count - must be odd (pairs of predicate-result plus default) | ||
| if (Parameters.Count % 2 == 0) | ||
| { | ||
| throw new InvalidOperationException("case requires an odd number of arguments"); | ||
| } | ||
|
|
||
| // Evaluate predicate-result pairs | ||
| for (var i = 0; i < Parameters.Count - 1; i += 2) | ||
| { | ||
| var predicate = Parameters[i].Evaluate(context); | ||
|
|
||
| // Predicate must be a boolean | ||
| if (predicate.Kind != ValueKind.Boolean) | ||
| { | ||
| throw new InvalidOperationException("case predicate must evaluate to a boolean value"); | ||
| } | ||
|
|
||
| // If predicate is true, return the corresponding result | ||
| if ((Boolean)predicate.Value) | ||
| { | ||
| var result = Parameters[i + 1].Evaluate(context); | ||
| return result.Value; | ||
| } | ||
| } | ||
|
|
||
| // No predicate matched, return default (last argument) | ||
| var defaultResult = Parameters[Parameters.Count - 1].Evaluate(context); | ||
| return defaultResult.Value; | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.