-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResult.kt
46 lines (39 loc) · 1.33 KB
/
Result.kt
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
package org.ilinykh.kotlin.logicjsonpath
import com.jayway.jsonpath.JsonPath
import org.ilinykh.kotlin.logicjsonpath.operation.Operations
/**
* Result of logical operation.
* @param json source json string
* @param exp logical expression
*/
class Result(
private val json: String,
private val exp: Expression
) {
constructor(exp: Expression): this(json = "", exp = exp)
/**
* @return logical result of expression
*/
fun result(): Boolean {
val deque = exp.tokens()
val acc = ArrayDeque<String>()
exp.tokens().forEach {
if (it.isJsonPath()) {
JsonPath.read<Any>(json, deque.removeFirst().toString()).toString().let(acc::addLast)
}
else if (it.isOperator()) {
val operand2 = acc.removeLast()
val operand1 = acc.removeLast()
deque.removeFirst()
Operations.ops[it.toString()]
?.let { constructor -> constructor(operand1, operand2) }
?.result()?.toString()?.apply { acc.addLast(this) }
?: throw RuntimeException("This '$it' operation in not supported")
}
else {
acc.addLast(deque.removeFirst().toString())
}
}
return acc.removeFirst() == "true"
}
}