feat(variable): Allow accessing dictionary count

This commit is contained in:
Yonas Kolb
2017-05-02 14:52:55 +08:00
committed by Kyle Fuller
parent 3180b26673
commit 14bac03990
3 changed files with 16 additions and 1 deletions

View File

@@ -9,6 +9,8 @@
- Introduces a new `DictionaryLoader` for loading templates from a Swift
Dictionary.
- Added `in` expression in if tag for strings and arrays of hashable types
- You can now access the amount of items in a dictionary using the `count`
property.
### Bug Fixes

View File

@@ -74,7 +74,11 @@ public struct Variable : Equatable, Resolvable {
if let context = current as? Context {
current = context[bit]
} else if let dictionary = current as? [String: Any] {
current = dictionary[bit]
if bit == "count" {
current = dictionary.count
} else {
current = dictionary[bit]
}
} else if let array = current as? [Any] {
if let index = Int(bit) {
if index >= 0 && index < array.count {

View File

@@ -26,6 +26,9 @@ func testVariable() {
"profiles": [
"github": "kylef",
],
"counter": [
"count": "kylef",
],
"article": Article(author: Person(name: "Kyle"))
])
@@ -107,6 +110,12 @@ func testVariable() {
try expect(result) == "Kyle"
}
$0.it("can get the count of a dictionary") {
let variable = Variable("profiles.count")
let result = try variable.resolve(context) as? Int
try expect(result) == 1
}
#if os(OSX)
$0.it("can resolve a value via KVO") {
let variable = Variable("object.title")