1+ #if canImport(FoundationEssentials)
2+ import FoundationEssentials
3+ #else
14import Foundation
5+ #endif
26
37/// JMES Expression
48///
59/// 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 {
711 let ast : Ast
812
913 public static func compile( _ text: String ) throws -> Self {
@@ -22,8 +26,12 @@ public struct JMESExpression: JMESSendable {
2226 /// - runtime: JMES runtime (includes functions)
2327 /// - Throws: JMESPathError
2428 /// - 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
2735 }
2836
2937 /// Search JSON
@@ -34,8 +42,12 @@ public struct JMESExpression: JMESSendable {
3442 /// - runtime: JMES runtime (includes functions)
3543 /// - Throws: JMESPathError
3644 /// - 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
3951 }
4052
4153 /// Search Swift type
@@ -46,9 +58,12 @@ public struct JMESExpression: JMESSendable {
4658 /// - runtime: JMES runtime (includes functions)
4759 /// - Throws: JMESPathError
4860 /// - 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
5267 }
5368
5469 /// Search JSON
@@ -58,9 +73,9 @@ public struct JMESExpression: JMESSendable {
5873 /// - runtime: JMES runtime (includes functions)
5974 /// - Throws: JMESPathError
6075 /// - 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 ( )
6479 }
6580
6681 /// Search JSON
@@ -70,9 +85,9 @@ public struct JMESExpression: JMESSendable {
7085 /// - runtime: JMES runtime (includes functions)
7186 /// - Throws: JMESPathError
7287 /// - 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 ( )
7691 }
7792
7893 /// Search Swift type
0 commit comments