Skip to content

Commit

Permalink
Merge pull request #1 from adam-fowler/any-object
Browse files Browse the repository at this point in the history
Optimisation: Reduce parsing of JSON object
  • Loading branch information
adam-fowler authored Jun 1, 2021
2 parents 0293605 + 86b9e85 commit 9288a73
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 250 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/.swiftpm
/Packages
/*.xcodeproj
/docs
xcuserdata/
Package.resolved
28 changes: 21 additions & 7 deletions Sources/JMESPath/Ast.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
//
// File.swift
//
//
// Created by Adam Fowler on 27/05/2021.
//

/// JMES expression abstract syntax tree
public indirect enum Ast: Equatable {
/// compares two nodes using a comparator
case comparison(comparator: Comparator, lhs: Ast, rhs: Ast)
/// if `predicate` evaluates to a truthy value returns result from `then`
case condition(predicate: Ast, then: Ast)
/// returns the current node
case identity
/// used by functions to dynamically evaluate argument values
case expRef(ast: Ast)
/// evaluates nodes and then flattens it one level
case flatten(node: Ast)
/// function name and a vector of function argument expressions
case function(name: String, args: [Ast])
/// extracts a key value from an object
case field(name: String)
/// extracts an indexed value from an array
case index(index: Int)
/// resolves to a literal value
case literal(value: JMESVariable)
/// resolves to a list of evaluated expressions
case multiList(elements: [Ast])
/// resolves to a map of key/evaluated expression pairs
case multiHash(elements: [String: Ast])
/// evaluates to true/false based on expression
case not(node: Ast)
/// evalutes `lhs` and pushes each value through to `rhs`
case projection(lhs: Ast, rhs: Ast)
/// evaluates expression and if result is an object then return array of its values
case objectValues(node: Ast)
/// evaluates `lhs` and if not truthy returns, otherwise evaluates `rhs`
case and(lhs: Ast, rhs: Ast)
/// evaluates `lhs` and if truthy returns, otherwise evaluates `rhs`
case or(lhs: Ast, rhs: Ast)
/// returns a slice of an array
case slice(start: Int?, stop: Int?, step: Int)
/// evalutes `lhs` and then provides that value to `rhs`
case subExpr(lhs: Ast, rhs: Ast)
}

/// Comparator used in comparison AST nodes
public enum Comparator: Equatable {
case equal
case notEqual
Expand All @@ -34,6 +47,7 @@ public enum Comparator: Equatable {
case greaterThan
case greaterThanOrEqual

/// initialise `Comparator` from `Token`
init(from token: Token) throws {
switch token {
case .equals: self = .equal
Expand Down
4 changes: 2 additions & 2 deletions Sources/JMESPath/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
/// Provides two errors, compile time and run time errors
public struct JMESPathError: Error, Equatable {
/// Error that occurred while compiling JMESPath
public static func compileTime(_ message: String) -> Self { .init(value: .compileTime(message))}
public static func compileTime(_ message: String) -> Self { .init(value: .compileTime(message)) }
/// Error that occurred while running a search
public static func runtime(_ message: String) -> Self { .init(value: .runtime(message))}
public static func runtime(_ message: String) -> Self { .init(value: .runtime(message)) }

private enum Internal: Equatable {
case compileTime(String)
Expand Down
4 changes: 2 additions & 2 deletions Sources/JMESPath/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct Expression {
/// - Throws: JMESPathError
/// - Returns: Search result
public func search<Value>(json: String, as: Value.Type = Value.self, runtime: JMESRuntime = .init()) throws -> Value? {
return try search(json: json, runtime: runtime) as? Value
return try self.search(json: json, runtime: runtime) as? Value
}

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

/// Search JSON
Expand Down
Loading

0 comments on commit 9288a73

Please sign in to comment.