Skip to content

Commit

Permalink
fix hidden row
Browse files Browse the repository at this point in the history
  • Loading branch information
bjosttveit committed Sep 11, 2023
1 parent f8cb24a commit a3de74c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
37 changes: 20 additions & 17 deletions src/Altinn.App.Core/Internal/Expressions/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
};
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -152,15 +153,15 @@ 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;
}

return stringOne.EndsWith(stringTwo, StringComparison.InvariantCulture);
}

private static bool StartsWith(object?[] args)
{
if (args.Length != 2)
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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)
{
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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}");
}
Expand All @@ -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
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/Altinn.App.Core/Models/Expressions/ComponentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public sealed class ComponentContext
/// <summary>
/// Constructor for ComponentContext
/// </summary>
public ComponentContext(BaseComponent component, int[]? RowIndicies, int? RowLength, IEnumerable<ComponentContext>? childContexts = null)
public ComponentContext(BaseComponent component, int[]? rowIndices, int? rowLength, IEnumerable<ComponentContext>? childContexts = null)
{
Component = component;
RowIndices = RowIndicies;
RowLength = RowLength;
RowIndices = rowIndices;
RowLength = rowLength;
ChildContexts = childContexts ?? Enumerable.Empty<ComponentContext>();
foreach (var child in ChildContexts)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public RepeatingGroupComponent(string id, string type, IReadOnlyDictionary<strin
base(id, type, dataModelBindings, children, childIDs, hidden, required, readOnly, additionalProperties)
{
MaxCount = maxCount;
HiddenRow = hidden;
HiddenRow = hiddenRow;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit a3de74c

Please sign in to comment.