Skip to content

Commit

Permalink
Handle left StringValue with right ObjectValue
Browse files Browse the repository at this point in the history
  • Loading branch information
DePasqualeOrg committed Oct 1, 2024
1 parent 6a9ba03 commit 0f062f4
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Sources/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,20 @@ struct Interpreter {
}

if left is StringValue, right is ObjectValue {
throw JinjaError.syntaxNotSupported
switch node.operation.value {
case "in":
if let leftString = (left as? StringValue)?.value,
let rightObject = right as? ObjectValue {
return BooleanValue(value: rightObject.value.keys.contains(leftString))
}
case "not in":
if let leftString = (left as? StringValue)?.value,
let rightObject = right as? ObjectValue {
return BooleanValue(value: !rightObject.value.keys.contains(leftString))
}
default:
throw JinjaError.runtime("Unsupported operation '\(node.operation.value)' between StringValue and ObjectValue")
}
}

throw JinjaError.syntax(
Expand Down Expand Up @@ -664,6 +677,17 @@ struct Interpreter {
throw JinjaError.runtime("Unknown filter: \(node.filter)")
}

func evaluateTestExpression(node: TestExpression, environment: Environment) throws -> any RuntimeValue {
let operand = try self.evaluate(statement: node.operand, environment: environment)

if let testFunction = environment.tests[node.test.value] {
let result = try testFunction(operand)
return BooleanValue(value: node.negate ? !result : result)
} else {
throw JinjaError.runtime("Unknown test: \(node.test.value)")
}
}

func evaluate(statement: Statement?, environment: Environment) throws -> any RuntimeValue {
if let statement {
switch statement {
Expand Down Expand Up @@ -693,6 +717,8 @@ struct Interpreter {
return BooleanValue(value: statement.value)
case let statement as FilterExpression:
return try self.evaluateFilterExpression(node: statement, environment: environment)
case let statement as TestExpression:
return try self.evaluateTestExpression(node: statement, environment: environment)
case is NullLiteral:
return NullValue()
default:
Expand Down

0 comments on commit 0f062f4

Please sign in to comment.