-
-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathIncrementStatement.cs
More file actions
57 lines (44 loc) · 1.67 KB
/
IncrementStatement.cs
File metadata and controls
57 lines (44 loc) · 1.67 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
57
using System.Text.Encodings.Web;
using Fluid.Values;
namespace Fluid.Ast
{
public sealed class IncrementStatement : Statement
{
public const string Prefix = "$$incdec$$$";
public IncrementStatement(string identifier)
{
Identifier = identifier ?? "";
}
public string Identifier { get; }
public override ValueTask<Completion> WriteToAsync(TextWriter writer, TextEncoder encoder, TemplateContext context)
{
context.IncrementSteps();
// We prefix the identifier to prevent collisions with variables.
// Variable identifiers don't represent the same slots as inc/dec ones.
// c.f. https://shopify.github.io/liquid/tags/variable/
var prefixedIdentifier = Prefix + Identifier;
var value = context.GetValue(prefixedIdentifier);
if (value.IsNil())
{
value = NumberValue.Zero;
}
else
{
value = NumberValue.Create(value.ToNumberValue() + 1);
}
context.SetValue(prefixedIdentifier, value);
var task = value.WriteToAsync(writer, encoder, context.CultureInfo);
if (task.IsCompletedSuccessfully)
{
return new ValueTask<Completion>(Completion.Normal);
}
return Awaited(task);
static async ValueTask<Completion> Awaited(ValueTask t)
{
await t;
return Completion.Normal;
}
}
protected internal override Statement Accept(AstVisitor visitor) => visitor.VisitIncrementStatement(this);
}
}