-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathCaseStatement.cs
More file actions
56 lines (44 loc) · 1.65 KB
/
CaseStatement.cs
File metadata and controls
56 lines (44 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System.Text.Encodings.Web;
namespace Fluid.Ast
{
public sealed class CaseStatement : TagStatement
{
private readonly WhenStatement[] _whenStatements;
public CaseStatement(
Expression expression,
ElseStatement elseStatement = null,
WhenStatement[] whenStatements = null
) : base([])
{
Expression = expression;
Else = elseStatement;
_whenStatements = whenStatements ?? [];
}
public Expression Expression { get; }
public ElseStatement Else { get; }
public IReadOnlyList<WhenStatement> Whens => _whenStatements;
public override async ValueTask<Completion> WriteToAsync(TextWriter writer, TextEncoder encoder, TemplateContext context)
{
context.IncrementSteps();
var value = await Expression.EvaluateAsync(context);
var elseShouldBeEvaluated = true;
foreach (var when in _whenStatements)
{
foreach (var option in when.Options)
{
if (value.Equals(await option.EvaluateAsync(context)))
{
elseShouldBeEvaluated = false;
await when.WriteToAsync(writer, encoder, context);
}
}
}
if (elseShouldBeEvaluated && Else != null)
{
await Else.WriteToAsync(writer, encoder, context);
}
return Completion.Normal;
}
protected internal override Statement Accept(AstVisitor visitor) => visitor.VisitCaseStatement(this);
}
}