Allow template filters to throw errors
This commit is contained in:
@@ -47,7 +47,7 @@ public class TextNode : NodeType {
|
||||
}
|
||||
|
||||
public protocol Resolvable {
|
||||
func resolve(context: Context) -> Any?
|
||||
func resolve(context: Context) throws -> Any?
|
||||
}
|
||||
|
||||
public class VariableNode : NodeType {
|
||||
@@ -62,7 +62,7 @@ public class VariableNode : NodeType {
|
||||
}
|
||||
|
||||
public func render(context: Context) throws -> String {
|
||||
let result = variable.resolve(context)
|
||||
let result = try variable.resolve(context)
|
||||
|
||||
if let result = result as? String {
|
||||
return result
|
||||
@@ -99,7 +99,7 @@ public class NowNode : NodeType {
|
||||
|
||||
public func render(context: Context) throws -> String {
|
||||
let date = NSDate()
|
||||
let format = self.format.resolve(context)
|
||||
let format = try self.format.resolve(context)
|
||||
var formatter:NSDateFormatter?
|
||||
|
||||
if let format = format as? NSDateFormatter {
|
||||
@@ -154,7 +154,7 @@ public class ForNode : NodeType {
|
||||
}
|
||||
|
||||
public func render(context: Context) throws -> String {
|
||||
let values = variable.resolve(context)
|
||||
let values = try variable.resolve(context)
|
||||
if let values = values as? NSArray {
|
||||
return try values.map { item in
|
||||
context.push()
|
||||
@@ -227,7 +227,7 @@ public class IfNode : NodeType {
|
||||
}
|
||||
|
||||
public func render(context: Context) throws -> String {
|
||||
let result = variable.resolve(context)
|
||||
let result = try variable.resolve(context)
|
||||
var truthy = false
|
||||
|
||||
if let result = result as? [AnyObject] {
|
||||
|
||||
@@ -10,7 +10,7 @@ public func until(tags:[String])(parser:TokenParser, token:Token) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
public typealias Filter = Any? -> Any?
|
||||
public typealias Filter = Any? throws -> Any?
|
||||
|
||||
/// A class for parsing an array of tokens and converts them into a collection of Node's
|
||||
public class TokenParser {
|
||||
|
||||
@@ -24,11 +24,11 @@ class FilterExpression : Resolvable {
|
||||
}
|
||||
}
|
||||
|
||||
func resolve(context: Context) -> Any? {
|
||||
let result = variable.resolve(context)
|
||||
func resolve(context: Context) throws -> Any? {
|
||||
let result = try variable.resolve(context)
|
||||
|
||||
return filters.reduce(result) { x, y in
|
||||
return y(x)
|
||||
return try filters.reduce(result) { x, y in
|
||||
return try y(x)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ public struct Variable : Equatable, Resolvable {
|
||||
}
|
||||
|
||||
/// Resolve the variable in the given context
|
||||
public func resolve(context:Context) -> Any? {
|
||||
public func resolve(context:Context) throws -> Any? {
|
||||
var current: Any? = context
|
||||
|
||||
if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) {
|
||||
|
||||
Reference in New Issue
Block a user