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 generate a UUID string #834

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<scala.version>2.13.13</scala.version>
<scala.binary.version>2.13.6</scala.binary.version>
<version.log4j>2.23.1</version.log4j>
<version.uuid>5.0.0</version.uuid>

<plugin.version.shade>3.5.2</plugin.version.shade>
<plugin.version.gpg>1.6</plugin.version.gpg>
Expand Down Expand Up @@ -90,6 +91,12 @@
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>${version.uuid}</version>
</dependency>
saig0 marked this conversation as resolved.
Show resolved Hide resolved

<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.13</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
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}

Expand All @@ -24,6 +25,8 @@ import scala.util.Try

object StringBuiltinFunctions {

private lazy val generator = Generators.timeBasedGenerator(EthernetAddress.fromInterface())

def functions = Map(
"substring" -> List(substringFunction, substringFunction3),
"string length" -> List(stringLengthFunction),
Expand All @@ -38,7 +41,8 @@ object StringBuiltinFunctions {
"matches" -> List(matchesFunction, matchesFunction3),
"split" -> List(splitFunction),
"extract" -> List(extractFunction),
"trim" -> List(trimFunction)
"trim" -> List(trimFunction),
"uuid" -> List(uuidFunction)
)

private def substringFunction = builtinFunction(
Expand Down Expand Up @@ -262,4 +266,12 @@ object StringBuiltinFunctions {
ValString(string.trim)
}
)

private def uuidFunction =
builtinFunction(
params = List(),
invoke = { case List() =>
ValString(generator.generate.toString())
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package org.camunda.feel.impl.builtin

import org.camunda.feel.impl.FeelIntegrationTest
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec
import org.camunda.feel.syntaxtree._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.math.BigDecimal.int2bigDecimal

Expand Down Expand Up @@ -163,4 +163,14 @@ class BuiltinStringFunctionsTest extends AnyFlatSpec with Matchers with FeelInte

eval(""" trim(" hello world ") """) should be(ValString("hello world"))
}

"A uuid() function" should "return a string" in {

eval(" uuid() ") shouldBe a[ValString]
}

it should "return a string of length 36" in {

eval(" string length(uuid()) ") should be(ValNumber(36))
}
}