Skip to content

Commit 628b661

Browse files
author
Antonis Lilis
committed
Adds options to supress exception and return false
1 parent f7dbe39 commit 628b661

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/main/kotlin/eu/afse/jsonlogic/JsonLogic.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ class JsonLogic {
2121
*
2222
* @param logic the logic
2323
* @param data the data
24+
* @param safe if true an exception is returned as false else exceptions are thrown
2425
* @return evaluation result
2526
*/
2627
@JvmOverloads
27-
fun apply(logic: Any?, data: Any? = null) = evaluate(logic, data).toString()
28+
fun apply(logic: Any?, data: Any? = null, safe: Boolean = true) =
29+
if(safe) try { evaluate(logic, data).toString() } catch (e: kotlin.NotImplementedError) { "false" }
30+
else evaluate(logic, data).toString()
2831

2932
/**
3033
* Add new operations http://jsonlogic.com/add_operation.html

src/test/kotlin/eu/afse/jsonlogic/JsonLogicTests.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,17 @@ class JsonLogicTests {
172172
fun unknownCustomOperation() {
173173
val jsonLogic = JsonLogic()
174174
assertThrows(kotlin.NotImplementedError::class.java) {
175-
jsonLogic.apply(mapOf("hello" to listOf(1, 2, 3)))
175+
jsonLogic.apply(mapOf("hello" to listOf(1, 2, 3)), safe = false)
176176
}
177177
}
178178

179+
@Test
180+
fun unknownCustomOperation2() {
181+
val jsonLogic = JsonLogic()
182+
val result = jsonLogic.apply(mapOf("hello" to listOf(1, 2, 3)), safe = true)
183+
assertEquals("false", result)
184+
}
185+
179186
@Test
180187
fun stringComparisonBug() {
181188
val jsonLogic = JsonLogic()

0 commit comments

Comments
 (0)