Skip to content

Commit

Permalink
made some change to make it works with core (adobe#14)
Browse files Browse the repository at this point in the history
Co-authored-by: jgeng <[email protected]>
  • Loading branch information
shalehaha and jiabingeng authored Aug 5, 2020
1 parent 49a151e commit 433ec80
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
5 changes: 2 additions & 3 deletions Sources/SwiftRulesEngine/Expression/Operand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 2 additions & 4 deletions Sources/SwiftRulesEngine/Template/MustacheToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
}
}
4 changes: 2 additions & 2 deletions Sources/SwiftRulesEngine/Traversable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ 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 {
subscript(path path: [String]) -> Any? {
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
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftRulesEngineTests/UnitTests/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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<CustomOperand>(mustache: "{{custom}}")
Expand Down
15 changes: 6 additions & 9 deletions Tests/SwiftRulesEngineTests/UnitTests/TraverseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,24 @@ 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
}
}

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
}
}

Expand Down

0 comments on commit 433ec80

Please sign in to comment.