From bb8879a9f5ccec8c9ba9faf728eb55322eafa631 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 8 Jun 2021 14:05:39 +0100 Subject: [PATCH 1/3] Expression -> JMESExpression Expression is too generic a term --- Sources/JMESPath/Expression.swift | 2 +- Sources/JMESPath/Lexer.swift | 2 +- Sources/JMESPath/Parser.swift | 2 +- Sources/JMESPath/Token.swift | 2 +- Tests/JMESPathTests/ComplianceTests.swift | 6 +++--- Tests/JMESPathTests/ErrorTests.swift | 12 ++++++------ Tests/JMESPathTests/MirrorTests.swift | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Sources/JMESPath/Expression.swift b/Sources/JMESPath/Expression.swift index 8678e29..fb5056d 100644 --- a/Sources/JMESPath/Expression.swift +++ b/Sources/JMESPath/Expression.swift @@ -3,7 +3,7 @@ import Foundation /// JMES Expression /// /// Holds a compiled JMES expression and allows you to search Json text or a type already in memory -public struct Expression { +public struct JMESExpression { let ast: Ast public static func compile(_ text: String) throws -> Self { diff --git a/Sources/JMESPath/Lexer.swift b/Sources/JMESPath/Lexer.swift index 56adbd4..5e959a3 100644 --- a/Sources/JMESPath/Lexer.swift +++ b/Sources/JMESPath/Lexer.swift @@ -3,7 +3,7 @@ import Foundation /// Lexer object /// /// Parses raw text to create an array of tokens -class Lexer { +internal class Lexer { var index: String.Index let text: String diff --git a/Sources/JMESPath/Parser.swift b/Sources/JMESPath/Parser.swift index 98b4d98..cff83e9 100644 --- a/Sources/JMESPath/Parser.swift +++ b/Sources/JMESPath/Parser.swift @@ -1,7 +1,7 @@ /// Parser object. /// /// Parses array of tokens to create AST -class Parser { +internal class Parser { let tokens: [Token] var index: Int diff --git a/Sources/JMESPath/Token.swift b/Sources/JMESPath/Token.swift index 75c4f79..9772d97 100644 --- a/Sources/JMESPath/Token.swift +++ b/Sources/JMESPath/Token.swift @@ -1,5 +1,5 @@ /// Represents a lexical token of a JMESPath expression -enum Token: Equatable { +internal enum Token: Equatable { case identifier(String) case quotedIdentifier(String) case number(Int) diff --git a/Tests/JMESPathTests/ComplianceTests.swift b/Tests/JMESPathTests/ComplianceTests.swift index c54fe74..e2ed76a 100644 --- a/Tests/JMESPathTests/ComplianceTests.swift +++ b/Tests/JMESPathTests/ComplianceTests.swift @@ -79,7 +79,7 @@ final class ComplianceTests: XCTestCase { func testBenchmark(_ c: Case) { do { - let expression = try Expression.compile(c.expression) + let expression = try JMESExpression.compile(c.expression) _ = try expression.search(object: self.given.value) } catch { XCTFail("\(error)") @@ -88,7 +88,7 @@ final class ComplianceTests: XCTestCase { func testError(_ c: Case, error: String) { do { - let expression = try Expression.compile(c.expression) + let expression = try JMESExpression.compile(c.expression) _ = try expression.search(object: self.given.value) } catch { return @@ -103,7 +103,7 @@ final class ComplianceTests: XCTestCase { @available(iOS 11.0, tvOS 11.0, watchOS 5.0, *) func testResult(_ c: Case, result: Any?) { do { - let expression = try Expression.compile(c.expression) + let expression = try JMESExpression.compile(c.expression) let resultJson: String? = try result.map { let data = try JSONSerialization.data(withJSONObject: $0, options: [.fragmentsAllowed, .sortedKeys]) diff --git a/Tests/JMESPathTests/ErrorTests.swift b/Tests/JMESPathTests/ErrorTests.swift index cb9c7ab..682e136 100644 --- a/Tests/JMESPathTests/ErrorTests.swift +++ b/Tests/JMESPathTests/ErrorTests.swift @@ -3,7 +3,7 @@ import XCTest final class ErrorTests: XCTestCase { func testUnknownFunction() throws { - let expression = try Expression.compile("unknown(@)") + let expression = try JMESExpression.compile("unknown(@)") XCTAssertThrowsError(try expression.search(object: "test")) { error in switch error { case let error as JMESPathError where error == .runtime("Unknown function name 'unknown'"): @@ -15,7 +15,7 @@ final class ErrorTests: XCTestCase { } func testWrongNumberOfArgs() throws { - let expression = try Expression.compile("reverse(@, @)") + let expression = try JMESExpression.compile("reverse(@, @)") XCTAssertThrowsError(try expression.search(object: "test")) { error in switch error { case let error as JMESPathError where error == .runtime("Invalid number of arguments, expected 1, got 2"): @@ -27,7 +27,7 @@ final class ErrorTests: XCTestCase { } func testWrongArg() throws { - let expression = try Expression.compile("sum(@)") + let expression = try JMESExpression.compile("sum(@)") XCTAssertThrowsError(try expression.search(object: "test")) { error in switch error { case let error as JMESPathError where error == .runtime("Invalid argument, expected array[number], got string"): @@ -39,7 +39,7 @@ final class ErrorTests: XCTestCase { } func testWrongVarArg() throws { - let expression = try Expression.compile("merge(@, i)") + let expression = try JMESExpression.compile("merge(@, i)") XCTAssertThrowsError(try expression.search(json: #"{"i": 24}"#)) { error in switch error { case let error as JMESPathError where error == .runtime("Invalid variadic argument, expected object, got number"): @@ -51,7 +51,7 @@ final class ErrorTests: XCTestCase { } func testMinByWrongType() throws { - let expression = try Expression.compile("min_by(@, &i)") + let expression = try JMESExpression.compile("min_by(@, &i)") XCTAssertThrowsError(try expression.search(json: #"[{"i": true}]"#)) { error in switch error { case let error as JMESPathError where error == .runtime("Invalid argment, expected array values to be strings or numbers, instead got boolean"): @@ -63,7 +63,7 @@ final class ErrorTests: XCTestCase { } func testSortByWrongType() throws { - let expression = try Expression.compile("sort_by(@, &i)") + let expression = try JMESExpression.compile("sort_by(@, &i)") XCTAssertThrowsError(try expression.search(json: #"[{"i": "one"}, {"i": 2}]"#)) { error in switch error { case let error as JMESPathError where error == .runtime("Sort arguments all have to be the same type, expected string, instead got number"): diff --git a/Tests/JMESPathTests/MirrorTests.swift b/Tests/JMESPathTests/MirrorTests.swift index 83a2590..efcbdcb 100644 --- a/Tests/JMESPathTests/MirrorTests.swift +++ b/Tests/JMESPathTests/MirrorTests.swift @@ -4,7 +4,7 @@ import XCTest final class MirrorTests: XCTestCase { func testInterpreter(_ expression: String, data: Any, result: Value) { do { - let expression = try Expression.compile(expression) + let expression = try JMESExpression.compile(expression) let value = try XCTUnwrap(expression.search(object: data, as: Value.self)) XCTAssertEqual(value, result) } catch { From 949693e4eeb0bcf636b787b48fa3a6b59a75fd33 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 8 Jun 2021 14:08:13 +0100 Subject: [PATCH 2/3] Update README.md --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8c7f553..d19d451 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,9 @@ Below is a simple example of usage. ```swift import JMESPath -let expression = try Expression.compile("a.b") +// compile query "a.b" +let expression = try JMESExpression.compile("a.b") +// use query to search json string let result = try expression.search(json: #"{"a": {"b": "hello"}}"#, as: String.self) assert(String == "hello") ``` @@ -22,8 +24,10 @@ struct TestObject { } let a: TestSubObject } -let expression = try Expression.compile("a.b[1]") +// compile query "a.b[1]" +let expression = try JMESExpression.compile("a.b[1]") let test = TestObject(a: .init(b: ["hello", "world!"])) -let result = try expression.search(test, as: String.self) +// use query to search `test` object +let result = try expression.search(object: test, as: String.self) assert(result == "world!") ``` From a71b479ba407e5618d1fe28df442993df61f6773 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Tue, 8 Jun 2021 14:09:21 +0100 Subject: [PATCH 3/3] swift format --- Tests/JMESPathTests/MirrorTests.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/JMESPathTests/MirrorTests.swift b/Tests/JMESPathTests/MirrorTests.swift index efcbdcb..cef21cb 100644 --- a/Tests/JMESPathTests/MirrorTests.swift +++ b/Tests/JMESPathTests/MirrorTests.swift @@ -79,16 +79,16 @@ final class MirrorTests: XCTestCase { func testCustomReflectableArray() { struct TestObject: CustomReflectable { let a: [Int] - var customMirror: Mirror { return Mirror(reflecting: a) } + var customMirror: Mirror { return Mirror(reflecting: self.a) } } - let test = TestObject(a: [1,2,3,4]) + let test = TestObject(a: [1, 2, 3, 4]) self.testInterpreter("[2]", data: test, result: 3) } func testCustomReflectableDictionary() { struct TestObject: CustomReflectable { let d: [String: String] - var customMirror: Mirror { return Mirror(reflecting: d) } + var customMirror: Mirror { return Mirror(reflecting: self.d) } } let test = TestObject(d: ["test": "one", "test2": "two", "test3": "three"]) self.testInterpreter("test2", data: test, result: "two")