From 8b2b612da5d815d72cf4ddc74d2702f958bf54d5 Mon Sep 17 00:00:00 2001 From: Philipp Ossler Date: Wed, 18 Dec 2024 12:31:51 +0100 Subject: [PATCH] refactor: Move function validation Move the function validation in the interpreter before invoking the function. --- .../feel/impl/builtin/BuiltinFunction.scala | 16 +++++----------- .../impl/interpreter/FeelInterpreter.scala | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/scala/org/camunda/feel/impl/builtin/BuiltinFunction.scala b/src/main/scala/org/camunda/feel/impl/builtin/BuiltinFunction.scala index 12f347b23..8d9368029 100644 --- a/src/main/scala/org/camunda/feel/impl/builtin/BuiltinFunction.scala +++ b/src/main/scala/org/camunda/feel/impl/builtin/BuiltinFunction.scala @@ -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 { @@ -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") } } diff --git a/src/main/scala/org/camunda/feel/impl/interpreter/FeelInterpreter.scala b/src/main/scala/org/camunda/feel/impl/interpreter/FeelInterpreter.scala index 60072b841..f721a22fc 100644 --- a/src/main/scala/org/camunda/feel/impl/interpreter/FeelInterpreter.scala +++ b/src/main/scala/org/camunda/feel/impl/interpreter/FeelInterpreter.scala @@ -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) @@ -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) + } } }