Skip to content

Commit 9288a73

Browse files
authored
Merge pull request #1 from adam-fowler/any-object
Optimisation: Reduce parsing of JSON object
2 parents 0293605 + 86b9e85 commit 9288a73

12 files changed

+442
-250
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
/.swiftpm
44
/Packages
55
/*.xcodeproj
6+
/docs
67
xcuserdata/
78
Package.resolved

Sources/JMESPath/Ast.swift

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Adam Fowler on 27/05/2021.
6-
//
7-
1+
/// JMES expression abstract syntax tree
82
public indirect enum Ast: Equatable {
3+
/// compares two nodes using a comparator
94
case comparison(comparator: Comparator, lhs: Ast, rhs: Ast)
5+
/// if `predicate` evaluates to a truthy value returns result from `then`
106
case condition(predicate: Ast, then: Ast)
7+
/// returns the current node
118
case identity
9+
/// used by functions to dynamically evaluate argument values
1210
case expRef(ast: Ast)
11+
/// evaluates nodes and then flattens it one level
1312
case flatten(node: Ast)
13+
/// function name and a vector of function argument expressions
1414
case function(name: String, args: [Ast])
15+
/// extracts a key value from an object
1516
case field(name: String)
17+
/// extracts an indexed value from an array
1618
case index(index: Int)
19+
/// resolves to a literal value
1720
case literal(value: JMESVariable)
21+
/// resolves to a list of evaluated expressions
1822
case multiList(elements: [Ast])
23+
/// resolves to a map of key/evaluated expression pairs
1924
case multiHash(elements: [String: Ast])
25+
/// evaluates to true/false based on expression
2026
case not(node: Ast)
27+
/// evalutes `lhs` and pushes each value through to `rhs`
2128
case projection(lhs: Ast, rhs: Ast)
29+
/// evaluates expression and if result is an object then return array of its values
2230
case objectValues(node: Ast)
31+
/// evaluates `lhs` and if not truthy returns, otherwise evaluates `rhs`
2332
case and(lhs: Ast, rhs: Ast)
33+
/// evaluates `lhs` and if truthy returns, otherwise evaluates `rhs`
2434
case or(lhs: Ast, rhs: Ast)
35+
/// returns a slice of an array
2536
case slice(start: Int?, stop: Int?, step: Int)
37+
/// evalutes `lhs` and then provides that value to `rhs`
2638
case subExpr(lhs: Ast, rhs: Ast)
2739
}
2840

41+
/// Comparator used in comparison AST nodes
2942
public enum Comparator: Equatable {
3043
case equal
3144
case notEqual
@@ -34,6 +47,7 @@ public enum Comparator: Equatable {
3447
case greaterThan
3548
case greaterThanOrEqual
3649

50+
/// initialise `Comparator` from `Token`
3751
init(from token: Token) throws {
3852
switch token {
3953
case .equals: self = .equal

Sources/JMESPath/Error.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
/// Provides two errors, compile time and run time errors
44
public struct JMESPathError: Error, Equatable {
55
/// Error that occurred while compiling JMESPath
6-
public static func compileTime(_ message: String) -> Self { .init(value: .compileTime(message))}
6+
public static func compileTime(_ message: String) -> Self { .init(value: .compileTime(message)) }
77
/// Error that occurred while running a search
8-
public static func runtime(_ message: String) -> Self { .init(value: .runtime(message))}
8+
public static func runtime(_ message: String) -> Self { .init(value: .runtime(message)) }
99

1010
private enum Internal: Equatable {
1111
case compileTime(String)

Sources/JMESPath/Expression.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public struct Expression {
2323
/// - Throws: JMESPathError
2424
/// - Returns: Search result
2525
public func search<Value>(json: String, as: Value.Type = Value.self, runtime: JMESRuntime = .init()) throws -> Value? {
26-
return try search(json: json, runtime: runtime) as? Value
26+
return try self.search(json: json, runtime: runtime) as? Value
2727
}
2828

2929
/// Search Swift type
@@ -35,7 +35,7 @@ public struct Expression {
3535
/// - Throws: JMESPathError
3636
/// - Returns: Search result
3737
public func search<Value>(_ any: Any, as: Value.Type = Value.self, runtime: JMESRuntime = .init()) throws -> Value? {
38-
return try search(any, runtime: runtime) as? Value
38+
return try self.search(any, runtime: runtime) as? Value
3939
}
4040

4141
/// Search JSON

0 commit comments

Comments
 (0)