-
Notifications
You must be signed in to change notification settings - Fork 26
Support objects in expressions #1777
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
base: support-lists-in-expressions
Are you sure you want to change the base?
Changes from all commits
ab5d0b1
f35a9ea
a5fa375
d14e010
6335458
1b629f7
4f08e2b
e4a90c0
df8320c
2b2a86b
4568156
58c2757
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,61 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
|
|
||
| namespace Altinn.App.Core.Internal.Expressions; | ||
|
|
||
| internal sealed class ObjectFunctionEvaluator | ||
|
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. Du laget vell en mappe for disse klassene som vi diskuterte i den andre PRen?
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. Ja, den blir opprettet i PR-en for Jmespath. Her har vi bare én slik klasse, så da tenker jeg uansett ikke at det er nødvendig med en egen mappe. |
||
| { | ||
| private readonly ExpressionValue[] _args; | ||
|
|
||
| public ObjectFunctionEvaluator(ExpressionValue[] args) => _args = args; | ||
|
|
||
| public JsonObject Evaluate() | ||
|
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. Claude skriver:
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. Jeg kan ikke se hvordan en løkke vil gjøre dette mer oversiktlig. Løkker er vanskelige å lese. Verktøy som CodeQL foreslår jo ofte å bytte ut løkker med nettopp Linq. Jeg kan eventuelt dele opp i flere funksjoner, for eksempel
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. La til disse metodene nå. Det ble mer ryddig. |
||
| { | ||
| AssertEvenNumberOfArguments(); | ||
| string[] keys = ExtractKeys(); | ||
| AssertKeysAreUnique(keys); | ||
| JsonNode?[] values = ExtractValues(); | ||
| Dictionary<string, JsonNode?> keyValuePairs = DictionaryFromKeysAndValues(keys, values); | ||
| return new JsonObject(keyValuePairs); | ||
| } | ||
|
|
||
| private void AssertEvenNumberOfArguments() | ||
| { | ||
| if (_args.Length % 2 == 1) | ||
| { | ||
| throw new ExpressionEvaluatorTypeErrorException( | ||
| "The object function must have an even number of arguments." | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private string[] ExtractKeys() | ||
| { | ||
| try | ||
| { | ||
| return ExtractEvenIndexedArguments().Select(v => v.String).ToArray(); | ||
| } | ||
| catch (InvalidCastException) | ||
| { | ||
| throw new ExpressionEvaluatorTypeErrorException("Object keys must be strings."); | ||
| } | ||
| } | ||
|
|
||
| private ExpressionValue[] ExtractEvenIndexedArguments() => _args.Where((_, index) => index % 2 == 0).ToArray(); | ||
|
|
||
| private static void AssertKeysAreUnique(string[] keys) | ||
| { | ||
| if (keys.Length != keys.Distinct().Count()) | ||
| { | ||
| throw new ExpressionEvaluatorTypeErrorException("Object keys must be unique."); | ||
| } | ||
| } | ||
|
|
||
| private JsonNode?[] ExtractValues() => | ||
| ExtractOddIndexedArguments().Select(v => JsonSerializer.SerializeToNode(v)).ToArray(); | ||
|
|
||
| private ExpressionValue[] ExtractOddIndexedArguments() => _args.Where((_, index) => index % 2 == 1).ToArray(); | ||
|
|
||
| private static Dictionary<string, JsonNode?> DictionaryFromKeysAndValues(string[] keys, JsonNode?[] values) => | ||
| keys.Zip(values, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,4 +228,11 @@ public enum ExpressionFunction | |
|
|
||
| /// <summary>Create a list from the arguments.</summary> | ||
| list, | ||
|
|
||
| /// <summary> | ||
| /// Create a dictionary from the arguments, which must be alternating keys and values. | ||
| /// </summary> | ||
| #pragma warning disable CA1720 | ||
| @object, | ||
| #pragma warning restore CA1720 | ||
|
Comment on lines
+235
to
+237
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. Sonar complained about this variable having a name that contains "object", but we need this for the |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bør kanskje rename Dictionary, nå når det ikke er en Dictionary lenger?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kalte den det fordi Sonar klagde på "Object". Har du noen andre forslag?