-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow conditions in variable node #243
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,18 +58,64 @@ public protocol Resolvable { | |
public class VariableNode : NodeType { | ||
public let variable: Resolvable | ||
public var token: Token? | ||
let condition: Expression? | ||
let elseExpression: Resolvable? | ||
|
||
class func parse(_ parser:TokenParser, token:Token) throws -> NodeType { | ||
var components = token.components() | ||
|
||
func hasToken(_ token: String, at index: Int) -> Bool { | ||
return components.count > (index + 1) && components[index] == token | ||
} | ||
|
||
let condition: Expression? | ||
let elseExpression: Resolvable? | ||
|
||
if hasToken("if", at: 1) { | ||
let components = components.suffix(from: 2) | ||
if let elseIndex = components.index(of: "else") { | ||
condition = try parseExpression(components: Array(components.prefix(upTo: elseIndex)), tokenParser: parser, token: token) | ||
let elseToken = components.suffix(from: elseIndex.advanced(by: 1)).joined(separator: " ") | ||
elseExpression = try parser.compileResolvable(elseToken, containedIn: token) | ||
} else { | ||
condition = try parseExpression(components: Array(components), tokenParser: parser, token: token) | ||
elseExpression = nil | ||
} | ||
} else { | ||
condition = nil | ||
elseExpression = nil | ||
} | ||
|
||
let filter = try parser.compileResolvable(components[0], containedIn: token) | ||
return VariableNode(variable: filter, token: token, condition: condition, elseExpression: elseExpression) | ||
} | ||
|
||
public init(variable: Resolvable, token: Token? = nil) { | ||
self.variable = variable | ||
self.token = token | ||
self.condition = nil | ||
self.elseExpression = nil | ||
} | ||
|
||
init(variable: Resolvable, token: Token? = nil, condition: Expression?, elseExpression: Resolvable?) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
self.variable = variable | ||
self.token = token | ||
self.condition = condition | ||
self.elseExpression = elseExpression | ||
} | ||
|
||
public init(variable: String, token: Token? = nil) { | ||
self.variable = Variable(variable) | ||
self.token = token | ||
self.condition = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
self.elseExpression = nil | ||
} | ||
|
||
public func render(_ context: Context) throws -> String { | ||
if let condition = self.condition, try condition.evaluate(context: context) == false { | ||
return try elseExpression?.resolve(context).map(stringify) ?? "" | ||
} | ||
|
||
let result = try variable.resolve(context) | ||
return stringify(result) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nitpick, but I think this is clearer:
https://github.com/SwiftGen/StencilSwiftKit/blob/master/Sources/StencilSwiftKit/MapNode.swift#L21
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find
~=
harder to grasp than>
🤷♂️