Skip to content

Commit

Permalink
Use object mapper to allow plugin specific type safe config
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvisser committed Sep 11, 2023
1 parent 24cf855 commit 38193f8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
}
Expand All @@ -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<Pair<String, String>>()

inline fun <reified T> convertTo() = objectMapper.convertValue<T>(pluginConfig.config)
}

class PluginConfig(pluginName: String, private val objectMapper: ObjectMapper, osUtils: OsUtils) {
Expand All @@ -68,14 +80,20 @@ class PluginConfig(pluginName: String, private val objectMapper: ObjectMapper, o

inline operator fun <reified T> 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?) {
config[key] = value
}

fun save() {
objectMapper.writeValue(pluginFile, config)
objectMapper.writerWithDefaultPrettyPrinter().writeValue(pluginFile, config)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class PluginConfigureCommand(
var hasPluginSpecificConfig = false
val pluginConfigFile = config.pluginConfigDirectory().resolve("$pluginName.json").toFile()
val pluginConfig = if (pluginConfigFile.exists()) objectMapper.readValue<Map<String, Any?>>(pluginConfigFile) else emptyMap()

val answers = mutableMapOf<String, Any>()
val answers = pluginConfig.toMutableMap()

config.configure {
hasPluginSpecificConfig = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 38193f8

Please sign in to comment.