Skip to content

Commit

Permalink
feat(PluginFactoryGenerator): Use default values for unparsable types
Browse files Browse the repository at this point in the history
If non-string types are not parsable but have a default value, do not
throw but fall back to that value.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Jan 10, 2025
1 parent 35b668c commit 8a3bf52
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions plugins/compiler/src/main/kotlin/PluginFactoryGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,21 @@ class PluginFactoryGenerator(private val codeGenerator: CodeGenerator) {
pluginOptions.forEach { option ->
add(" ${option.name} = ")

fun readOption(name: String) =
fun readOption(name: String) {
val orNull = "OrNull".takeIf { option.defaultValue != null }.orEmpty()

when (option.type) {
PluginOptionType.BOOLEAN -> add("config.options[%S]?.toBooleanStrict()", name)
PluginOptionType.INTEGER -> add("config.options[%S]?.toInt()", name)
PluginOptionType.LONG -> add("config.options[%S]?.toLong()", name)
PluginOptionType.BOOLEAN -> add("config.options[%S]?.toBooleanStrict$orNull()", name)
PluginOptionType.INTEGER -> add("config.options[%S]?.toInt$orNull()", name)
PluginOptionType.LONG -> add("config.options[%S]?.toLong$orNull()", name)
PluginOptionType.SECRET -> add("config.secrets[%S]?.let { %T(it) }", name, Secret::class)
PluginOptionType.STRING -> add("config.options[%S]", name)
PluginOptionType.STRING_LIST -> add(
"config.options[%S]?.split(\",\")?.map { it.trim() }",
name
)
}
}

// Add code to read the option from the options or secrets maps based on its type.
readOption(option.name)
Expand Down

0 comments on commit 8a3bf52

Please sign in to comment.