Skip to content

Commit 695fb07

Browse files
committed
Added JMESVariable.fromJson, Expression.search(json:)
1 parent 92b2902 commit 695fb07

5 files changed

Lines changed: 25 additions & 13 deletions

File tree

‎Sources/JMESPath/Expression.swift‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import Foundation
12

3+
/// JMES Expression
4+
///
5+
/// Holds a compiled JMES expression and allows you to search Json text or a structure already in memory
26
public struct Expression {
37
let ast: Ast
48

@@ -10,11 +14,15 @@ public struct Expression {
1014
return self.init(ast)
1115
}
1216

13-
public func search(_ any: Any) throws -> Any? {
14-
let runtime = Runtime()
17+
public func search(_ any: Any, runtime: Runtime = .init()) throws -> Any? {
1518
return try runtime.interpret(JMESVariable(from: any), ast: self.ast).collapse()
1619
}
1720

21+
public func search(json: String, runtime: Runtime = .init()) throws -> Any? {
22+
let value = try JMESVariable.fromJson(json)
23+
return try runtime.interpret(value, ast: self.ast).collapse()
24+
}
25+
1826
private init(_ ast: Ast) {
1927
self.ast = ast
2028
}

‎Sources/JMESPath/Functions.swift‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@ struct ToNumberFunction: Function {
536536
return .number(number)
537537
case .string(let string):
538538
do {
539-
let number = try JSONSerialization.jsonObject(with: Data("\(string)".utf8), options: [.allowFragments, .fragmentsAllowed])
540-
return try JMESVariable(from: number)
539+
return try JMESVariable.fromJson(string)
541540
} catch {
542541
return .null
543542
}

‎Sources/JMESPath/JMESVariable.swift‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public enum JMESVariable: Equatable {
4646
}
4747
}
4848

49+
static func fromJson(_ json: String) throws -> Self {
50+
let object = try JSONSerialization.jsonObject(with: Data(json.utf8), options: [.fragmentsAllowed])
51+
return try JMESVariable(from: object)
52+
}
53+
4954
func collapse() -> Any? {
5055
switch self {
5156
case .null:

‎Sources/JMESPath/Lexer.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ public class Lexer {
149149

150150
private func readQuotedIdentifier() throws -> String {
151151
let string = try readInside()
152-
let expanded = try JSONSerialization.jsonObject(with: Data("\"\(string)\"".utf8), options: [.allowFragments, .fragmentsAllowed])
153-
return expanded as! String
152+
let variable = try JMESVariable.fromJson("\"\(string)\"")
153+
guard case .string(let string) = variable else { preconditionFailure() }
154+
return string
154155
}
155156

156157
private func readRawString() throws -> JMESVariable {
@@ -162,8 +163,7 @@ public class Lexer {
162163
let string = try readInside()
163164
do {
164165
let unescaped = string.replacingOccurrences(of: "\\`", with: "`")
165-
let expanded = try JSONSerialization.jsonObject(with: Data(unescaped.utf8), options: [.allowFragments, .fragmentsAllowed])
166-
return try JMESVariable(from: expanded)
166+
return try JMESVariable.fromJson(unescaped)
167167
} catch {
168168
throw JMESPathError.syntaxError("Unable to parse literal JSON")
169169
}

‎Sources/JMESPath/Runtime.swift‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11

22
public class Runtime {
3-
init() {
3+
public init() {
44
self.functions = [:]
55
self.registerBuiltInFunctions()
66
}
77

8-
func getFunction(_ name: String) -> Function.Type? {
9-
return self.functions[name]
8+
public func registerFunction(_ name: String, function: Function.Type) {
9+
self.functions[name] = function
1010
}
1111

12-
func registerFunction(_ name: String, function: Function.Type) {
13-
self.functions[name] = function
12+
func getFunction(_ name: String) -> Function.Type? {
13+
return self.functions[name]
1414
}
1515

1616
func registerBuiltInFunctions() {

0 commit comments

Comments
 (0)