Skip to content

Commit

Permalink
add randomUUID expression and scripting functions (#94)
Browse files Browse the repository at this point in the history
Thanks for the contribution!
  • Loading branch information
benmusson authored Sep 11, 2024
1 parent 33ff306 commit 5aba12e
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.imdc.extensions.common.UtilitiesExtensions
import org.imdc.extensions.common.addPropertyBundle
import org.imdc.extensions.common.expressions.IsAvailableFunction
import org.imdc.extensions.common.expressions.LogicalPredicate.Companion.registerLogicFunctions
import org.imdc.extensions.common.expressions.UUID4Function

@Suppress("unused")
class ClientHook : AbstractClientModuleHook() {
Expand Down Expand Up @@ -50,6 +51,11 @@ class ClientHook : AbstractClientModuleHook() {
IsAvailableFunction(),
)
registerLogicFunctions()
addFunction(
UUID4Function.NAME,
UUID4Function.CATEGORY,
UUID4Function(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -129,4 +130,9 @@ public FunctionFactory getFunctionFactory() {
return context.getExpressionFunctionFactory();
}
}

@ScriptFunction(docBundlePrefix = "UtilitiesExtensions")
public UUID getUUID4() {
return UUID.randomUUID();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.imdc.extensions.common.expressions

import com.inductiveautomation.ignition.common.expressions.Expression
import com.inductiveautomation.ignition.common.expressions.functions.AbstractFunction
import com.inductiveautomation.ignition.common.model.values.BasicQualifiedValue
import com.inductiveautomation.ignition.common.model.values.QualifiedValue
import java.util.UUID

class UUID4Function : AbstractFunction() {
override fun validateNumArgs(num: Int): Boolean = num == 0
override fun execute(expressions: Array<out Expression>): QualifiedValue {
return BasicQualifiedValue(UUID.randomUUID())
}

override fun getArgDocString(): String = ""
override fun getFunctionDisplayName(): String = NAME
override fun getType(): Class<*> = UUID::class.java

companion object {
const val NAME = "uuid4"
const val CATEGORY = "Advanced"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ deepCopy.returns=A plain Python primitive object.
evalExpression.desc=Evaluates the supplied expression. Provide keyword arguments to populate values to curly braces.
evalExpression.param.expression=The expression to evaluate.
evalExpression.returns=A QualifiedValue with the result of the provided expression.

getUUID4.desc=Returns type 4 pseudo randomly generated UUID.
getUUID4.returns=A type 4 pseudo randomly generated UUID.

24 changes: 24 additions & 0 deletions common/src/test/kotlin/org/imdc/extensions/common/UUID4Tests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.imdc.extensions.common

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.beInstanceOf
import org.imdc.extensions.common.ExpressionTestHarness.Companion.withFunction
import org.imdc.extensions.common.expressions.UUID4Function
import java.util.*

class UUID4Tests : FunSpec() {
init {
context("RandomUUID") {
withFunction("uuid4", UUID4Function()) {
test("Instance of UUID") {
evaluate("uuid4()") should beInstanceOf<UUID>()
}
test("Unique results") {
evaluate("uuid4() = uuid4()") shouldBe false
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.imdc.extensions.common

import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.beInstanceOf
import io.mockk.mockk
import org.python.core.Py
import org.python.core.PyDictionary
import org.python.core.PyList
import java.util.UUID

@Suppress("PyUnresolvedReferences", "PyInterpreter")
class UtilitiesExtensionsTests : JythonTest(
Expand Down Expand Up @@ -46,5 +49,13 @@ class UtilitiesExtensionsTests : JythonTest(
}
}
}
context("UUID4 tests") {
test("Instance of UUID") {
eval<UUID?>("utils.getUUID4()") should beInstanceOf<UUID>()
}
test("Unique results") {
eval<Boolean?>("utils.getUUID4() == utils.getUUID4()") shouldBe false
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.imdc.extensions.common.UtilitiesExtensions
import org.imdc.extensions.common.addPropertyBundle
import org.imdc.extensions.common.expressions.IsAvailableFunction
import org.imdc.extensions.common.expressions.LogicalPredicate.Companion.registerLogicFunctions
import org.imdc.extensions.common.expressions.UUID4Function

@Suppress("unused")
class DesignerHook : AbstractDesignerModuleHook() {
Expand Down Expand Up @@ -50,6 +51,11 @@ class DesignerHook : AbstractDesignerModuleHook() {
IsAvailableFunction(),
)
registerLogicFunctions()
addFunction(
UUID4Function.NAME,
UUID4Function.CATEGORY,
UUID4Function(),
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.imdc.extensions.common.UtilitiesExtensions
import org.imdc.extensions.common.addPropertyBundle
import org.imdc.extensions.common.expressions.IsAvailableFunction
import org.imdc.extensions.common.expressions.LogicalPredicate.Companion.registerLogicFunctions
import org.imdc.extensions.common.expressions.UUID4Function

@Suppress("unused")
class GatewayHook : AbstractGatewayModuleHook() {
Expand Down Expand Up @@ -57,6 +58,11 @@ class GatewayHook : AbstractGatewayModuleHook() {
IsAvailableFunction(),
)
registerLogicFunctions()
addFunction(
UUID4Function.NAME,
UUID4Function.CATEGORY,
UUID4Function(),
)
}
}

Expand Down

0 comments on commit 5aba12e

Please sign in to comment.