Skip to content

Commit

Permalink
fix: Kotlin evaluator is not supporting nullable values (#26)
Browse files Browse the repository at this point in the history
When evaluating an expression using the Kotlin Evaluator, when the input
map contains nullable values, the evaluator throws an exception to key
not found instead of returning null.

This change fixes it, returning null when a key is present but has null
as value, throwing the exception only when the key is not present in the
input map.
  • Loading branch information
rapatao authored Jul 5, 2024
1 parent 049bc2b commit b542633
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ fun Any.isFalse(): Expression = ExpressionBuilder.isFalse(this)
* @param right The object to compare with.
* @return An expression representing the equality comparison between the current object and the given object.
*/
infix fun Any.equalsTo(right: Any): Expression = ExpressionBuilder.left(this).equalsTo(right)
infix fun Any.equalsTo(right: Any?): Expression = ExpressionBuilder.left(this).equalsTo(right)

/**
* Compares this object with the specified [right] object for inequality.
*
* @param right The object to compare with this object.
* @return An [Expression] representing the inequality comparison result.
*/
infix fun Any.notEqualsTo(right: Any): Expression = ExpressionBuilder.left(this).notEqualsTo(right)
infix fun Any.notEqualsTo(right: Any?): Expression = ExpressionBuilder.left(this).notEqualsTo(right)

Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@ internal class SerializationExamplesBuilder {
),
)

private val casesFromTests = TestData.allCases()
private val casesFromTests = TestData.cases()
.flatMap { it.get().toList() }
.filterIsInstance<Expression>()
.filter {
// remove from serialization example rules that one of the operands is null
it.isValid() &&
(
(it.left != null && it.right != null) ||
!it.noneMatch.isNullOrEmpty() || !it.allMatch.isNullOrEmpty() || !it.anyMatch.isNullOrEmpty()
)
}
.toSet()

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SerializationTest {

companion object {
@JvmStatic
fun tests() = TestData.allCases()
fun tests() = TestData.cases()
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ class KotlinContext(
key.toBooleanStrictOrNull()
},
{
inputData.getOrElse(key) {
inputData[key]
},
{
if (!inputData.containsKey(key)) {
throw NoSuchElementException("$key not found")
}
},
null
}
).firstNotNullOfOrNull { it() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class BaseEvaluatorTest(
companion object {

@JvmStatic
fun tests() = TestData.allCases()
fun tests() = TestData.cases()

@JvmStatic
fun onFailure() = TestData.onFailureCases()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ object ExpressionCases {
"item.price" equalsTo 0,
false
),
Arguments.of(
"item.nullableStr" equalsTo null,
true
)
)

@Suppress("MagicNumber")
private fun notEqualsCases() = listOf(
Arguments.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object TestData {
val name: String,
val tags: List<String>,
val arrTags: Array<String>,
val nullableStr: String? = null,
)

val inputData = RequestData(
Expand All @@ -29,8 +30,6 @@ object TestData {
)
)

fun allCases(): List<Arguments> = cases()

fun onFailureCases(): List<Arguments> = (OnFailureCases.cases())

fun cases() =
Expand Down

0 comments on commit b542633

Please sign in to comment.