-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathBoolishConverter.cs
More file actions
37 lines (30 loc) · 1.54 KB
/
BoolishConverter.cs
File metadata and controls
37 lines (30 loc) · 1.54 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
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using static Expressions.Shortcuts.ExpressionShortcuts;
namespace HandlebarsDotNet.Compiler
{
internal class BoolishConverter : HandlebarsExpressionVisitor
{
private readonly CompilationContext _compilationContext;
public BoolishConverter(CompilationContext compilationContext)
{
_compilationContext = compilationContext;
}
private const string IncludeZero = "includeZero";
protected override Expression VisitBoolishExpression(BoolishExpression bex)
{
var condition = Visit(bex.Condition);
condition = FunctionBuilder.Reduce(condition, _compilationContext, out _);
var hashParameters = (HashParametersExpression)VisitHashParametersExpression(bex.HashParameters);
// Assert that if there is a hashParameter, it is "includeZero" and has a boolean value
if (hashParameters.Parameters.Count > 1 || (hashParameters.Parameters.Count == 1 && !hashParameters.Parameters.ContainsKey(IncludeZero)))
{
throw new HandlebarsCompilerException($"Boolish expression can only have up to one hash parameter, '{IncludeZero}'");
}
var @object = Arg<object>(condition);
var includeZero = Arg<bool>(hashParameters.Parameters.Count == 1 ? hashParameters.Parameters[IncludeZero] : Expression.Constant(false));
return Call(() => HandlebarsUtils.IsTruthyOrNonEmpty(@object, includeZero));
}
}
}