Skip to content

Commit

Permalink
Merge pull request #847 from camunda/846-encode-base64
Browse files Browse the repository at this point in the history
feat: add built-in function to encode a string as base64
  • Loading branch information
saig0 authored May 7, 2024
2 parents c90ad66 + cdc515a commit 9842304
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down Expand Up @@ -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))
}
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -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=="))
}
}

0 comments on commit 9842304

Please sign in to comment.