Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 1.17] fix: A unary-test expression with ? returns true/false #964

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
package org.camunda.feel.impl.builtin

import org.camunda.feel.logger
import org.camunda.feel.syntaxtree.{Val, ValError, ValFatalError, ValFunction, ValNull}
import org.camunda.feel.syntaxtree.{Val, ValError, ValFunction}

object BuiltinFunction {

Expand All @@ -33,13 +32,9 @@ object BuiltinFunction {
)
}

private def error: PartialFunction[List[Val], Any] = {
case args if args.exists(_.isInstanceOf[ValFatalError]) =>
args.find(_.isInstanceOf[ValFatalError])
case args if args.exists(_.isInstanceOf[ValError]) => args.find(_.isInstanceOf[ValError])
case args =>
val argumentList = args.map("'" + _ + "'").mkString(", ")
ValError(s"Illegal arguments: $argumentList")
private def error: PartialFunction[List[Val], Any] = { case args =>
val argumentList = args.map("'" + _ + "'").mkString(", ")
ValError(s"Illegal arguments: $argumentList")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class FeelInterpreter(private val valueMapper: ValueMapper) {
findFunction(context, name, params),
f =>
invokeFunction(f, params) match {
case fatalError: ValFatalError => fatalError
case ValError(failure) if name == "assert" =>
error(EvaluationFailureType.ASSERT_FAILURE, failure)
ValError(failure)
Expand Down Expand Up @@ -727,10 +728,19 @@ class FeelInterpreter(private val valueMapper: ValueMapper) {
}
}

function.invoke(paramList) match {
case fatalError: ValFatalError => fatalError
case e: ValError => e
case result => context.valueMapper.toVal(result)
// validate parameters
if (paramList.exists(_.isInstanceOf[ValFatalError])) {
paramList.find(_.isInstanceOf[ValFatalError]).get

} else if (paramList.exists(_.isInstanceOf[ValError])) {
paramList.find(_.isInstanceOf[ValError]).get

} else {
function.invoke(paramList) match {
case fatalError: ValFatalError => fatalError
case e: ValError => e
case result => context.valueMapper.toVal(result)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class InterpreterUnaryTest
evaluateUnaryTests("null", inputValue = null) should returnResult(true)
}

"A function" should "be invoked with ? (input value)" in {
"A function" should "be invoked with the special variable '?'" in {

evaluateUnaryTests(""" starts with(?, "f") """, "foo") should returnResult(true)
evaluateUnaryTests(""" starts with(?, "b") """, "foo") should returnResult(false)
Expand All @@ -541,6 +541,12 @@ class InterpreterUnaryTest
evaluateUnaryTests("< min(1,2,3)", 2) should returnResult(false)
}

it should "be invoked with the special variable '?' for a parameter with ANY type" in {

evaluateUnaryTests("list contains([481, 485, 551, 483], ?)", 481) should returnResult(true)
evaluateUnaryTests("list contains([481, 485, 551, 483], ?)", 999) should returnResult(false)
}

"A unary-tests expression" should "return true if it evaluates to a value that is equal to the implicit value" in {

evaluateUnaryTests("5", 5) should returnResult(true)
Expand Down
Loading