-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
e084500
Support lists in expressions
TomasEng 4a88789
Simplify ReadArray
TomasEng ee7a096
Merge branch 'main' into support-lists-in-expressions
TomasEng b58a3af
Merge branch 'main' into support-lists-in-expressions
TomasEng 739cc4e
Revert irrelevant changes
TomasEng ab5d0b1
Support objects in expressions
TomasEng f24f67b
Use JsonArray
TomasEng 20dbd11
Update public API test file
TomasEng c495caa
Remove unused code
TomasEng 3b5d165
Use correct value in tests
TomasEng f35a9ea
Merge remote-tracking branch 'origin/support-lists-in-expressions' in…
TomasEng a5fa375
Use JsonObject
TomasEng d14e010
Seal ObjectFunctionEvaluator
TomasEng 6335458
Update src/Altinn.App.Core/Internal/Expressions/ExpressionValue.cs
TomasEng 1b629f7
Update src/Altinn.App.Core/Internal/Expressions/ExpressionValue.cs
TomasEng 4f08e2b
Update src/Altinn.App.Core/Models/Expressions/ExpressionFunction.cs
TomasEng e4a90c0
Revert uncomment of unused code
TomasEng df8320c
Refactor ObjectFunctionEvaluator
TomasEng 2b2a86b
Rename variable
TomasEng 4568156
Correct error message
TomasEng 1ee183c
Correct error message
TomasEng 58c2757
Merge branch 'support-lists-in-expressions' into support-objects-in-e…
TomasEng 44ea2b4
Return ExpressionValue from List
TomasEng 219d38b
Merge branch 'support-lists-in-expressions' into support-objects-in-e…
TomasEng d794fcf
Uncomment code in ToStringForText
TomasEng 05ab65e
Merge branch 'support-lists-in-expressions' into support-objects-in-e…
TomasEng 965daad
Rename Dictionary to Object
TomasEng 403f245
Make ObjectFunctionEvaluator static
TomasEng b062e3d
Uncomment code in ToStringForText
TomasEng 8b4cde4
Add data to error messages
TomasEng 7f82123
Return ExpressionValue from Object
TomasEng 9f96406
Merge branch 'main' into support-lists-in-expressions
TomasEng e533215
Merge branch 'support-lists-in-expressions' into support-objects-in-e…
TomasEng aa24c44
Merge branch 'main' into support-objects-in-expressions
TomasEng 84e2800
Fix test file
TomasEng 3ac54d3
Empty commit
TomasEng 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
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
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
70 changes: 70 additions & 0 deletions
70
src/Altinn.App.Core/Internal/Expressions/ObjectFunctionEvaluator.cs
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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
| using Altinn.App.Core.Models.Expressions; | ||
|
|
||
| namespace Altinn.App.Core.Internal.Expressions; | ||
|
|
||
| internal static class ObjectFunctionEvaluator | ||
| { | ||
| public static JsonObject Evaluate(ExpressionValue[] args) | ||
| { | ||
| AssertEvenNumberOfArguments(args); | ||
| string[] keys = ExtractKeys(args); | ||
| AssertKeysAreUnique(keys, args); | ||
| JsonNode?[] values = ExtractValues(args); | ||
| Dictionary<string, JsonNode?> keyValuePairs = DictionaryFromKeysAndValues(keys, values); | ||
| return new JsonObject(keyValuePairs); | ||
| } | ||
|
|
||
| private static void AssertEvenNumberOfArguments(ExpressionValue[] args) | ||
| { | ||
| if (args.Length % 2 == 1) | ||
| { | ||
| throw new ExpressionEvaluatorTypeErrorException( | ||
| "The object function must have an even number of arguments.", | ||
| ExpressionFunction.@object, | ||
| args | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static string[] ExtractKeys(ExpressionValue[] args) | ||
| { | ||
| try | ||
| { | ||
| return ExtractEvenIndexedArguments(args).Select(v => v.String).ToArray(); | ||
| } | ||
| catch (InvalidCastException) | ||
| { | ||
| throw new ExpressionEvaluatorTypeErrorException( | ||
| "Object keys must be strings.", | ||
| ExpressionFunction.@object, | ||
| args | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static ExpressionValue[] ExtractEvenIndexedArguments(ExpressionValue[] args) => | ||
| args.Where((_, index) => index % 2 == 0).ToArray(); | ||
|
|
||
| private static void AssertKeysAreUnique(string[] keys, ExpressionValue[] args) | ||
| { | ||
| if (keys.Length != keys.Distinct().Count()) | ||
| { | ||
| throw new ExpressionEvaluatorTypeErrorException( | ||
| "Object keys must be unique.", | ||
| ExpressionFunction.@object, | ||
| args | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static JsonNode?[] ExtractValues(ExpressionValue[] args) => | ||
| ExtractOddIndexedArguments(args).Select(v => JsonSerializer.SerializeToNode(v)).ToArray(); | ||
|
|
||
| private static ExpressionValue[] ExtractOddIndexedArguments(ExpressionValue[] args) => | ||
| 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); | ||
| } |
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
38 changes: 38 additions & 0 deletions
38
...ore.Tests/LayoutExpressions/CommonTests/shared-tests/functions/component/lookup-list.json
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "name": "Lookup a list", | ||
| "layouts": { | ||
| "Page1": { | ||
| "$schema": "https://altinncdn.no/schemas/json/layout/layout.schema.v1.json", | ||
| "data": { | ||
| "layout": [ | ||
| { | ||
| "id": "some-component", | ||
| "type": "Checkboxes", | ||
| "dataModelBindings": { | ||
| "simpleBinding": "chosenValues" | ||
| }, | ||
| "options": [ | ||
| { | ||
| "value": "a", | ||
| "label": "id_KZuKMBAE5eel" | ||
| }, | ||
| { | ||
| "value": "b", | ||
| "label": "id_Erk5v6GSNDu6" | ||
| }, | ||
| { | ||
| "value": "c", | ||
| "label": "id_6S71AQBlLcPX" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| "dataModel": { | ||
| "chosenValues": ["a", "c"] | ||
| }, | ||
| "expression": ["component", "some-component"], | ||
| "expects": ["a", "c"] | ||
| } |
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.