Skip to content

Commit

Permalink
fix: Do not print Optional(...) when rendering arrays (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka authored and kylef committed Apr 5, 2018
1 parent 8fa0bd2 commit 29e859f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Fixed comparing string variables with string literals, in Swift 4 string literals became `Substring` and thus couldn't be directly compared to strings.
- 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(..)`


## 0.10.1
Expand Down
15 changes: 15 additions & 0 deletions Sources/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public class VariableNode : NodeType {
func stringify(_ result: Any?) -> String {
if let result = result as? String {
return result
} else if let array = result as? [Any?] {
return unwrap(array).description
} else if let result = result as? CustomStringConvertible {
return result.description
} else if let result = result as? NSObject {
Expand All @@ -86,3 +88,16 @@ func stringify(_ result: Any?) -> String {

return ""
}

func unwrap(_ array: [Any?]) -> [Any] {
return array.map { (item: Any?) -> Any in
if let item = item {
if let items = item as? [Any?] {
return unwrap(items)
} else {
return item
}
}
else { return item as Any }
}
}
10 changes: 9 additions & 1 deletion Tests/StencilTests/VariableSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func testVariable() {
try expect(result) == "Foo"
}
#endif

$0.it("can resolve a value via reflection") {
let variable = Variable("blog.articles.0.author.name")
let result = try variable.resolve(context) as? String
Expand All @@ -167,5 +167,13 @@ func testVariable() {
try expect(result) == "Jhon"
}

$0.it("does not render Optional") {
var array: [Any?] = [1, nil]
array.append(array)
let context = Context(dictionary: ["values": array])

try expect(VariableNode(variable: "values").render(context)) == "[1, nil, [1, nil]]"
try expect(VariableNode(variable: "values.1").render(context)) == ""
}
}
}

0 comments on commit 29e859f

Please sign in to comment.