-
Notifications
You must be signed in to change notification settings - Fork 26
Delay throwing exceptions for invalid expressions untill thy actually gets evalutated. #1555
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14d9696
Delay throwing exceptions for invalid expressions untill thy actually…
ivarne 6b3155c
Coderabbit suggesions
ivarne 24c36b5
Remvoe extra using
ivarne 584f48a
Update public api verification
ivarne 231708e
Kudos for Coderabbit to notice a bug in a comment!
ivarne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| using System.Diagnostics; | ||
| using System.Globalization; | ||
| using System.Text.Json; | ||
| using System.Text.RegularExpressions; | ||
|
|
@@ -80,19 +81,21 @@ internal static async Task<ExpressionValue> EvaluateExpression_internal( | |
| ExpressionValue[]? positionalArguments = null | ||
| ) | ||
| { | ||
| if (!expr.IsFunctionExpression) | ||
| if (expr.IsLiteralValue) | ||
| { | ||
| return expr.ValueUnion; | ||
| } | ||
|
|
||
| ValidateExpressionArgs(expr); | ||
| var args = new ExpressionValue[expr.Args.Count]; | ||
| var args = new ExpressionValue[expr.Args.Length]; | ||
| for (var i = 0; i < args.Length; i++) | ||
| { | ||
| args[i] = await EvaluateExpression_internal(state, expr.Args[i], context, positionalArguments); | ||
| } | ||
|
|
||
| ExpressionValue ret = expr.Function switch | ||
| { | ||
| //ExpressionFunction.LITERAL_VALUE => expr.ValueUnion, // Handled above | ||
| ExpressionFunction.dataModel => await DataModel(args, context, state), | ||
| ExpressionFunction.component => await Component(args, context, state), | ||
| ExpressionFunction.countDataElements => CountDataElements(args, state), | ||
|
|
@@ -129,27 +132,31 @@ internal static async Task<ExpressionValue> EvaluateExpression_internal( | |
| ExpressionFunction.argv => Argv(args, positionalArguments), | ||
| ExpressionFunction.gatewayAction => state.GetGatewayAction(), | ||
| ExpressionFunction.language => state.GetLanguage(), | ||
| _ => throw new ExpressionEvaluatorTypeErrorException("Function not implemented", expr.Function, args), | ||
| ExpressionFunction.INVALID => throw new ExpressionEvaluatorTypeErrorException( | ||
| $"Function {expr.Args.FirstOrDefault()} not implemented in backend {expr}" | ||
| ), | ||
| _ => throw new UnreachableException($"Function {(int)expr.Function} not a valid enum value {expr}"), | ||
| }; | ||
| return ret; | ||
| } | ||
|
|
||
| private static void ValidateExpressionArgs(Expression expr) | ||
| { | ||
| // Some functions have restrictions that arguments must be literal values and not subexpressions. | ||
| switch (expr) | ||
| { | ||
| case { Function: ExpressionFunction.dataModel, Args: [_, { IsFunctionExpression: true }] }: | ||
| case { Function: ExpressionFunction.dataModel, Args: [_, { IsLiteralValue: false }] }: | ||
| throw new ExpressionEvaluatorTypeErrorException( | ||
| "The data type must be a string (expressions cannot be used here)" | ||
| ); | ||
| case { Function: ExpressionFunction.@if, Args: [_, _, { IsFunctionExpression: true }, _] }: | ||
| case { Function: ExpressionFunction.@if, Args: [_, _, { IsLiteralValue: false }, _] }: | ||
| throw new ExpressionEvaluatorTypeErrorException("Expected third argument to be \"else\""); | ||
| case { Function: ExpressionFunction.compare, Args: [_, { IsFunctionExpression: true }, _] }: | ||
| case { Function: ExpressionFunction.compare, Args: [_, _, { IsFunctionExpression: true }, _] }: | ||
| case { Function: ExpressionFunction.compare, Args: [_, { IsLiteralValue: false }, _] }: | ||
| case { Function: ExpressionFunction.compare, Args: [_, _, { IsLiteralValue: false }, _] }: | ||
| throw new ExpressionEvaluatorTypeErrorException( | ||
| "Invalid operator (it cannot be an expression or null)" | ||
| ); | ||
| case { Function: ExpressionFunction.compare, Args: [_, { IsFunctionExpression: true }, _, _] }: | ||
| case { Function: ExpressionFunction.compare, Args: [_, { IsLiteralValue: false }, _, _] }: | ||
| throw new ExpressionEvaluatorTypeErrorException( | ||
| "Second argument must be \"not\" when providing 4 arguments in total" | ||
| ); | ||
|
|
@@ -333,7 +340,7 @@ private static bool Contains(ExpressionValue[] args) | |
| date = TimeZoneInfo.ConvertTime(date.Value, timezone); | ||
| } | ||
|
|
||
| string? language = state.GetLanguage(); | ||
| string language = state.GetLanguage(); | ||
| return UnicodeDateTimeTokenConverter.Format( | ||
| date, | ||
| args.Length == 2 ? args[1].ToStringForEquals() : null, | ||
|
|
@@ -653,12 +660,7 @@ private static string Round(ExpressionValue[] args) | |
| ); | ||
| } | ||
|
|
||
| var number = PrepareNumericArg(args[0]); | ||
|
|
||
| if (number is null) | ||
| { | ||
| number = 0; | ||
| } | ||
| var number = PrepareNumericArg(args[0]) ?? 0; | ||
|
|
||
| int precision = 0; | ||
|
|
||
|
|
@@ -667,7 +669,7 @@ private static string Round(ExpressionValue[] args) | |
| precision = (int)(PrepareNumericArg(args[1]) ?? 0); | ||
| } | ||
|
|
||
| return number.Value.ToString($"N{precision}", CultureInfo.InvariantCulture); | ||
| return number.ToString($"N{precision}", CultureInfo.InvariantCulture); | ||
| } | ||
|
|
||
| private static string? UpperCase(ExpressionValue[] args) | ||
|
|
@@ -770,9 +772,17 @@ private static bool PrepareBooleanArg(ExpressionValue arg) | |
| throw new ExpressionEvaluatorTypeErrorException("Expected 1+ argument(s), got 0"); | ||
| } | ||
|
|
||
| var preparedArgs = args.Select(arg => PrepareBooleanArg(arg)).ToArray(); | ||
| // Ensure all args gets converted, because they might throw an Exception | ||
| return preparedArgs.All(a => a); | ||
| var all = true; | ||
| foreach (var arg in args) | ||
| { | ||
| // the LINQ All() method would short-circuit and not evaluate all args, so we do it manually to ensure exceptions are thrown correctly | ||
|
Contributor
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. That's correct, but no need to keep this comment. |
||
| if (!PrepareBooleanArg(arg)) | ||
| { | ||
| all = false; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return all; | ||
| } | ||
|
|
||
| private static async Task<string?> Text( | ||
|
|
@@ -804,9 +814,17 @@ ExpressionValue[] args | |
| throw new ExpressionEvaluatorTypeErrorException("Expected 1+ argument(s), got 0"); | ||
| } | ||
|
|
||
| var preparedArgs = args.Select(arg => PrepareBooleanArg(arg)).ToArray(); | ||
| // Ensure all args gets converted, because they might throw an Exception | ||
| return preparedArgs.Any(a => a); | ||
| bool any = false; | ||
| foreach (var arg in args) | ||
| { | ||
| // the LINQ Any() method would short-circuit and not evaluate all args, so we do it manually to ensure exceptions are thrown correctly | ||
|
Contributor
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. That's correct, but no need to keep this comment. |
||
| if (PrepareBooleanArg(arg)) | ||
| { | ||
| any = true; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return any; | ||
| } | ||
|
|
||
| private static bool? Not(ExpressionValue[] args) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.