Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add built-in function to encode a string as base64 #847

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 encode base64() function" should "return a string encoded as base64" in {
saig0 marked this conversation as resolved.
Show resolved Hide resolved

eval(""" to base64("FEEL") """) should be(ValString("RkVFTA=="))

eval(""" to base64(value: "Camunda") """) should be(ValString("Q2FtdW5kYQ=="))
}
}