Added support for Dot Notation (fixes ISS-349)
This commit is contained in:
parent
f3f3a670bd
commit
39e334a6a4
|
@ -195,7 +195,17 @@ public class MustacheEvaluationContext {
|
||||||
/// - parameter named: The name of the value to find
|
/// - parameter named: The name of the value to find
|
||||||
/// - returns: The value, if found, or nil
|
/// - returns: The value, if found, or nil
|
||||||
public func getValue(named nam: String) -> MapType.Value? {
|
public func getValue(named nam: String) -> MapType.Value? {
|
||||||
let v = mapValues[nam]
|
let values = nam.components(separatedBy: ".")
|
||||||
|
|
||||||
|
var cntxt: MapType.Value? = mapValues
|
||||||
|
for val in values {
|
||||||
|
guard let prev = cntxt as? MapType else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
cntxt = prev[val]
|
||||||
|
}
|
||||||
|
|
||||||
|
let v = cntxt
|
||||||
if v == nil && parent != nil {
|
if v == nil && parent != nil {
|
||||||
return parent?.getValue(named: nam)
|
return parent?.getValue(named: nam)
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,13 +165,51 @@ class PerfectMustacheTests: XCTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testDotNotation1() {
|
||||||
|
let usingTemplate = "TOP {\n{{name.first}} {{name.last}}\n}\nBOTTOM"
|
||||||
|
do {
|
||||||
|
let template = try MustacheParser().parse(string: usingTemplate)
|
||||||
|
let d = ["name": ["first": "The", "last": "name"]] as [String:Any]
|
||||||
|
|
||||||
|
let response = ShimHTTPResponse()
|
||||||
|
|
||||||
|
let context = MustacheWebEvaluationContext(webResponse: response, map: d)
|
||||||
|
let collector = MustacheEvaluationOutputCollector()
|
||||||
|
template.evaluate(context: context, collector: collector)
|
||||||
|
|
||||||
|
XCTAssertEqual(collector.asString(), "TOP {\nThe name\n}\nBOTTOM")
|
||||||
|
} catch {
|
||||||
|
XCTAssert(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDotNotation2() {
|
||||||
|
let usingTemplate = "TOP {\n{{foo.data.name.first}} {{foo.data.name.last}}\n}\nBOTTOM"
|
||||||
|
do {
|
||||||
|
let template = try MustacheParser().parse(string: usingTemplate)
|
||||||
|
let d = ["foo": ["data": ["name": ["first": "The", "last": "name"]]]] as [String:Any]
|
||||||
|
|
||||||
|
let response = ShimHTTPResponse()
|
||||||
|
|
||||||
|
let context = MustacheWebEvaluationContext(webResponse: response, map: d)
|
||||||
|
let collector = MustacheEvaluationOutputCollector()
|
||||||
|
template.evaluate(context: context, collector: collector)
|
||||||
|
|
||||||
|
XCTAssertEqual(collector.asString(), "TOP {\nThe name\n}\nBOTTOM")
|
||||||
|
} catch {
|
||||||
|
XCTAssert(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static var allTests : [(String, (PerfectMustacheTests) -> () throws -> Void)] {
|
static var allTests : [(String, (PerfectMustacheTests) -> () throws -> Void)] {
|
||||||
return [
|
return [
|
||||||
("testMustacheParser1", testMustacheParser1),
|
("testMustacheParser1", testMustacheParser1),
|
||||||
("testMustacheLambda1", testMustacheLambda1),
|
("testMustacheLambda1", testMustacheLambda1),
|
||||||
("testMustacheParser2", testMustacheParser2),
|
("testMustacheParser2", testMustacheParser2),
|
||||||
("testMustacheLambda2", testMustacheLambda2),
|
("testMustacheLambda2", testMustacheLambda2),
|
||||||
("testPartials1", testPartials1)
|
("testPartials1", testPartials1),
|
||||||
|
("testDotNotation1", testDotNotation1),
|
||||||
|
("testDotNotation2", testDotNotation2)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue