From 828a45939e95dbff016fa0d907a053e0ec6ed4eb Mon Sep 17 00:00:00 2001 From: steve benedick Date: Wed, 6 Jan 2021 16:32:46 -0700 Subject: [PATCH] -fix linter and format warnings --- Sources/AEPRulesEngine/ConditionEvaluator.swift | 14 +++++++------- Sources/AEPRulesEngine/Context.swift | 2 +- .../Expression/ComparisonExpression.swift | 2 +- Sources/AEPRulesEngine/Expression/Operand.swift | 4 ++-- Sources/AEPRulesEngine/Log/Log.swift | 2 +- Sources/AEPRulesEngine/Log/LogLevel.swift | 6 +++--- Sources/AEPRulesEngine/RulesFailure.swift | 2 +- .../AEPRulesEngine/Template/MustacheError.swift | 4 ++-- .../Template/ParserTagDelimiters.swift | 12 ++++++------ .../AEPRulesEngine/Template/TemplateParser.swift | 2 +- Sources/AEPRulesEngine/Transformer.swift | 10 +++++----- Sources/AEPRulesEngine/Transforming.swift | 1 - .../UnitTests/OperandTests.swift | 13 ++++++------- .../UnitTests/ParserTests.swift | 4 ++-- .../UnitTests/RulesEngineLogLevelTests.swift | 1 - .../UnitTests/RulesEngineTests.swift | 2 +- 16 files changed, 39 insertions(+), 42 deletions(-) diff --git a/Sources/AEPRulesEngine/ConditionEvaluator.swift b/Sources/AEPRulesEngine/ConditionEvaluator.swift index cde5a90..f6820b3 100644 --- a/Sources/AEPRulesEngine/ConditionEvaluator.swift +++ b/Sources/AEPRulesEngine/ConditionEvaluator.swift @@ -13,11 +13,12 @@ public class ConditionEvaluator: Evaluating { fileprivate let LOG_TAG = "ConditionEvaluator" var operators: [String: Any] = [:] - + // MARK: - Evaluating + public func evaluate(operation: String, lhs: A) -> Result { let op = operators[getHash(operation: operation, typeA: A.self)] as? ((A) -> Bool) - + guard let op_ = op else { let message = "No operator defined for \(getHash(operation: operation, typeA: A.self))" Log.trace(label: LOG_TAG, message) @@ -38,16 +39,16 @@ public class ConditionEvaluator: Evaluating { } } -extension ConditionEvaluator { - public func addUnaryOperator(operation: String, closure: @escaping (A) -> Bool) { +public extension ConditionEvaluator { + func addUnaryOperator(operation: String, closure: @escaping (A) -> Bool) { operators[getHash(operation: operation, typeA: A.self)] = closure } - public func addComparisonOperator(operation: String, closure: @escaping (A, B) -> Bool) { + func addComparisonOperator(operation: String, closure: @escaping (A, B) -> Bool) { operators[getHash(operation: operation, typeA: A.self, typeB: B.self)] = closure } - public func addComparisonOperator(operation: String, type _: A.Type, closure: @escaping (A, A) -> Bool) { + func addComparisonOperator(operation: String, type _: A.Type, closure: @escaping (A, A) -> Bool) { operators[getHash(operation: operation, typeA: A.self, typeB: A.self)] = closure } @@ -81,7 +82,6 @@ public extension ConditionEvaluator { } private func addDefaultOperators() { - addComparisonOperator(operation: "and", type: Bool.self, closure: { $0 && $1 }) addComparisonOperator(operation: "or", type: Bool.self, closure: { $0 || $1 }) diff --git a/Sources/AEPRulesEngine/Context.swift b/Sources/AEPRulesEngine/Context.swift index 965b743..ea68a69 100644 --- a/Sources/AEPRulesEngine/Context.swift +++ b/Sources/AEPRulesEngine/Context.swift @@ -3,7 +3,7 @@ This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language diff --git a/Sources/AEPRulesEngine/Expression/ComparisonExpression.swift b/Sources/AEPRulesEngine/Expression/ComparisonExpression.swift index b9e604f..4f2bd83 100644 --- a/Sources/AEPRulesEngine/Expression/ComparisonExpression.swift +++ b/Sources/AEPRulesEngine/Expression/ComparisonExpression.swift @@ -25,7 +25,7 @@ public struct ComparisonExpression: Evaluable { } // MARK: - Evaluable - + public func evaluate(in context: Context) -> Result { Log.trace(label: LOG_TAG, "Evaluating \(lhs) - \(operationName) - \(rhs)") let resolvedLhs = lhs(context) diff --git a/Sources/AEPRulesEngine/Expression/Operand.swift b/Sources/AEPRulesEngine/Expression/Operand.swift index 9d3df7b..4538604 100644 --- a/Sources/AEPRulesEngine/Expression/Operand.swift +++ b/Sources/AEPRulesEngine/Expression/Operand.swift @@ -34,8 +34,8 @@ public enum Operand { } } -extension Operand { - public init(mustache: String) { +public extension Operand { + init(mustache: String) { let tokens = try? TemplateParser.parse(mustache).get() if let tokens = tokens, tokens.count > 0, case let .mustache(token) = tokens[0].type { self = .token(token) diff --git a/Sources/AEPRulesEngine/Log/Log.swift b/Sources/AEPRulesEngine/Log/Log.swift index c24280f..74f2e2b 100644 --- a/Sources/AEPRulesEngine/Log/Log.swift +++ b/Sources/AEPRulesEngine/Log/Log.swift @@ -15,7 +15,7 @@ import Foundation /// The `Log` class will be dormant unless its static `logging` variable is initialized. /// To enable logging from the RulesEngine, implement a class that conforms to the /// `Logging` protocol and use an instance of it to set the `Log.logging` variable. -public class Log { +public enum Log { public static var logging: Logging? /// Used to print more verbose information. /// - Parameters: diff --git a/Sources/AEPRulesEngine/Log/LogLevel.swift b/Sources/AEPRulesEngine/Log/LogLevel.swift index 221e7a5..2b23790 100644 --- a/Sources/AEPRulesEngine/Log/LogLevel.swift +++ b/Sources/AEPRulesEngine/Log/LogLevel.swift @@ -3,7 +3,7 @@ This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language @@ -17,7 +17,7 @@ public enum LogLevel: Int, Comparable { case warning = 1 case debug = 2 case trace = 3 - + /// Compares two `LogLevel`s for order /// - Parameters: /// - lhs: the first `LogLevel` to be compared @@ -26,7 +26,7 @@ public enum LogLevel: Int, Comparable { public static func < (lhs: LogLevel, rhs: LogLevel) -> Bool { lhs.rawValue < rhs.rawValue } - + public func toString() -> String { switch self { case .trace: diff --git a/Sources/AEPRulesEngine/RulesFailure.swift b/Sources/AEPRulesEngine/RulesFailure.swift index fc47864..812eeab 100644 --- a/Sources/AEPRulesEngine/RulesFailure.swift +++ b/Sources/AEPRulesEngine/RulesFailure.swift @@ -3,7 +3,7 @@ This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language diff --git a/Sources/AEPRulesEngine/Template/MustacheError.swift b/Sources/AEPRulesEngine/Template/MustacheError.swift index d0d9ddc..f2770fd 100644 --- a/Sources/AEPRulesEngine/Template/MustacheError.swift +++ b/Sources/AEPRulesEngine/Template/MustacheError.swift @@ -3,7 +3,7 @@ This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language @@ -15,7 +15,7 @@ import Foundation public struct MustacheError: Error { /// Eventual error message public let message: String? - + public init(message: String? = nil) { self.message = message } diff --git a/Sources/AEPRulesEngine/Template/ParserTagDelimiters.swift b/Sources/AEPRulesEngine/Template/ParserTagDelimiters.swift index 00de45f..5adfae5 100644 --- a/Sources/AEPRulesEngine/Template/ParserTagDelimiters.swift +++ b/Sources/AEPRulesEngine/Template/ParserTagDelimiters.swift @@ -3,7 +3,7 @@ This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language @@ -14,23 +14,23 @@ import Foundation struct ParserTagDelimiters { let tagDelimiterPair: DelimiterPair - + init(_ tagDelimiterPair: DelimiterPair) { self.tagDelimiterPair = tagDelimiterPair } - + var startTag: String { return tagDelimiterPair.0 } - + var startTagLength: Int { return startTag.count } - + var endTag: String { return tagDelimiterPair.1 } - + var endTagLength: Int { return endTag.count } diff --git a/Sources/AEPRulesEngine/Template/TemplateParser.swift b/Sources/AEPRulesEngine/Template/TemplateParser.swift index 3cbc3cb..470b768 100644 --- a/Sources/AEPRulesEngine/Template/TemplateParser.swift +++ b/Sources/AEPRulesEngine/Template/TemplateParser.swift @@ -15,7 +15,7 @@ import Foundation /// A pair of tag delimiters, such as `("{{", "}}")`. public typealias DelimiterPair = (String, String) -public struct TemplateParser { +public enum TemplateParser { static let DefaultTagDelimiterPair: DelimiterPair = ("{{", "}}") static func parse(_ templateString: String, tagDelimiterPair: DelimiterPair = TemplateParser.DefaultTagDelimiterPair) -> Result<[Segment], Error> { diff --git a/Sources/AEPRulesEngine/Transformer.swift b/Sources/AEPRulesEngine/Transformer.swift index dc5e53c..b6a8ace 100644 --- a/Sources/AEPRulesEngine/Transformer.swift +++ b/Sources/AEPRulesEngine/Transformer.swift @@ -3,7 +3,7 @@ This file is licensed to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language @@ -14,15 +14,15 @@ import Foundation public class Transformer: Transforming { var transformations: [String: Transformation] = [:] - + public init() {} - + public func register(name: String, transformation: @escaping Transformation) { transformations[name] = transformation } - + // MARK: - Transforming - + public func transform(name: String, parameter: Any) -> Any { guard let transformation = transformations[name] else { return parameter diff --git a/Sources/AEPRulesEngine/Transforming.swift b/Sources/AEPRulesEngine/Transforming.swift index 47e6969..b28be3e 100644 --- a/Sources/AEPRulesEngine/Transforming.swift +++ b/Sources/AEPRulesEngine/Transforming.swift @@ -17,4 +17,3 @@ public typealias Transformation = (Any) -> Any public protocol Transforming { func transform(name: String, parameter: Any) -> Any } - diff --git a/Tests/AEPRulesEngineTests/UnitTests/OperandTests.swift b/Tests/AEPRulesEngineTests/UnitTests/OperandTests.swift index d542cf4..b963dcb 100644 --- a/Tests/AEPRulesEngineTests/UnitTests/OperandTests.swift +++ b/Tests/AEPRulesEngineTests/UnitTests/OperandTests.swift @@ -16,23 +16,22 @@ import XCTest @testable import AEPRulesEngine class OperandTests: XCTestCase { - func testMustacheNoneValue() { let mustache = Operand(mustache: "") XCTAssertEqual("", String(describing: mustache)) } - - func testDoubleValue(){ + + func testDoubleValue() { let operand = Operand(floatLiteral: 1.2) XCTAssertEqual("", String(describing: operand)) } - - func testBoolValue(){ + + func testBoolValue() { let operand = Operand(booleanLiteral: true) XCTAssertEqual("", String(describing: operand)) } - - func testNilValue(){ + + func testNilValue() { let operand: Operand = nil XCTAssertEqual("", String(describing: operand)) } diff --git a/Tests/AEPRulesEngineTests/UnitTests/ParserTests.swift b/Tests/AEPRulesEngineTests/UnitTests/ParserTests.swift index b201e70..73f4014 100644 --- a/Tests/AEPRulesEngineTests/UnitTests/ParserTests.swift +++ b/Tests/AEPRulesEngineTests/UnitTests/ParserTests.swift @@ -34,9 +34,9 @@ class ParserTests: XCTestCase { let result = template.render(data: ["test": "value"], transformers: Transformer()) XCTAssertEqual("sdfdfdvalueaaa", result) } - + func testCustomizedTokenFormat() { - let template = Template(templateString: "sdfdfd[[test]]aaa", tagDelimiterPair: ("[[","]]")) + let template = Template(templateString: "sdfdfd[[test]]aaa", tagDelimiterPair: ("[[", "]]")) let result = template.render(data: ["test": "value"], transformers: Transformer()) XCTAssertEqual("sdfdfdvalueaaa", result) } diff --git a/Tests/AEPRulesEngineTests/UnitTests/RulesEngineLogLevelTests.swift b/Tests/AEPRulesEngineTests/UnitTests/RulesEngineLogLevelTests.swift index 5e71e9e..18ce06b 100644 --- a/Tests/AEPRulesEngineTests/UnitTests/RulesEngineLogLevelTests.swift +++ b/Tests/AEPRulesEngineTests/UnitTests/RulesEngineLogLevelTests.swift @@ -14,7 +14,6 @@ import XCTest class RulesEngineLogLevelTests: XCTestCase { - func testLogLevelComparison() { XCTAssertTrue(LogLevel.error < LogLevel.warning) XCTAssertTrue(LogLevel.warning < LogLevel.debug) diff --git a/Tests/AEPRulesEngineTests/UnitTests/RulesEngineTests.swift b/Tests/AEPRulesEngineTests/UnitTests/RulesEngineTests.swift index 265e00f..b71fd99 100644 --- a/Tests/AEPRulesEngineTests/UnitTests/RulesEngineTests.swift +++ b/Tests/AEPRulesEngineTests/UnitTests/RulesEngineTests.swift @@ -50,7 +50,7 @@ class RulesEngineTests: XCTestCase { let rule = TestRule(condition: andCondition) rulesEngine.addRules(rules: [rule]) var passed = true - var error:RulesFailure? + var error: RulesFailure? rulesEngine.trace { result, _, _, failure in passed = result error = failure