Skip to content

Commit

Permalink
refactor: Move function validation
Browse files Browse the repository at this point in the history
Move the function validation in the interpreter before invoking the function.
  • Loading branch information
saig0 committed Dec 18, 2024
1 parent 632bbe1 commit 8b2b612
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
16 changes: 5 additions & 11 deletions src/main/scala/org/camunda/feel/impl/builtin/BuiltinFunction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.camunda.feel.impl.builtin

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

object BuiltinFunction {

Expand All @@ -27,20 +27,14 @@ object BuiltinFunction {
): ValFunction = {
ValFunction(
params = params,
invoke = validateArgs.orElse(invoke).orElse(error),
invoke = invoke.orElse(error),
hasVarArgs = hasVarArgs
)
}

private def validateArgs: PartialFunction[List[Val], Any] = {
case args if args.exists(_.isInstanceOf[ValFatalError]) => args.find(_.isInstanceOf[ValFatalError]).get
case args if args.exists(_.isInstanceOf[ValError]) => args.find(_.isInstanceOf[ValError]).get
}

private def error: PartialFunction[List[Val], Any] = {
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

0 comments on commit 8b2b612

Please sign in to comment.