YamlConfigManager

The main entry point for KConfig -- a type-safe, annotation-driven YAML configuration manager for Kotlin.

YamlConfigManager provides a complete lifecycle for managing YAML configuration files backed by Kotlin data classes:

  1. Register custom serializers for types not natively supported.

  2. Register migrations to upgrade configuration schemas across versions.

  3. Load a configuration from a YAML file (creating it with defaults if absent).

  4. Save a configuration back to YAML with comments and proper formatting.

  5. Reload a configuration from disk on demand.

  6. Watch a configuration file for changes and auto-reload.

Full lifecycle example:

// 1. Register custom serializers (optional)
YamlConfigManager.registerSerializer(Duration::class, DurationSerializer)

// 2. Register migrations (optional)
YamlConfigManager.registerMigration(MyConfig::class, MigrateV1ToV2)

// 3. Load configuration
val config = YamlConfigManager.load<MyConfig>(dataFolder.resolve("config.yml"))

// 4. Save configuration
YamlConfigManager.save(dataFolder.resolve("config.yml"), config)

// 5. Reload on demand
val fresh = YamlConfigManager.reload<MyConfig>(dataFolder.resolve("config.yml"))

// 6. Watch for file changes
YamlConfigManager.watch<MyConfig>(dataFolder.resolve("config.yml")) { newConfig ->
println("Config reloaded: $newConfig")
}

// 7. Debug output with secret masking
println(YamlConfigManager.toDebugString(config))

// 8. Cleanup watchers on shutdown
YamlConfigManager.stopAllWatchers()

Built-in features:

  • Automatic default generation from data class constructor defaults

  • @Comment annotations rendered as YAML comments

  • @Env annotations for environment variable overrides

  • @Secret annotations for masking sensitive values in debug output

  • @Transient annotations for excluding fields from serialization

  • Schema versioning and migration with automatic backups

  • File watching with debounced auto-reload

  • Colored error reporting with field-level validation details

Built-in serializers: The manager automatically registers serializers for common types including SecretString. Additional Bukkit serializers are available via club.skidware.kconfig.bukkit.BukkitSerializers.registerAll.

Since

1.0

See also

Properties

Link copied to clipboard

The serializer registry used to resolve TypeSerializer implementations for custom types during serialization and deserialization.

Functions

Link copied to clipboard
inline fun <T : Any> load(file: File): T
fun <T : Any> load(file: File, klass: KClass<T>): T

Loads a configuration of type T from the specified file.

Link copied to clipboard
inline fun <T : Any> ref(file: File): ConfigRef<T>

Creates a live ConfigRef for the given configuration type and file.

Link copied to clipboard
fun registerMigration(klass: KClass<*>, migration: ConfigMigration)

Registers a ConfigMigration for the given configuration type klass.

Link copied to clipboard
inline fun <T : Any> registerSerializer(serializer: TypeSerializer<T>)

Registers a custom TypeSerializer for the reified type T.

fun <T : Any> registerSerializer(klass: KClass<T>, serializer: TypeSerializer<T>)

Registers a custom TypeSerializer for the given type klass.

Link copied to clipboard
inline fun <T : Any> reload(file: File): T

Reloads a configuration of type T from the specified file.

Link copied to clipboard
fun <T : Any> save(file: File, instance: T)

Saves a configuration instance to the specified file in YAML format.

Link copied to clipboard
fun setOutput(stream: PrintStream)

Sets the output stream used for error and diagnostic messages.

Link copied to clipboard

Stops all active file watchers and clears the watcher registry.

Link copied to clipboard
fun stopWatching(file: File)

Stops watching the specified file for changes.

Link copied to clipboard
fun <T : Any> toDebugString(instance: T): String

Produces a YAML-formatted debug string of the configuration instance with secret values masked.

Link copied to clipboard
inline fun <T : Any> watch(file: File, noinline onReload: (T) -> Unit): FileWatcher
fun <T : Any> watch(file: File, klass: KClass<T>, onReload: (T) -> Unit): FileWatcher

Watches a configuration file for changes and invokes onReload with the freshly loaded configuration whenever the file is modified.