diff --git a/src/main/scala/org/camunda/feel/impl/builtin/StringBuiltinFunctions.scala b/src/main/scala/org/camunda/feel/impl/builtin/StringBuiltinFunctions.scala index 23f573c2b..fa0b56eed 100644 --- a/src/main/scala/org/camunda/feel/impl/builtin/StringBuiltinFunctions.scala +++ b/src/main/scala/org/camunda/feel/impl/builtin/StringBuiltinFunctions.scala @@ -19,7 +19,8 @@ package org.camunda.feel.impl.builtin import com.fasterxml.uuid.{EthernetAddress, Generators} import org.camunda.feel.impl.builtin.BuiltinFunction.builtinFunction import org.camunda.feel.syntaxtree.{ValBoolean, ValError, ValList, ValNumber, ValString} - +import java.util.Base64 +import java.nio.charset.StandardCharsets import java.util.regex.Pattern import scala.util.Try @@ -42,7 +43,8 @@ object StringBuiltinFunctions { "split" -> List(splitFunction), "extract" -> List(extractFunction), "trim" -> List(trimFunction), - "uuid" -> List(uuidFunction) + "uuid" -> List(uuidFunction), + "to base64" -> List(toBase64Function) ) private def substringFunction = builtinFunction( @@ -274,4 +276,14 @@ object StringBuiltinFunctions { ValString(generator.generate.toString()) } ) + + private def toBase64Function = + builtinFunction( + params = List("value"), + invoke = { case List(ValString(value)) => + val bytes = value.getBytes(StandardCharsets.UTF_8) + ValString(Base64.getEncoder.encodeToString(bytes)) + } + ) + } diff --git a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala index b73763f74..de7eac8f0 100644 --- a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala +++ b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala @@ -173,4 +173,11 @@ class BuiltinStringFunctionsTest extends AnyFlatSpec with Matchers with FeelInte eval(" string length(uuid()) ") should be(ValNumber(36)) } + + "A to base64() function" should "return a string encoded as base64" in { + + eval(""" to base64("FEEL") """) should be(ValString("RkVFTA==")) + + eval(""" to base64(value: "Camunda") """) should be(ValString("Q2FtdW5kYQ==")) + } }