1
+ #if canImport(FoundationEssentials)
2
+ import FoundationEssentials
3
+ #else
1
4
import Foundation
5
+ #endif
2
6
3
7
/// JMES Expression
4
8
///
5
9
/// Holds a compiled JMES expression and allows you to search Json text or a type already in memory
6
- public struct JMESExpression : JMESSendable {
10
+ public struct JMESExpression : Sendable {
7
11
let ast : Ast
8
12
9
13
public static func compile( _ text: String ) throws -> Self {
@@ -22,8 +26,12 @@ public struct JMESExpression: JMESSendable {
22
26
/// - runtime: JMES runtime (includes functions)
23
27
/// - Throws: JMESPathError
24
28
/// - Returns: Search result
25
- public func search< Value> ( json: Data , as: Value . Type = Value . self, runtime: JMESRuntime = . init( ) ) throws -> Value ? {
26
- try self . search ( json: json, runtime: runtime) as? Value
29
+ public func search< Value> ( json: String , as: Value . Type = Value . self, runtime: JMESRuntime = . init( ) ) throws -> Value {
30
+ let searchResult = try self . search ( json: json, runtime: runtime)
31
+ guard let value = searchResult as? Value else {
32
+ throw JMESPathError . runtime ( " Expected \( Value . self) ) but got a \( type ( of: searchResult) ) " )
33
+ }
34
+ return value
27
35
}
28
36
29
37
/// Search JSON
@@ -34,8 +42,12 @@ public struct JMESExpression: JMESSendable {
34
42
/// - runtime: JMES runtime (includes functions)
35
43
/// - Throws: JMESPathError
36
44
/// - Returns: Search result
37
- public func search< Value> ( json: String , as: Value . Type = Value . self, runtime: JMESRuntime = . init( ) ) throws -> Value ? {
38
- try self . search ( json: json, runtime: runtime) as? Value
45
+ public func search< Value> ( json: some ContiguousBytes , as: Value . Type = Value . self, runtime: JMESRuntime = . init( ) ) throws -> Value {
46
+ let searchResult = try self . search ( json: json, runtime: runtime)
47
+ guard let value = searchResult as? Value else {
48
+ throw JMESPathError . runtime ( " Expected \( Value . self) ) but got a \( type ( of: searchResult) ) " )
49
+ }
50
+ return value
39
51
}
40
52
41
53
/// Search Swift type
@@ -46,9 +58,12 @@ public struct JMESExpression: JMESSendable {
46
58
/// - runtime: JMES runtime (includes functions)
47
59
/// - Throws: JMESPathError
48
60
/// - Returns: Search result
49
- public func search< Value> ( object: Any , as: Value . Type = Value . self, runtime: JMESRuntime = . init( ) ) throws -> Value ? {
50
- let value = try self . search ( object: object, runtime: runtime)
51
- return value as? Value
61
+ public func search< Value> ( object: Any , as: Value . Type = Value . self, runtime: JMESRuntime = . init( ) ) throws -> Value {
62
+ let searchResult = try self . search ( object: object, runtime: runtime)
63
+ guard let value = searchResult as? Value else {
64
+ throw JMESPathError . runtime ( " Expected \( Value . self) ) but got a \( type ( of: searchResult) ) " )
65
+ }
66
+ return value
52
67
}
53
68
54
69
/// Search JSON
@@ -58,9 +73,9 @@ public struct JMESExpression: JMESSendable {
58
73
/// - runtime: JMES runtime (includes functions)
59
74
/// - Throws: JMESPathError
60
75
/// - Returns: Search result
61
- public func search( json: Data , runtime: JMESRuntime = . init( ) ) throws -> Any ? {
62
- let value = try JMESVariable . fromJson ( json)
63
- return try runtime. interpret ( value, ast: self . ast) . collapse ( )
76
+ public func search( json: String , runtime: JMESRuntime = . init( ) ) throws -> Any ? {
77
+ let value = try JMESJSON . parse ( json : json)
78
+ return try runtime. interpret ( JMESVariable ( from : value) , ast: self . ast) . collapse ( )
64
79
}
65
80
66
81
/// Search JSON
@@ -70,9 +85,9 @@ public struct JMESExpression: JMESSendable {
70
85
/// - runtime: JMES runtime (includes functions)
71
86
/// - Throws: JMESPathError
72
87
/// - Returns: Search result
73
- public func search( json: String , runtime: JMESRuntime = . init( ) ) throws -> Any ? {
74
- let value = try JMESVariable . fromJson ( json)
75
- return try runtime. interpret ( value, ast: self . ast) . collapse ( )
88
+ public func search( json: some ContiguousBytes , runtime: JMESRuntime = . init( ) ) throws -> Any ? {
89
+ let value = try JMESJSON . parse ( json : json)
90
+ return try runtime. interpret ( JMESVariable ( from : value) , ast: self . ast) . collapse ( )
76
91
}
77
92
78
93
/// Search Swift type
0 commit comments