Skip to content

Commit

Permalink
Merge pull request #2 from adam-fowler/jmes-expression
Browse files Browse the repository at this point in the history
Rename `Expression` to `JMESExpression`
  • Loading branch information
adam-fowler committed Jun 8, 2021
2 parents 3a7d095 + a71b479 commit 64366a8
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 20 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
```
Expand All @@ -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!")
```
2 changes: 1 addition & 1 deletion Sources/JMESPath/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/JMESPath/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Sources/JMESPath/Parser.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Parser object.
///
/// Parses array of tokens to create AST
class Parser {
internal class Parser {
let tokens: [Token]
var index: Int

Expand Down
2 changes: 1 addition & 1 deletion Sources/JMESPath/Token.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Tests/JMESPathTests/ComplianceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand All @@ -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
Expand All @@ -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])
Expand Down
12 changes: 6 additions & 6 deletions Tests/JMESPathTests/ErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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'"):
Expand All @@ -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"):
Expand All @@ -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"):
Expand All @@ -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"):
Expand All @@ -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"):
Expand All @@ -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"):
Expand Down
8 changes: 4 additions & 4 deletions Tests/JMESPathTests/MirrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest
final class MirrorTests: XCTestCase {
func testInterpreter<Value: Equatable>(_ 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 {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 64366a8

Please sign in to comment.