Added sum function - #1817
Conversation
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs`:
- Around line 1040-1050: The validation condition in the Sum method uses AND
(&&) when it should use OR (||), which allows invalid arguments to pass
validation. The condition should ensure that both the argument count is exactly
one AND the argument is an array type. Change the && operator to || in the if
statement so that the exception is correctly thrown when either args.Length is
not equal to 1 OR expressionValue.ValueKind is not JsonValueKind.Array,
preventing non-array values from reaching the .Array property access.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e5b24669-1993-4591-8827-4755164870eb
📒 Files selected for processing (4)
src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cssrc/Altinn.App.Core/Models/Expressions/ExpressionFunction.cstest/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cstest/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/sum/sum.json
183d09e to
30b7fa2
Compare
TomasEng
left a comment
There was a problem hiding this comment.
Har ikke noe særlig å utsette på dette, bare noen spørmål om potensielle forenklinger. Husk å markere sjekkboksene i beskrivelsen. Beskrivelsen sier også "the function takes one or multiple lists as arguments", men den tar vel bare én?
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs (1)
1050-1056: 🧹 Nitpick | 🔵 Trivial | 💤 Low valueConsider reordering: validate argument count before accessing element.
The current code calls
FirstOrDefault()before validating the argument count. While functionally correct, accessing the element after validation is cleaner and avoids the unnecessary default-value path.♻️ Suggested reorder
private static double? Sum(ExpressionValue[] args) { - var expressionValue = args.FirstOrDefault(); if (args.Length != 1) { throw new ExpressionEvaluatorTypeErrorException($"Expected 1 argument(s), got {args.Length}"); } + var expressionValue = args[0];🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs` around lines 1050 - 1056, In the Sum method, reorder the logic to validate the argument count before accessing array elements. Move the length validation check (args.Length != 1) to occur before the FirstOrDefault() call on the args array. This ensures that we validate the preconditions first and only then access the expressionValue, making the code cleaner and avoiding unnecessary default-value path execution.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs`:
- Around line 1050-1056: In the Sum method, reorder the logic to validate the
argument count before accessing array elements. Move the length validation check
(args.Length != 1) to occur before the FirstOrDefault() call on the args array.
This ensures that we validate the preconditions first and only then access the
expressionValue, making the code cleaner and avoiding unnecessary default-value
path execution.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1a583011-c510-47b5-afed-cf698c2a6aaf
📒 Files selected for processing (5)
src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cssrc/Altinn.App.Core/Models/Expressions/ExpressionFunction.cstest/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cstest/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/sum/sum.jsontest/Altinn.App.Core.Tests/PublicApiTests.PublicApi_ShouldNotChange_Unintentionally.verified.txt
✅ Files skipped from review due to trivial changes (1)
- test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/shared-tests/functions/sum/sum.json
🚧 Files skipped from review as they are similar to previous changes (2)
- test/Altinn.App.Core.Tests/LayoutExpressions/CommonTests/TestFunctions.cs
- src/Altinn.App.Core/Models/Expressions/ExpressionFunction.cs
|



Description
Adds sum support to the expression engine. The function takes one or multiple lists as argument.
Related Issue(s)
Verification
Documentation
Summary by CodeRabbit
Release Notes
New Features
sumexpression function to calculate the arithmetic total of numeric values in a list. Supports integers, floating-point numbers, negative values, scientific notation (including numeric strings), and mixed numeric types. Empty lists return0; all-null lists return0(nulls are otherwise ignored).Bug Fixes
Tests