Skip to content

Commit

Permalink
fix: kotlin build somehow broke (#778)
Browse files Browse the repository at this point in the history
I also simplified how config/secrets work in Kotlin, because inline
reified methods are a bit gross.
  • Loading branch information
alecthomas authored Jan 15, 2024
1 parent 27d1b2f commit 59ec553
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 28 deletions.
1 change: 1 addition & 0 deletions bin/.act-0.2.57.pkg
1 change: 1 addition & 0 deletions bin/act
6 changes: 3 additions & 3 deletions go-runtime/sdk/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 8 additions & 6 deletions kotlin-runtime/ftl-runtime/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
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">

<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -108,14 +108,16 @@
</exclusions>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>2.1.0</version>
<scope>test</scope>
<version>2.2.0</version>
<scope>compile</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ package xyz.block.ftl.config

import xyz.block.ftl.serializer.makeGson

class Secret<T>(val name: String) {
val _module: String
val _gson = makeGson()
class Config<T>(private val cls: Class<T>, val name: String) {
private val module: String
private val gson = makeGson()

companion object {
/**
* A convenience method for creating a new Secret.
*
* <pre>
* val secret = Config.new<String>("test")
* </pre>
*
*/
inline fun <reified T> new(name: String): Config<T> = 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 <reified T> 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>("test")
assertEquals("testingtesting", secret.get())
val config = Config.new<String>("test")
assertEquals("testingtesting", config.get())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ package xyz.block.ftl.secrets

import xyz.block.ftl.serializer.makeGson

class Secret<T>(val name: String) {
val module: String
val gson = makeGson()
class Secret<T>(private val cls: Class<T>, private val name: String) {
private val module: String
private val gson = makeGson()

companion object {
/**
* A convenience method for creating a new Secret.
*
* <pre>
* val secret = Secret.new<String>("test")
* </pre>
*
*/
inline fun <reified T> new(name: String): Secret<T> = Secret(T::class.java, name) }

init {
val caller = Thread.currentThread().getStackTrace()[2].className
Expand All @@ -13,9 +24,9 @@ class Secret<T>(val name: String) {
module = parts[parts.size - 2]
}

inline fun <reified T> 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SecretTest {
@Test
@SetEnvironmentVariable(key = "FTL_SECRET_SECRETS_TEST", value = "testingtesting")
fun testSecret() {
val secret = Secret<String>("test")
val secret = Secret.new<String>("test")
assertEquals("testingtesting", secret.get())
}
}
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
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">

<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -150,6 +150,12 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit 59ec553

Please sign in to comment.