execute method

  1. @override
JinjaValue execute(
  1. Context ctx
)
override

Executes the statement within the given ctx and returns the result as a JinjaValue.

Implementation

@override
JinjaValue execute(Context ctx) {
  final val = operand.execute(ctx);

  String testName;
  List<JinjaValue> args = [val];
  Map<String, JinjaValue> kwargs = {};

  if (test is Identifier) {
    testName = (test as Identifier).name;
  } else if (test is CallExpression) {
    final call = test as CallExpression;
    if (call.callee is! Identifier) {
      throw Exception('Test must be identifier');
    }
    testName = (call.callee as Identifier).name;
    for (final arg in call.args) {
      if (arg is KeywordArgumentExpression) {
        kwargs[(arg.key as Identifier).name] = arg.val.execute(ctx);
      } else if (arg is Expression) {
        args.add(arg.execute(ctx));
      }
    }
  } else {
    throw Exception('Invalid test expression');
  }

  if (globalTests.containsKey(testName)) {
    final res = globalTests[testName]!(args, kwargs);
    return negate ? JinjaBoolean(!res.asBool) : res;
  }

  throw Exception('Unknown test: $testName');
}