Skip to content

Commit

Permalink
Adds options to supress exception and return false
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonis Lilis committed Mar 6, 2019
1 parent f7dbe39 commit 628b661
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/kotlin/eu/afse/jsonlogic/JsonLogic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ class JsonLogic {
*
* @param logic the logic
* @param data the data
* @param safe if true an exception is returned as false else exceptions are thrown
* @return evaluation result
*/
@JvmOverloads
fun apply(logic: Any?, data: Any? = null) = evaluate(logic, data).toString()
fun apply(logic: Any?, data: Any? = null, safe: Boolean = true) =
if(safe) try { evaluate(logic, data).toString() } catch (e: kotlin.NotImplementedError) { "false" }
else evaluate(logic, data).toString()

/**
* Add new operations http://jsonlogic.com/add_operation.html
Expand Down
9 changes: 8 additions & 1 deletion src/test/kotlin/eu/afse/jsonlogic/JsonLogicTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,17 @@ class JsonLogicTests {
fun unknownCustomOperation() {
val jsonLogic = JsonLogic()
assertThrows(kotlin.NotImplementedError::class.java) {
jsonLogic.apply(mapOf("hello" to listOf(1, 2, 3)))
jsonLogic.apply(mapOf("hello" to listOf(1, 2, 3)), safe = false)
}
}

@Test
fun unknownCustomOperation2() {
val jsonLogic = JsonLogic()
val result = jsonLogic.apply(mapOf("hello" to listOf(1, 2, 3)), safe = true)
assertEquals("false", result)
}

@Test
fun stringComparisonBug() {
val jsonLogic = JsonLogic()
Expand Down

0 comments on commit 628b661

Please sign in to comment.