1- import Foundation
2-
31/// JMES Expression
42///
53/// Holds a compiled JMES expression and allows you to search Json text or a type already in memory
6- public struct JMESExpression : JMESSendable {
4+ public struct JMESExpression : Sendable {
75 let ast : Ast
86
97 public static func compile( _ text: String ) throws -> Self {
@@ -22,20 +20,12 @@ public struct JMESExpression: JMESSendable {
2220 /// - runtime: JMES runtime (includes functions)
2321 /// - Throws: JMESPathError
2422 /// - 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
27- }
28-
29- /// Search JSON
30- ///
31- /// - Parameters:
32- /// - any: JSON to search
33- /// - as: Swift type to return
34- /// - runtime: JMES runtime (includes functions)
35- /// - Throws: JMESPathError
36- /// - 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
23+ public func search< Value> ( json: String , as: Value . Type = Value . self, runtime: JMESRuntime = . init( ) ) throws -> Value {
24+ let searchResult = try self . search ( json: json, runtime: runtime)
25+ guard let value = searchResult as? Value else {
26+ throw JMESPathError . runtime ( " Expected \( Value . self) ) but got a \( type ( of: searchResult) ) " )
27+ }
28+ return value
3929 }
4030
4131 /// Search Swift type
@@ -46,27 +36,12 @@ public struct JMESExpression: JMESSendable {
4636 /// - runtime: JMES runtime (includes functions)
4737 /// - Throws: JMESPathError
4838 /// - 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
52- }
53-
54- /// Search JSON
55- ///
56- /// - Parameters:
57- /// - any: JSON to search
58- /// - runtime: JMES runtime (includes functions)
59- /// - Throws: JMESPathError
60- /// - Returns: Search result
61- public func search( json: Data , runtime: JMESRuntime = . init( ) ) throws -> Any ? {
62- let variable = try json. withBufferView { view -> JMESVariable ? in
63- var scanner = JSONScanner ( bytes: view, options: . init( ) )
64- let map = try scanner. scan ( )
65- guard let value = map. loadValue ( at: 0 ) else { return nil }
66- return try JMESJSONVariable ( value: value) . getJMESVariable ( map)
39+ public func search< Value> ( object: Any , as: Value . Type = Value . self, runtime: JMESRuntime = . init( ) ) throws -> Value {
40+ let searchResult = try self . search ( object: object, runtime: runtime)
41+ guard let value = searchResult as? Value else {
42+ throw JMESPathError . runtime ( " Expected \( Value . self) ) but got a \( type ( of: searchResult) ) " )
6743 }
68- guard let variable else { return nil }
69- return try runtime. interpret ( variable, ast: self . ast) . collapse ( )
44+ return value
7045 }
7146
7247 /// Search JSON
@@ -77,14 +52,8 @@ public struct JMESExpression: JMESSendable {
7752 /// - Throws: JMESPathError
7853 /// - Returns: Search result
7954 public func search( json: String , runtime: JMESRuntime = . init( ) ) throws -> Any ? {
80- let variable = try json. withBufferView { view -> JMESVariable ? in
81- var scanner = JSONScanner ( bytes: view, options: . init( ) )
82- let map = try scanner. scan ( )
83- guard let value = map. loadValue ( at: 0 ) else { return nil }
84- return try JMESJSONVariable ( value: value) . getJMESVariable ( map)
85- }
86- guard let variable else { return nil }
87- return try runtime. interpret ( variable, ast: self . ast) . collapse ( )
55+ let value = try JMESJSON . parse ( json: json)
56+ return try runtime. interpret ( JMESVariable ( from: value) , ast: self . ast) . collapse ( )
8857 }
8958
9059 /// Search Swift type
0 commit comments