-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSONPathEvaluator.swift
611 lines (587 loc) · 22.7 KB
/
JSONPathEvaluator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//
// JSONPathQueryEvaluator.swift
// DynamicJSON
//
// Created by Matthias Zenger on 17/02/2024.
// Copyright © 2024 Matthias Zenger. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
///
/// `JSONPathEvaluator` implements an evaulator of JSONPath queries given
/// a root JSON document. Evaluators are parameterized with `JSONPathEnvironment`
/// objects which define the functions supported in filter expressions.
///
public struct JSONPathEvaluator {
/// The JSON document for which queries can be evaluated.
let root: JSON
/// The environment defining functions which can be used in filter expressions.
let env: JSONPathEnvironment
/// Execute in strict mode?
let strict: Bool
/// Collection of errors raised by functionality provided by `JSONPathEvaluator`.
public enum Error: LocalizedError, CustomStringConvertible {
case booleanNotCoercableToBool
case doesNotEvaluateToJSON(JSONPath.Expression)
case cannotNegate(JSONPath.Expression)
case expectedType(ValueType, JSONPath.Expression)
case mismatchOfOperandTypes(JSONPath.Expression)
case divisionByZero(JSONPath.Expression)
case unknownVariable(String)
case unknownFunction(String)
case numberOfArgumentsMismatch(JSONPath.Expression)
case filterExprNotLogical(JSONPath.Expression)
case typeMismatch(String, ValueType, Value)
case jsonTypeMismatch(String, JSONType, Value)
public var description: String {
switch self {
case .booleanNotCoercableToBool:
return "cannot coerce a boolean to a nodelist"
case .doesNotEvaluateToJSON(let expr):
return "expression \(expr) does not evaluate to a JSON value"
case .cannotNegate(let expr):
return "cannot negate expression \(expr)"
case .expectedType(let val, let expr):
return "expression \(expr) not conforming to expected type \(val)"
case .mismatchOfOperandTypes(let expr):
return "operand types of expression \(expr) not matching"
case .divisionByZero(let expr):
return "expression \(expr) subject to division by zero"
case .unknownVariable(let ident):
return "unknown variable '\(ident)'"
case .unknownFunction(let ident):
return "unknown function '\(ident)'"
case .numberOfArgumentsMismatch(let expr):
return "mismatch of number of arguments in expression \(expr)"
case .typeMismatch(let fun, let type, let val):
return "expected value of type \(type) in function '\(fun)', but received \(val)"
case .jsonTypeMismatch(let fun, let type, let val):
return "expected JSON value of type \(type) in function '\(fun)', but received \(val)"
case .filterExprNotLogical(let expr):
return "\(expr) does not evaluate to a logical value"
}
}
public var errorDescription: String? {
return self.description
}
public var failureReason: String? {
switch self {
case .booleanNotCoercableToBool,
.doesNotEvaluateToJSON(_),
.cannotNegate(_),
.expectedType(_, _),
.mismatchOfOperandTypes(_),
.typeMismatch(_, _, _),
.jsonTypeMismatch(_, _, _),
.filterExprNotLogical(_):
return "type mismatch"
case .divisionByZero(_):
return "division by zero"
case .unknownVariable(_), .unknownFunction(_):
return "unknown identifier"
case .numberOfArgumentsMismatch(_):
return "number of arguments mismatch"
}
}
}
/// Representation of functions available in JSONPath filter expressions.
public struct Function {
public let argtypes: [ValueType]
public let restype: ValueType
public let impl: (JSON, JSON, [Value]) throws -> Value
}
/// Types of values as defined by JSONPath
public enum ValueType: Hashable, CustomStringConvertible {
case logicalType
case jsonType
case nodesType
public var description: String {
switch self {
case .logicalType:
return "Logical"
case .jsonType:
return "JSON"
case .nodesType:
return "Nodes"
}
}
}
/// Values used for evaluating JSONPath filter expressions.
public enum Value: Hashable, CustomStringConvertible {
case logical(Bool)
case json(JSON?)
case nodes([JSON])
public init(_ bool: Bool, type: ValueType) throws {
switch type {
case .logicalType:
self = .logical(bool)
case .jsonType:
self = .json(.boolean(bool))
case .nodesType:
throw Error.booleanNotCoercableToBool
}
}
public var isTrue: Bool {
switch self {
case .logical(let bool):
return bool
default:
return false
}
}
public func has(type: ValueType) -> Bool {
switch self {
case .logical(_):
return type == .logicalType
case .json(_):
return type == .jsonType
case .nodes(_):
return type == .nodesType
}
}
public var description: String {
switch self {
case .logical(let bool):
return bool ? "YES" : "NO"
case .json(let json):
if let json {
return json.description
} else {
return "NONE"
}
case .nodes(let nodes):
return "<" + nodes.map { n in n.description }.joined(separator: ", ") + ">"
}
}
}
/// Initializer creating an evaluator object for querying JSON document `value`.
/// Queries executed using this evaluator can refer to the JSONPath filter expression
/// functions provided by the environment `env`. An evaluator object can be reused
/// for executing many queries.
public init(value: JSON, env: JSONPathEnvironment? = nil, strict: Bool = true) {
self.root = value
self.env = env ?? JSONPathEnvironment()
self.strict = strict
}
/// Executes `query` using this evaluator returning an array of results.
public func query(_ query: JSONPath) throws -> [LocatedJSON] {
return try self.query(current: .root(self.root), with: query)
}
private func query(current: LocatedJSON, with query: JSONPath) throws -> [LocatedJSON] {
switch query {
case .self:
return [.root(self.root)]
case .current:
return [current]
case .select(let path, let segment):
return try self.query(current: current, with: path).flatMap { result in
return try self.select(segment, from: result)
}
}
}
private func select(_ segment: JSONPath.Segment,
from current: LocatedJSON) throws -> [LocatedJSON] {
switch segment {
case .children(let selectors):
return try selectors.flatMap { selector in
return try self.select(selector, from: current)
}
case .descendants(let selectors):
var result: [LocatedJSON] = []
try current.forEachDescendant { json in
try result.append(contentsOf: selectors.flatMap { selector in
return try self.select(selector, from: json)
})
}
return result
}
}
private func select(_ selector: JSONPath.Selector,
from current: LocatedJSON) throws -> [LocatedJSON] {
switch selector {
case .wildcard:
switch current.value {
case .array(let arr):
return arr.located(at: current.location)
case .object(let dict):
return dict.located(at: current.location)
default:
return []
}
case .member(let name):
if let result = current.member(name) {
return [result]
} else {
return []
}
case .index(let i):
if let result = current.index(i) {
return [result]
} else {
return []
}
case .slice(nil, nil, nil):
switch current.value {
case .array(let arr):
return arr.located(at: current.location)
default:
return []
}
case .slice(let s, let e, let inc):
let step = inc ?? 1
guard step != 0 else {
return []
}
switch current.value {
case .array(let arr):
func normalize(_ i: Int) -> Int {
return i >= 0 ? i : arr.count + i
}
let start = normalize(s ?? (step >= 0 ? 0 : arr.count - 1))
let end = normalize(e ?? (step >= 0 ? arr.count : -arr.count - 1))
let lower = step >= 0 ? min(max(start, 0), arr.count) : min(max(end, -1), arr.count - 1)
let upper = step >= 0 ? min(max(end, 0), arr.count) : min(max(start, -1), arr.count - 1)
var result: [LocatedJSON] = []
if step > 0 {
var i = lower
while i < upper {
result.append(LocatedJSON(arr[i], .index(current.location, i)))
i += step
}
} else {
var i = upper
while lower < i {
result.append(LocatedJSON(arr[i], .index(current.location, i)))
i += step
}
}
return result
default:
return []
}
case .filter(let expr):
switch current.value {
case .array(let arr):
var res: [LocatedJSON] = []
for i in arr.indices {
switch try self.evaluate(expr,
for: arr[i],
at: .index(current.location, i),
expecting: .logicalType) {
case .logical(let bool):
if bool {
res.append(LocatedJSON(arr[i], .index(current.location, i)))
}
case .json(_), .nodes(_):
if self.strict {
throw Error.filterExprNotLogical(expr)
}
}
}
return res
case .object(let dict):
var res: [LocatedJSON] = []
for (key, child) in dict {
switch try self.evaluate(expr,
for: child,
at: .member(current.location, key),
expecting: .logicalType) {
case .logical(let bool):
if bool {
res.append(LocatedJSON(child, .member(current.location, key)))
}
case .json(_), .nodes(_):
if self.strict {
throw Error.filterExprNotLogical(expr)
}
}
}
return res
default:
return []
}
}
}
private func evaluate(lhs: JSONPath.Expression,
rhs: JSONPath.Expression,
for value: JSON,
at location: JSONLocation) throws -> (JSON?, JSON?) {
guard case .some(.json(let l)) = try? self.evaluate(lhs,
for: value,
at: location,
expecting: .jsonType) else {
throw Error.doesNotEvaluateToJSON(lhs)
}
guard case .some(.json(let r)) = try? self.evaluate(rhs,
for: value,
at: location,
expecting: .jsonType) else {
throw Error.doesNotEvaluateToJSON(rhs)
}
switch (l, r) {
case (.some(.integer(let l)), .some(.float(let r))):
return (.float(Double(l)), .float(r))
case (.some(.float(let l)), .some(.integer(let r))):
return (.float(l), .float(Double(r)))
default:
return (l, r)
}
}
private func evaluate(_ expr: JSONPath.Expression,
for value: JSON,
at location: JSONLocation,
expecting type: ValueType) throws -> Value {
switch expr {
case .null:
return .json(.null)
case .true:
return .json(.boolean(true))
case .false:
return .json(.boolean(false))
case .integer(let num):
return .json(.integer(num))
case .float(let num):
return .json(.float(num))
case .string(let str):
return .json(.string(str))
case .variable(let ident):
if let res = self.env.value(of: ident) {
return res
} else {
throw Error.unknownVariable(ident)
}
case .query(let path):
let nodes = try self.query(current: LocatedJSON(value, location), with: path).values
switch type {
case .logicalType:
return .logical(!nodes.isEmpty)
case .jsonType:
throw Error.doesNotEvaluateToJSON(expr)
case .nodesType:
return .nodes(nodes)
}
case .singularQuery(let path):
let nodes = try self.query(current: LocatedJSON(value, location), with: path).values
switch type {
case .logicalType:
return .logical(!nodes.isEmpty)
case .jsonType:
return .json(nodes.first)
case .nodesType:
return .nodes(nodes)
}
case .call(let ident, let arguments):
guard let function = self.env.function(name: ident) else {
throw Error.unknownFunction(ident)
}
guard function.argtypes.count == arguments.count else {
throw Error.numberOfArgumentsMismatch(expr)
}
var args: [Value] = []
var i = 0
while i < arguments.count {
args.append(try self.evaluate(arguments[i],
for: value,
at: location,
expecting: function.argtypes[i]))
i += 1
}
return try function.impl(self.root, value, args)
case .prefix(let op, let argument):
switch op {
case .negate:
let argres = try self.evaluate(argument,
for: value,
at: location,
expecting: .jsonType)
switch argres {
case .json(.boolean(let bool)):
return .json(.boolean(!bool))
case .json(.integer(let num)):
return .json(.integer(-num))
case .json(.float(let num)):
return .json(.float(-num))
default:
throw Error.cannotNegate(argument)
}
case .not:
let argres = try self.evaluate(argument,
for: value,
at: location,
expecting: .logicalType)
switch argres {
case .logical(let bool):
return .logical(!bool)
default:
throw Error.cannotNegate(argument)
}
}
case .operation(let lhs, let op, let rhs):
switch op {
case .equals:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
return try .init(l == r, type: type)
case .notEquals:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
return try .init(l != r, type: type)
case .lessThan:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
switch (l, r) {
case (.boolean(let l), .boolean(let r)):
return try .init(!l && r, type: type)
case (.integer(let l), .integer(let r)):
return try .init(l < r, type: type)
case (.float(let l), .float(let r)):
return try .init(l < r, type: type)
case (.string(let l), .string(let r)):
return try .init(l.compare(r) == .orderedAscending, type: type)
default:
return try .init(false, type: type)
}
case .lessThanEquals:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
switch (l, r) {
case (nil, nil):
return try .init(true, type: type)
case (.null, .null):
return try .init(true, type: type)
case (.boolean(let lb), .boolean(let rb)):
return try .init(!lb && rb || lb == rb, type: type)
case (.integer(let li), .integer(let ri)):
return try .init(li <= ri, type: type)
case (.float(let lf), .float(let rf)):
return try .init(lf <= rf, type: type)
case (.string(let l), .string(let r)):
let res = l.compare(r)
return try .init(res == .orderedAscending || res == .orderedSame, type: type)
default:
return try .init(false, type: type)
}
case .greaterThan:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
switch (l, r) {
case (.boolean(let lb), .boolean(let rb)):
return try .init(!rb && lb, type: type)
case (.integer(let li), .integer(let ri)):
return try .init(li > ri, type: type)
case (.float(let lf), .float(let rf)):
return try .init(lf > rf, type: type)
case (.string(let l), .string(let r)):
return try .init(l.compare(r) == .orderedDescending, type: type)
default:
return try .init(false, type: type)
}
case .greaterThanEquals:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
switch (l, r) {
case (nil, nil):
return try .init(true, type: type)
case (.null, .null):
return try .init(true, type: type)
case (.boolean(let lb), .boolean(let rb)):
return try .init(!rb && lb || rb == lb, type: type)
case (.integer(let li), .integer(let ri)):
return try .init(li >= ri, type: type)
case (.float(let lf), .float(let rf)):
return try .init(lf >= rf, type: type)
case (.string(let l), .string(let r)):
let res = l.compare(r)
return try .init(res == .orderedDescending || res == .orderedSame, type: type)
default:
return try .init(false, type: type)
}
case .or:
let l = try self.evaluate(lhs, for: value, at: location, expecting: type)
switch l {
case .logical(let bool):
guard type == .logicalType else {
throw Error.expectedType(type, lhs)
}
return bool ? l : try self.evaluate(rhs, for: value, at: location, expecting: type)
case .json(.boolean(let bool)):
guard type == .jsonType else {
throw Error.expectedType(type, lhs)
}
return bool ? l : try self.evaluate(rhs, for: value, at: location, expecting: type)
default:
throw Error.expectedType(type, lhs)
}
case .and:
let l = try self.evaluate(lhs, for: value, at: location, expecting: type)
switch l {
case .logical(let bool):
guard type == .logicalType else {
throw Error.expectedType(type, lhs)
}
return !bool ? l : try self.evaluate(rhs, for: value, at: location, expecting: type)
case .json(.boolean(let bool)):
guard type == .jsonType else {
throw Error.expectedType(type, lhs)
}
return !bool ? l : try self.evaluate(rhs, for: value, at: location, expecting: type)
default:
throw Error.expectedType(type, lhs)
}
case .plus:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
switch (l, r) {
case (.integer(let l), .integer(let r)):
return .json(.integer(l &+ r))
case (.float(let l), .float(let r)):
return .json(.float(l + r))
case (.string(let l), .string(let r)):
return .json(.string(l.appending(r)))
default:
throw Error.mismatchOfOperandTypes(expr)
}
case .minus:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
switch (l, r) {
case (.integer(let l), .integer(let r)):
return .json(.integer(l &- r))
case (.float(let l), .float(let r)):
return .json(.float(l - r))
default:
throw Error.mismatchOfOperandTypes(expr)
}
case .mult:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
switch (l, r) {
case (.integer(let l), .integer(let r)):
return .json(.integer(l &* r))
case (.float(let l), .float(let r)):
return .json(.float(l * r))
default:
throw Error.mismatchOfOperandTypes(expr)
}
case .divide:
let (l, r) = try self.evaluate(lhs: lhs, rhs: rhs, for: value, at: location)
switch (l, r) {
case (.integer(let l), .integer(let r)):
guard r != 0 else {
throw Error.divisionByZero(expr)
}
return .json(.integer(l / r))
case (.float(let l), .float(let r)):
guard r != 0.0 else {
throw Error.divisionByZero(expr)
}
return .json(.float(l / r))
default:
throw Error.mismatchOfOperandTypes(expr)
}
}
}
}
}