From 59ec553b2615ffdabd1792ae999c444777f41c18 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Mon, 15 Jan 2024 11:54:11 +1100 Subject: [PATCH] fix: kotlin build somehow broke (#778) I also simplified how config/secrets work in Kotlin, because inline reified methods are a bit gross. --- bin/.act-0.2.57.pkg | 1 + bin/act | 1 + go-runtime/sdk/secrets.go | 6 ++--- kotlin-runtime/ftl-runtime/pom.xml | 14 +++++----- .../kotlin/xyz/block/ftl/config/Config.kt | 27 +++++++++++++------ .../kotlin/xyz/block/ftl/config/ConfigTest.kt | 6 ++--- .../ftl/secrets/{Secrets.kt => Secret.kt} | 21 +++++++++++---- .../xyz/block/ftl/secrets/SecretTest.kt | 2 +- pom.xml | 10 +++++-- 9 files changed, 60 insertions(+), 28 deletions(-) create mode 120000 bin/.act-0.2.57.pkg create mode 120000 bin/act rename kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/secrets/{Secrets.kt => Secret.kt} (54%) diff --git a/bin/.act-0.2.57.pkg b/bin/.act-0.2.57.pkg new file mode 120000 index 0000000000..383f4511d4 --- /dev/null +++ b/bin/.act-0.2.57.pkg @@ -0,0 +1 @@ +hermit \ No newline at end of file diff --git a/bin/act b/bin/act new file mode 120000 index 0000000000..1dade306e4 --- /dev/null +++ b/bin/act @@ -0,0 +1 @@ +.act-0.2.57.pkg \ No newline at end of file diff --git a/go-runtime/sdk/secrets.go b/go-runtime/sdk/secrets.go index 5cf2b85620..b9b6a0d514 100644 --- a/go-runtime/sdk/secrets.go +++ b/go-runtime/sdk/secrets.go @@ -34,13 +34,13 @@ func (s *SecretValue[Type]) String() string { } // Get returns the value of the secret from FTL. -func (c *SecretValue[Type]) Get() (out Type) { - value, ok := os.LookupEnv(fmt.Sprintf("FTL_SECRET_%s_%s", strings.ToUpper(c.module), strings.ToUpper(c.name))) +func (s *SecretValue[Type]) Get() (out Type) { + value, ok := os.LookupEnv(fmt.Sprintf("FTL_SECRET_%s_%s", strings.ToUpper(s.module), strings.ToUpper(s.name))) if !ok { return out } if err := json.Unmarshal([]byte(value), &out); err != nil { - panic(fmt.Errorf("failed to parse %s: %w", c, err)) + panic(fmt.Errorf("failed to parse %s: %w", s, err)) } return } diff --git a/kotlin-runtime/ftl-runtime/pom.xml b/kotlin-runtime/ftl-runtime/pom.xml index 53ded133cf..fae05e2ed5 100644 --- a/kotlin-runtime/ftl-runtime/pom.xml +++ b/kotlin-runtime/ftl-runtime/pom.xml @@ -1,6 +1,6 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 @@ -108,14 +108,16 @@ test - + + org.junit.jupiter + junit-jupiter + org.junit-pioneer junit-pioneer - 2.1.0 - test + 2.2.0 + compile - diff --git a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/config/Config.kt b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/config/Config.kt index 5aa90467e9..8a1b645c3e 100644 --- a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/config/Config.kt +++ b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/config/Config.kt @@ -2,20 +2,31 @@ package xyz.block.ftl.config import xyz.block.ftl.serializer.makeGson -class Secret(val name: String) { - val _module: String - val _gson = makeGson() +class Config(private val cls: Class, val name: String) { + private val module: String + private val gson = makeGson() + + companion object { + /** + * A convenience method for creating a new Secret. + * + *
+     *   val secret = Config.new("test")
+     * 
+ * + */ + inline fun new(name: String): Config = Config(T::class.java, name) } init { val caller = Thread.currentThread().stackTrace[2].className require(caller.startsWith("ftl.") || caller.startsWith("xyz.block.ftl.config.")) { "Config must be defined in an FTL module not $caller" } val parts = caller.split(".") - _module = parts[parts.size - 2] + module = parts[parts.size - 2] } - inline fun get(): T { - val key = "FTL_CONFIG_${_module.uppercase()}_${name.uppercase()}" - val value = System.getenv(key) ?: throw Exception("Config key ${_module}.${name} not found") - return _gson.fromJson(value, T::class.java) + fun get(): T { + val key = "FTL_CONFIG_${module.uppercase()}_${name.uppercase()}" + val value = System.getenv(key) ?: throw Exception("Config key ${module}.${name} not found") + return gson.fromJson(value, cls) } } diff --git a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/config/ConfigTest.kt b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/config/ConfigTest.kt index 2faeb242ef..627196b206 100644 --- a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/config/ConfigTest.kt +++ b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/config/ConfigTest.kt @@ -6,9 +6,9 @@ import org.junitpioneer.jupiter.SetEnvironmentVariable class ConfigTest { @Test - @SetEnvironmentVariable(key = "FTL_SECRET_SECRETS_TEST", value = "testingtesting") + @SetEnvironmentVariable(key = "FTL_CONFIG_CONFIG_TEST", value = "testingtesting") fun testSecret() { - val secret = Secret("test") - assertEquals("testingtesting", secret.get()) + val config = Config.new("test") + assertEquals("testingtesting", config.get()) } } diff --git a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/secrets/Secrets.kt b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/secrets/Secret.kt similarity index 54% rename from kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/secrets/Secrets.kt rename to kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/secrets/Secret.kt index bfe1baf244..f0061589ad 100644 --- a/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/secrets/Secrets.kt +++ b/kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/secrets/Secret.kt @@ -2,9 +2,20 @@ package xyz.block.ftl.secrets import xyz.block.ftl.serializer.makeGson -class Secret(val name: String) { - val module: String - val gson = makeGson() +class Secret(private val cls: Class, private val name: String) { + private val module: String + private val gson = makeGson() + + companion object { + /** + * A convenience method for creating a new Secret. + * + *
+     *   val secret = Secret.new("test")
+     * 
+ * + */ + inline fun new(name: String): Secret = Secret(T::class.java, name) } init { val caller = Thread.currentThread().getStackTrace()[2].className @@ -13,9 +24,9 @@ class Secret(val name: String) { module = parts[parts.size - 2] } - inline fun get(): T { + fun get(): T { val key = "FTL_SECRET_${module.uppercase()}_${name.uppercase()}" val value = System.getenv(key) ?: throw Exception("Secret ${module}.${name} not found") - return gson.fromJson(value, T::class.java) + return gson.fromJson(value, cls) } } diff --git a/kotlin-runtime/ftl-runtime/src/test/kotlin/xyz/block/ftl/secrets/SecretTest.kt b/kotlin-runtime/ftl-runtime/src/test/kotlin/xyz/block/ftl/secrets/SecretTest.kt index ba3a0fd440..7dcaa3961d 100644 --- a/kotlin-runtime/ftl-runtime/src/test/kotlin/xyz/block/ftl/secrets/SecretTest.kt +++ b/kotlin-runtime/ftl-runtime/src/test/kotlin/xyz/block/ftl/secrets/SecretTest.kt @@ -8,7 +8,7 @@ class SecretTest { @Test @SetEnvironmentVariable(key = "FTL_SECRET_SECRETS_TEST", value = "testingtesting") fun testSecret() { - val secret = Secret("test") + val secret = Secret.new("test") assertEquals("testingtesting", secret.get()) } } diff --git a/pom.xml b/pom.xml index 576444a43c..0057db8795 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 @@ -150,6 +150,12 @@ junit-jupiter-params test + + org.junit-pioneer + junit-pioneer + 2.2.0 + test +