From 433ec80ba9d053cfb791a34f27d06ddcbebc9d0a Mon Sep 17 00:00:00 2001 From: Jiabin Geng Date: Wed, 5 Aug 2020 13:27:19 -0600 Subject: [PATCH] made some change to make it works with core (#14) Co-authored-by: jgeng --- Sources/SwiftRulesEngine/Expression/Operand.swift | 5 ++--- .../SwiftRulesEngine/Template/MustacheToken.swift | 6 ++---- Sources/SwiftRulesEngine/Traversable.swift | 4 ++-- .../UnitTests/ExpressionTests.swift | 6 +++--- .../UnitTests/TraverseTests.swift | 15 ++++++--------- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/Sources/SwiftRulesEngine/Expression/Operand.swift b/Sources/SwiftRulesEngine/Expression/Operand.swift index d14598d..946d689 100644 --- a/Sources/SwiftRulesEngine/Expression/Operand.swift +++ b/Sources/SwiftRulesEngine/Expression/Operand.swift @@ -64,9 +64,8 @@ extension MustacheToken { case let .function(name, innerToken): let innerValue = innerToken.resolve(in: context) return context.transformer.transform(name: name, parameter: innerValue ?? "") - case let .variable(name): - let path = name.components(separatedBy: ".") - return context.data[path: path] + case let .variable(path): + return context.data.get(key: path) } } } diff --git a/Sources/SwiftRulesEngine/Template/MustacheToken.swift b/Sources/SwiftRulesEngine/Template/MustacheToken.swift index 2541c2f..eb693e0 100644 --- a/Sources/SwiftRulesEngine/Template/MustacheToken.swift +++ b/Sources/SwiftRulesEngine/Template/MustacheToken.swift @@ -19,8 +19,7 @@ public indirect enum MustacheToken { case function(content: String, inner: MustacheToken) public init(_ tokenString: String) { - if let range = tokenString.range(of: #"\((.*\))+"#, - options: .regularExpression) { + if let range = tokenString.range(of: #"\((.*\))+"#, options: .regularExpression) { let variable = String(tokenString[tokenString.index(after: range.lowerBound) ..< tokenString.index(before: range.upperBound)]).trimmingCharacters(in: .whitespacesAndNewlines) let funtionName = String(tokenString[tokenString.startIndex ... tokenString.index(before: range.lowerBound)]).trimmingCharacters(in: .whitespacesAndNewlines) self = .function(content: funtionName, inner: .variable(text: variable)) @@ -35,8 +34,7 @@ public indirect enum MustacheToken { let innerValue = innerToken.resolve(in: transformer, data: data) return transformer.transform(name: name, parameter: innerValue ?? "") case let .variable(name): - let path = name.components(separatedBy: ".") - return data[path: path] + return data.get(key: name) } } } diff --git a/Sources/SwiftRulesEngine/Traversable.swift b/Sources/SwiftRulesEngine/Traversable.swift index 0c1d15e..5db00e5 100644 --- a/Sources/SwiftRulesEngine/Traversable.swift +++ b/Sources/SwiftRulesEngine/Traversable.swift @@ -14,7 +14,7 @@ import Foundation /// A type that can be traversed by the rules engine to retrieve a certain key/value pair. public protocol Traversable { - subscript(traverse _: String) -> Any? { get } + func get(key: String) -> Any? } extension Traversable { @@ -22,7 +22,7 @@ extension Traversable { let result = path.reduce(self as Any?) { switch $0 { case is Traversable: - return ($0 as! Traversable)[traverse: $1] + return ($0 as! Traversable).get(key: $1) default: return nil } diff --git a/Tests/SwiftRulesEngineTests/UnitTests/ExpressionTests.swift b/Tests/SwiftRulesEngineTests/UnitTests/ExpressionTests.swift index 2ff519b..8d496f4 100644 --- a/Tests/SwiftRulesEngineTests/UnitTests/ExpressionTests.swift +++ b/Tests/SwiftRulesEngineTests/UnitTests/ExpressionTests.swift @@ -15,8 +15,8 @@ import XCTest @testable import SwiftRulesEngine struct CustomOperand: Traversable { - subscript(traverse sub: String) -> Any? { - sub + func get(key:String) -> Any? { + return key } } @@ -92,7 +92,7 @@ class ExpressionTests: XCTestCase { func testMustache_Custom() { let evaluator = ConditionEvaluator(options: .defaultOptions) evaluator.addUnaryOperator(operation: "isAmazing") { (lhs: CustomOperand) -> Bool in - (lhs[traverse: "test"] as? String) == "test" + (lhs.get(key: "test") as? String) == "test" } let mustache = Operand(mustache: "{{custom}}") diff --git a/Tests/SwiftRulesEngineTests/UnitTests/TraverseTests.swift b/Tests/SwiftRulesEngineTests/UnitTests/TraverseTests.swift index c2e24a9..4948553 100644 --- a/Tests/SwiftRulesEngineTests/UnitTests/TraverseTests.swift +++ b/Tests/SwiftRulesEngineTests/UnitTests/TraverseTests.swift @@ -16,8 +16,8 @@ import Foundation import XCTest extension Array: Traversable { - public subscript(traverse sub: String) -> Any? { - if let index = Int(sub) { + public func get(key: String) -> Any? { + if let index = Int(key) { return self[index] } return nil @@ -25,18 +25,15 @@ extension Array: Traversable { } extension Dictionary: Traversable where Key == String { - public subscript(traverse sub: String) -> Any? { - let result = self[sub] - if result is AnyCodable { - return (result as! AnyCodable).value - } + public func get(key: String) -> Any? { + let result = self[key] return result } } struct CustomTraverse: Traversable { - subscript(traverse sub: String) -> Any? { - sub + public func get(key: String) -> Any? { + key } }