Skip to content

Commit 0e70044

Browse files
committed
Add search functions that take json as Data
1 parent 8060f42 commit 0e70044

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

Sources/JMESPath/Expression.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ public struct Expression {
1414
return self.init(ast)
1515
}
1616

17+
/// Search JSON
18+
///
19+
/// - Parameters:
20+
/// - any: JSON to search
21+
/// - as: Swift type to return
22+
/// - runtime: JMES runtime (includes functions)
23+
/// - Throws: JMESPathError
24+
/// - Returns: Search result
25+
public func search<Value>(json: Data, as: Value.Type = Value.self, runtime: JMESRuntime = .init()) throws -> Value? {
26+
return try self.search(json: json, runtime: runtime) as? Value
27+
}
28+
1729
/// Search JSON
1830
///
1931
/// - Parameters:
@@ -26,7 +38,22 @@ public struct Expression {
2638
return try self.search(json: json, runtime: runtime) as? Value
2739
}
2840

29-
/// Search Swift type
41+
/// Search JSON
42+
///
43+
/// - Parameters:
44+
/// - any: Swift type to search
45+
/// - as: Swift type to return
46+
/// - runtime: JMES runtime (includes functions)
47+
/// - Throws: JMESPathError
48+
/// - Returns: Search result
49+
public func search<Value: RawRepresentable>(json: Data, as: Value.Type = Value.self, runtime: JMESRuntime = .init()) throws -> Value? {
50+
if let rawValue = try self.search(json: json, runtime: runtime) as? Value.RawValue {
51+
return Value(rawValue: rawValue)
52+
}
53+
return nil
54+
}
55+
56+
/// Search JSON
3057
///
3158
/// - Parameters:
3259
/// - any: Swift type to search
@@ -68,6 +95,18 @@ public struct Expression {
6895
return nil
6996
}
7097

98+
/// Search JSON
99+
///
100+
/// - Parameters:
101+
/// - any: JSON to search
102+
/// - runtime: JMES runtime (includes functions)
103+
/// - Throws: JMESPathError
104+
/// - Returns: Search result
105+
public func search(json: Data, runtime: JMESRuntime = .init()) throws -> Any? {
106+
let value = try JMESVariable.fromJson(json)
107+
return try runtime.interpret(value, ast: self.ast).collapse()
108+
}
109+
71110
/// Search JSON
72111
///
73112
/// - Parameters:

Sources/JMESPath/Token.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension Token: CustomStringConvertible {
6464
case .identifier(let string): return string
6565
case .quotedIdentifier(let string): return string
6666
case .number(let number): return "\(number)"
67-
case .literal(_): return "`...`"
67+
case .literal: return "`"
6868
case .dot: return "."
6969
case .star: return "*"
7070
case .flatten: return "[]"

Sources/JMESPath/Variable.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ public enum JMESVariable {
6060

6161
/// create JMESVariable from json
6262
public static func fromJson(_ json: String) throws -> Self {
63-
let object = try JSONSerialization.jsonObject(with: Data(json.utf8), options: [.allowFragments])
63+
return try self.fromJson(Data(json.utf8))
64+
}
65+
66+
/// create JMESVariable from json
67+
public static func fromJson(_ json: Data) throws -> Self {
68+
let object = try JSONSerialization.jsonObject(with: json, options: [.allowFragments])
6469
return JMESVariable(from: object)
6570
}
6671

Tests/JMESPathTests/ErrorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import XCTest
33

44
final class ErrorTests: XCTestCase {
5-
func testUnknownFunction() throws {
5+
func testUnknownFunction() throws {
66
let expression = try Expression.compile("unknown(@)")
77
XCTAssertThrowsError(try expression.search("test")) { error in
88
switch error {

0 commit comments

Comments
 (0)