Skip to content

Commit

Permalink
Add search functions that take json as Data
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jun 2, 2021
1 parent 8060f42 commit 0e70044
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
41 changes: 40 additions & 1 deletion Sources/JMESPath/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ public struct Expression {
return self.init(ast)
}

/// Search JSON
///
/// - Parameters:
/// - any: JSON to search
/// - as: Swift type to return
/// - runtime: JMES runtime (includes functions)
/// - Throws: JMESPathError
/// - Returns: Search result
public func search<Value>(json: Data, as: Value.Type = Value.self, runtime: JMESRuntime = .init()) throws -> Value? {
return try self.search(json: json, runtime: runtime) as? Value
}

/// Search JSON
///
/// - Parameters:
Expand All @@ -26,7 +38,22 @@ public struct Expression {
return try self.search(json: json, runtime: runtime) as? Value
}

/// Search Swift type
/// Search JSON
///
/// - Parameters:
/// - any: Swift type to search
/// - as: Swift type to return
/// - runtime: JMES runtime (includes functions)
/// - Throws: JMESPathError
/// - Returns: Search result
public func search<Value: RawRepresentable>(json: Data, as: Value.Type = Value.self, runtime: JMESRuntime = .init()) throws -> Value? {
if let rawValue = try self.search(json: json, runtime: runtime) as? Value.RawValue {
return Value(rawValue: rawValue)
}
return nil
}

/// Search JSON
///
/// - Parameters:
/// - any: Swift type to search
Expand Down Expand Up @@ -68,6 +95,18 @@ public struct Expression {
return nil
}

/// Search JSON
///
/// - Parameters:
/// - any: JSON to search
/// - runtime: JMES runtime (includes functions)
/// - Throws: JMESPathError
/// - Returns: Search result
public func search(json: Data, runtime: JMESRuntime = .init()) throws -> Any? {
let value = try JMESVariable.fromJson(json)
return try runtime.interpret(value, ast: self.ast).collapse()
}

/// Search JSON
///
/// - Parameters:
Expand Down
2 changes: 1 addition & 1 deletion Sources/JMESPath/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension Token: CustomStringConvertible {
case .identifier(let string): return string
case .quotedIdentifier(let string): return string
case .number(let number): return "\(number)"
case .literal(_): return "`...`"
case .literal: return "`"
case .dot: return "."
case .star: return "*"
case .flatten: return "[]"
Expand Down
7 changes: 6 additions & 1 deletion Sources/JMESPath/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ public enum JMESVariable {

/// create JMESVariable from json
public static func fromJson(_ json: String) throws -> Self {
let object = try JSONSerialization.jsonObject(with: Data(json.utf8), options: [.allowFragments])
return try self.fromJson(Data(json.utf8))
}

/// create JMESVariable from json
public static func fromJson(_ json: Data) throws -> Self {
let object = try JSONSerialization.jsonObject(with: json, options: [.allowFragments])
return JMESVariable(from: object)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/JMESPathTests/ErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import XCTest

final class ErrorTests: XCTestCase {
func testUnknownFunction() throws {
func testUnknownFunction() throws {
let expression = try Expression.compile("unknown(@)")
XCTAssertThrowsError(try expression.search("test")) { error in
switch error {
Expand Down

0 comments on commit 0e70044

Please sign in to comment.