From a45812f17c8ef8ecb3c62161fa447e0320ac27ba Mon Sep 17 00:00:00 2001 From: = Date: Thu, 7 Sep 2023 15:52:17 +0200 Subject: [PATCH] fix hidden row --- .../Expressions/ExpressionEvaluator.cs | 37 ++++++++++--------- .../Internal/Expressions/LayoutEvaluator.cs | 1 - .../Models/Expressions/ComponentContext.cs | 6 +-- .../Components/RepeatingGroupComponent.cs | 2 +- .../Models/Layout/PageComponentConverter.cs | 2 +- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs b/src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs index fb90a7e43..cb4657e4b 100644 --- a/src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs +++ b/src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs @@ -21,6 +21,7 @@ public static bool EvaluateBooleanExpression(LayoutEvaluatorState state, Compone var expr = property switch { "hidden" => context.Component.Hidden, + "hiddenRow" => context.Component is RepeatingGroupComponent repeatingGroup ? repeatingGroup.HiddenRow : null, "required" => context.Component.Required, _ => throw new ExpressionEvaluatorTypeErrorException($"unknown boolean expression property {property}") }; @@ -37,7 +38,7 @@ public static bool EvaluateBooleanExpression(LayoutEvaluatorState state, Compone _ => throw new ExpressionEvaluatorTypeErrorException($"Return was not boolean (value)") }; } - catch(Exception e) + catch (Exception e) { throw new ExpressionEvaluatorTypeErrorException($"Error while evaluating \"{property}\" on \"{context.Component.PageId}.{context.Component.Id}\"", e); } @@ -108,10 +109,10 @@ public static bool EvaluateBooleanExpression(LayoutEvaluatorState state, Compone { throw new ArgumentException("component lookup requires the target component to have a simpleBinding"); } - ComponentContext? parent = targetContext; + ComponentContext? parent = targetContext; while (parent is not null) { - if(EvaluateBooleanExpression(state, parent, "hidden", false)) + if (EvaluateBooleanExpression(state, parent, "hidden", false)) { // Don't lookup data in hidden components return null; @@ -135,7 +136,7 @@ private static bool Contains(object?[] args) } string? stringOne = ToStringForEquals(args[0]); string? stringTwo = ToStringForEquals(args[1]); - + if (stringOne is null || stringTwo is null) { return false; @@ -152,7 +153,7 @@ private static bool EndsWith(object?[] args) } string? stringOne = ToStringForEquals(args[0]); string? stringTwo = ToStringForEquals(args[1]); - + if (stringOne is null || stringTwo is null) { return false; @@ -160,7 +161,7 @@ private static bool EndsWith(object?[] args) return stringOne.EndsWith(stringTwo, StringComparison.InvariantCulture); } - + private static bool StartsWith(object?[] args) { if (args.Length != 2) @@ -169,7 +170,7 @@ private static bool StartsWith(object?[] args) } string? stringOne = ToStringForEquals(args[0]); string? stringTwo = ToStringForEquals(args[1]); - + if (stringOne is null || stringTwo is null) { return false; @@ -186,12 +187,12 @@ private static bool CommaContains(object?[] args) } string? stringOne = ToStringForEquals(args[0]); string? stringTwo = ToStringForEquals(args[1]); - + if (stringOne is null || stringTwo is null) { return false; } - + return stringOne.Split(",").Select(s => s.Trim()).Contains(stringTwo, StringComparer.InvariantCulture); } @@ -207,18 +208,18 @@ private static int StringLength(object?[] args) private static string Round(object?[] args) { - if (args.Length < 1 || args.Length> 2) + if (args.Length < 1 || args.Length > 2) { throw new ExpressionEvaluatorTypeErrorException($"Expected 1-2 argument(s), got {args.Length}"); } var number = PrepareNumericArg(args[0]); - - if(number is null) + + if (number is null) { number = 0; } - + int precision = 0; if (args.Length == 2 && args[1] is not null) { @@ -237,7 +238,7 @@ private static string Round(object?[] args) string? stringOne = ToStringForEquals(args[0]); return stringOne?.ToUpperInvariant(); } - + private static string? LowerCase(object?[] args) { if (args.Length != 1) @@ -247,7 +248,7 @@ private static string Round(object?[] args) string? stringOne = ToStringForEquals(args[0]); return stringOne?.ToLowerInvariant(); } - + private static bool PrepareBooleanArg(object? arg) { return arg switch @@ -303,7 +304,7 @@ private static bool PrepareBooleanArg(object? arg) private static bool? Not(object?[] args) { - if(args.Length != 1) + if (args.Length != 1) { throw new ExpressionEvaluatorTypeErrorException($"Expected 1 argument(s), got {args.Length}"); } @@ -323,13 +324,15 @@ private static (double?, double?) PrepareNumericArgs(object?[] args) return (a, b); } - + private static double? PrepareNumericArg(object? arg) { return arg switch { bool ab => throw new ExpressionEvaluatorTypeErrorException($"Expected number, got value {(ab ? "true" : "false")}"), string s => parseNumber(s), + int i => Convert.ToDouble(i), + decimal d => Convert.ToDouble(d), object o => o as double?, // assume all relevant numbers are representable as double (as in frontend) _ => null }; diff --git a/src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs b/src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs index 7e2f89dd3..8dd76ef8b 100644 --- a/src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs +++ b/src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs @@ -43,7 +43,6 @@ private static void HiddenFieldsForRemovalRecurs(LayoutEvaluatorState state, Has var rowHidden = ExpressionEvaluator.EvaluateBooleanExpression(state, rowContext, "hiddenRow", false); hiddenRow.Add(index, rowHidden); } - } foreach (var childContext in context.ChildContexts) diff --git a/src/Altinn.App.Core/Models/Expressions/ComponentContext.cs b/src/Altinn.App.Core/Models/Expressions/ComponentContext.cs index 890608d2f..8048fcaa8 100644 --- a/src/Altinn.App.Core/Models/Expressions/ComponentContext.cs +++ b/src/Altinn.App.Core/Models/Expressions/ComponentContext.cs @@ -12,11 +12,11 @@ public sealed class ComponentContext /// /// Constructor for ComponentContext /// - public ComponentContext(BaseComponent component, int[]? RowIndicies, int? RowLength, IEnumerable? childContexts = null) + public ComponentContext(BaseComponent component, int[]? rowIndices, int? rowLength, IEnumerable? childContexts = null) { Component = component; - RowIndices = RowIndicies; - RowLength = RowLength; + RowIndices = rowIndices; + RowLength = rowLength; ChildContexts = childContexts ?? Enumerable.Empty(); foreach (var child in ChildContexts) { diff --git a/src/Altinn.App.Core/Models/Layout/Components/RepeatingGroupComponent.cs b/src/Altinn.App.Core/Models/Layout/Components/RepeatingGroupComponent.cs index 15e99fcf4..35add8b68 100644 --- a/src/Altinn.App.Core/Models/Layout/Components/RepeatingGroupComponent.cs +++ b/src/Altinn.App.Core/Models/Layout/Components/RepeatingGroupComponent.cs @@ -14,7 +14,7 @@ public RepeatingGroupComponent(string id, string type, IReadOnlyDictionary diff --git a/src/Altinn.App.Core/Models/Layout/PageComponentConverter.cs b/src/Altinn.App.Core/Models/Layout/PageComponentConverter.cs index 320a149a6..607bdc391 100644 --- a/src/Altinn.App.Core/Models/Layout/PageComponentConverter.cs +++ b/src/Altinn.App.Core/Models/Layout/PageComponentConverter.cs @@ -281,7 +281,7 @@ private BaseComponent ReadComponent(ref Utf8JsonReader reader, JsonSerializerOpt case "hidden": hidden = ExpressionConverter.ReadNotNull(ref reader, options); break; - case "hiddenRow": + case "hiddenrow": hiddenRow = ExpressionConverter.ReadNotNull(ref reader, options); break; case "required":