Skip to content
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

Fixed tuple subscript by value index #172

Merged
merged 1 commit into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Integer literals now resolve into Int values, not Float
- Fixed accessing properties of optional properties via reflection
- No longer render optional values in arrays as `Optional(..)`
- Fixed subscription tuples by value index, i.e. `{{ tuple.0 }}`


## 0.10.1
Expand Down
2 changes: 1 addition & 1 deletion Sources/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func parseFilterComponents(token: String) -> (String, [Variable]) {

extension Mirror {
func getValue(for key: String) -> Any? {
let result = descendant(key)
let result = descendant(key) ?? Int(key).flatMap({ descendant($0) })
if result == nil {
// go through inheritance chain to reach superclass properties
return superclassMirror?.getValue(for: key)
Expand Down
15 changes: 14 additions & 1 deletion Tests/StencilTests/VariableSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func testVariable() {
"counter": [
"count": "kylef",
],
"article": Article(author: Person(name: "Kyle"))
"article": Article(author: Person(name: "Kyle")),
"tuple": (one: 1, two: 2)
])

#if os(OSX)
Expand Down Expand Up @@ -175,5 +176,17 @@ func testVariable() {
try expect(VariableNode(variable: "values").render(context)) == "[1, nil, [1, nil]]"
try expect(VariableNode(variable: "values.1").render(context)) == ""
}

$0.it("can subscript tuple by index") {
let variable = Variable("tuple.0")
let result = try variable.resolve(context) as? Int
try expect(result) == 1
}

$0.it("can subscript tuple by label") {
let variable = Variable("tuple.two")
let result = try variable.resolve(context) as? Int
try expect(result) == 2
}
}
}