feat(variable): Allow accessing dictionary count
This commit is contained in:
@@ -9,6 +9,8 @@
|
|||||||
- Introduces a new `DictionaryLoader` for loading templates from a Swift
|
- Introduces a new `DictionaryLoader` for loading templates from a Swift
|
||||||
Dictionary.
|
Dictionary.
|
||||||
- Added `in` expression in if tag for strings and arrays of hashable types
|
- 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
|
### Bug Fixes
|
||||||
|
|
||||||
|
|||||||
@@ -74,7 +74,11 @@ public struct Variable : Equatable, Resolvable {
|
|||||||
if let context = current as? Context {
|
if let context = current as? Context {
|
||||||
current = context[bit]
|
current = context[bit]
|
||||||
} else if let dictionary = current as? [String: Any] {
|
} else if let dictionary = current as? [String: Any] {
|
||||||
|
if bit == "count" {
|
||||||
|
current = dictionary.count
|
||||||
|
} else {
|
||||||
current = dictionary[bit]
|
current = dictionary[bit]
|
||||||
|
}
|
||||||
} else if let array = current as? [Any] {
|
} else if let array = current as? [Any] {
|
||||||
if let index = Int(bit) {
|
if let index = Int(bit) {
|
||||||
if index >= 0 && index < array.count {
|
if index >= 0 && index < array.count {
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ func testVariable() {
|
|||||||
"profiles": [
|
"profiles": [
|
||||||
"github": "kylef",
|
"github": "kylef",
|
||||||
],
|
],
|
||||||
|
"counter": [
|
||||||
|
"count": "kylef",
|
||||||
|
],
|
||||||
"article": Article(author: Person(name: "Kyle"))
|
"article": Article(author: Person(name: "Kyle"))
|
||||||
])
|
])
|
||||||
|
|
||||||
@@ -107,6 +110,12 @@ func testVariable() {
|
|||||||
try expect(result) == "Kyle"
|
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)
|
#if os(OSX)
|
||||||
$0.it("can resolve a value via KVO") {
|
$0.it("can resolve a value via KVO") {
|
||||||
let variable = Variable("object.title")
|
let variable = Variable("object.title")
|
||||||
|
|||||||
Reference in New Issue
Block a user