From 71c3e99ef66c9c23f4d5def425d6dd68575890ab Mon Sep 17 00:00:00 2001 From: sbuettner Date: Mon, 6 May 2024 17:24:30 +0200 Subject: [PATCH 1/4] feat: add built-in function to encode a string as base64 --- .../impl/builtin/StringBuiltinFunctions.scala | 16 ++++++++++++++-- .../builtin/BuiltinStringFunctionsTest.scala | 7 +++++++ 2 files changed, 21 insertions(+), 2 deletions(-) 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..26326cfa9 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), + "encode base64" -> List(encodeBase64Function) ) private def substringFunction = builtinFunction( @@ -274,4 +276,14 @@ object StringBuiltinFunctions { ValString(generator.generate.toString()) } ) + + private def encodeBase64Function = + builtinFunction( + params = List("string"), + invoke = { case List(ValString(string)) => + val bytes = string.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..b2a07d2b1 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)) } + + it should "return a string encoded as base64" in { + + eval(""" encode base64("FEEL") """) should be(ValString("RkVFTA==")) + + eval(""" encode base64("Camunda") """) should be(ValString("Q2FtdW5kYQ==")) + } } From 8daac8080a2e137f129740feb259be8a630dd48a Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 7 May 2024 11:23:17 +0200 Subject: [PATCH 2/4] Update src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala Co-authored-by: Philipp Ossler --- .../camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b2a07d2b1..2408e5867 100644 --- a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala +++ b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala @@ -174,7 +174,7 @@ class BuiltinStringFunctionsTest extends AnyFlatSpec with Matchers with FeelInte eval(" string length(uuid()) ") should be(ValNumber(36)) } - it should "return a string encoded as base64" in { + "A encode base64() function" should "return a string encoded as base64" in { eval(""" encode base64("FEEL") """) should be(ValString("RkVFTA==")) From 32e3fbc071486e51e410651313d514bc444c2400 Mon Sep 17 00:00:00 2001 From: sbuettner Date: Tue, 7 May 2024 11:28:32 +0200 Subject: [PATCH 3/4] Rename to "to base64" Add test with named parameter --- .../feel/impl/builtin/StringBuiltinFunctions.scala | 10 +++++----- .../feel/impl/builtin/BuiltinStringFunctionsTest.scala | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) 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 26326cfa9..fa0b56eed 100644 --- a/src/main/scala/org/camunda/feel/impl/builtin/StringBuiltinFunctions.scala +++ b/src/main/scala/org/camunda/feel/impl/builtin/StringBuiltinFunctions.scala @@ -44,7 +44,7 @@ object StringBuiltinFunctions { "extract" -> List(extractFunction), "trim" -> List(trimFunction), "uuid" -> List(uuidFunction), - "encode base64" -> List(encodeBase64Function) + "to base64" -> List(toBase64Function) ) private def substringFunction = builtinFunction( @@ -277,11 +277,11 @@ object StringBuiltinFunctions { } ) - private def encodeBase64Function = + private def toBase64Function = builtinFunction( - params = List("string"), - invoke = { case List(ValString(string)) => - val bytes = string.getBytes(StandardCharsets.UTF_8) + 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 2408e5867..afaae1d00 100644 --- a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala +++ b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala @@ -176,8 +176,8 @@ class BuiltinStringFunctionsTest extends AnyFlatSpec with Matchers with FeelInte "A encode base64() function" should "return a string encoded as base64" in { - eval(""" encode base64("FEEL") """) should be(ValString("RkVFTA==")) + eval(""" to base64("FEEL") """) should be(ValString("RkVFTA==")) - eval(""" encode base64("Camunda") """) should be(ValString("Q2FtdW5kYQ==")) + eval(""" to base64(value: "Camunda") """) should be(ValString("Q2FtdW5kYQ==")) } } From cdc515a9169fcf049797b2e98bfcaca0a87a6df3 Mon Sep 17 00:00:00 2001 From: Philipp Ossler Date: Tue, 7 May 2024 14:33:01 +0200 Subject: [PATCH 4/4] test: Rename test group --- .../camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 afaae1d00..de7eac8f0 100644 --- a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala +++ b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinStringFunctionsTest.scala @@ -174,7 +174,7 @@ class BuiltinStringFunctionsTest extends AnyFlatSpec with Matchers with FeelInte eval(" string length(uuid()) ") should be(ValNumber(36)) } - "A encode base64() function" should "return a string encoded as base64" in { + "A to base64() function" should "return a string encoded as base64" in { eval(""" to base64("FEEL") """) should be(ValString("RkVFTA=="))