From 38193f8fd1f2203c46064ca0e436917b4070411f Mon Sep 17 00:00:00 2001 From: Martin Visser Date: Fri, 1 Sep 2023 11:03:10 +0200 Subject: [PATCH] Use object mapper to allow plugin specific type safe config --- .../ret/configuration/BasePluginConfig.kt | 30 +++++++++++++++---- .../version/VersionProperties.kt | 2 +- .../ret/commands/PluginConfigureCommand.kt | 3 +- .../ret/commands/PluginInitializeCommand.kt | 3 +- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/ret-core/src/main/kotlin/io/rabobank/ret/configuration/BasePluginConfig.kt b/ret-core/src/main/kotlin/io/rabobank/ret/configuration/BasePluginConfig.kt index caf1cf6..4c958c5 100644 --- a/ret-core/src/main/kotlin/io/rabobank/ret/configuration/BasePluginConfig.kt +++ b/ret-core/src/main/kotlin/io/rabobank/ret/configuration/BasePluginConfig.kt @@ -1,8 +1,10 @@ package io.rabobank.ret.configuration import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.convertValue import com.fasterxml.jackson.module.kotlin.readValue import io.quarkus.logging.Log +import io.rabobank.ret.RetConsole import io.rabobank.ret.util.OsUtils import jakarta.annotation.PostConstruct import jakarta.enterprise.context.Dependent @@ -29,23 +31,31 @@ open class BasePluginConfig : Configurable { @Inject lateinit var retConfig: RetConfig - val config by lazy { PluginConfig(pluginName, objectMapper, osUtils) } + @Inject + lateinit var retConsole: RetConsole + + val pluginConfig by lazy { PluginConfig(pluginName, objectMapper, osUtils) } @PostConstruct fun migrateOldConfig() { + val configCopy = pluginConfig.config.toMap() + val keysToMigrate = keysToMigrate() + if (keysToMigrate.isNotEmpty()) { - Log.info("Migrating old configuration to plugin specific configuration") + Log.debug("Migrating old configuration to plugin specific configuration") keysToMigrate.forEach { (oldKey, newKey) -> val oldValue = retConfig[oldKey] if (oldValue != null) { - config[newKey] = oldValue + pluginConfig[newKey] = oldValue retConfig.remove(oldKey) } } + } - config.save() + if (configCopy != pluginConfig.config) { + pluginConfig.save() retConfig.save() } } @@ -58,6 +68,8 @@ open class BasePluginConfig : Configurable { * The left should be the name of the old key and the right the name of the new key */ open fun keysToMigrate() = emptyList>() + + inline fun convertTo() = objectMapper.convertValue(pluginConfig.config) } class PluginConfig(pluginName: String, private val objectMapper: ObjectMapper, osUtils: OsUtils) { @@ -68,7 +80,13 @@ class PluginConfig(pluginName: String, private val objectMapper: ObjectMapper, o inline operator fun get(key: String): T? { val value = config[key] - return if (value is T?) value else error("The config value cannot be cast to ${T::class.java}") + return if (value is T?) { + value + } else { + error( + "The config value '$key' cannot be cast to ${T::class.java}, because it's of type ${value?.javaClass}", + ) + } } operator fun set(key: String, value: Any?) { @@ -76,6 +94,6 @@ class PluginConfig(pluginName: String, private val objectMapper: ObjectMapper, o } fun save() { - objectMapper.writeValue(pluginFile, config) + objectMapper.writerWithDefaultPrettyPrinter().writeValue(pluginFile, config) } } diff --git a/ret-core/src/main/kotlin/io/rabobank/ret/configuration/version/VersionProperties.kt b/ret-core/src/main/kotlin/io/rabobank/ret/configuration/version/VersionProperties.kt index d9b8c9e..ee987d7 100644 --- a/ret-core/src/main/kotlin/io/rabobank/ret/configuration/version/VersionProperties.kt +++ b/ret-core/src/main/kotlin/io/rabobank/ret/configuration/version/VersionProperties.kt @@ -50,6 +50,6 @@ class VersionProperties { private fun loadGitProperties() { VersionProperties::class.java.classLoader.getResourceAsStream(GIT_PROPERTIES) ?.run { properties.load(this) } - ?: Log.info("No Git information available: cannot load git.properties file.") + ?: Log.debug("No Git information available: cannot load git.properties file.") } } diff --git a/ret-plugin/src/main/kotlin/io/rabobank/ret/commands/PluginConfigureCommand.kt b/ret-plugin/src/main/kotlin/io/rabobank/ret/commands/PluginConfigureCommand.kt index 0a6c74f..7a338b8 100644 --- a/ret-plugin/src/main/kotlin/io/rabobank/ret/commands/PluginConfigureCommand.kt +++ b/ret-plugin/src/main/kotlin/io/rabobank/ret/commands/PluginConfigureCommand.kt @@ -51,8 +51,7 @@ class PluginConfigureCommand( var hasPluginSpecificConfig = false val pluginConfigFile = config.pluginConfigDirectory().resolve("$pluginName.json").toFile() val pluginConfig = if (pluginConfigFile.exists()) objectMapper.readValue>(pluginConfigFile) else emptyMap() - - val answers = mutableMapOf() + val answers = pluginConfig.toMutableMap() config.configure { hasPluginSpecificConfig = true diff --git a/ret-plugin/src/main/kotlin/io/rabobank/ret/commands/PluginInitializeCommand.kt b/ret-plugin/src/main/kotlin/io/rabobank/ret/commands/PluginInitializeCommand.kt index a9931d5..003d776 100644 --- a/ret-plugin/src/main/kotlin/io/rabobank/ret/commands/PluginInitializeCommand.kt +++ b/ret-plugin/src/main/kotlin/io/rabobank/ret/commands/PluginInitializeCommand.kt @@ -86,6 +86,7 @@ class PluginInitializeCommand( private fun createPluginInformationFile(pluginName: String) { val pluginDefinition = IntrospectionUtil.introspect(commandSpec.root(), pluginName) - objectMapper.writeValue(pluginDirectory.resolve("$pluginName.plugin").toFile(), pluginDefinition) + objectMapper.writerWithDefaultPrettyPrinter() + .writeValue(pluginDirectory.resolve("$pluginName.plugin").toFile(), pluginDefinition) } }