SpongePowered / Configurate

A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
https://configurate.aoeu.xyz
Apache License 2.0
386 stars 68 forks source link

Empty Files created when using Kotlin Classes without "extras-kotlin" module #526

Open xMrAfonso opened 2 weeks ago

xMrAfonso commented 2 weeks ago

I've been using Configurate with Kotlin and encountered some issues. The extras-kotlin module mentions that it adds support for data classes. I assumed this meant normal Kotlin classes would be fully supported out of the box. However, when attempting to create configurations with normal Kotlin classes, the files are created but remain empty with no errors being present on the console.

I'm unsure if this behavior is intended or if I'm missing something in my setup.

Why I'm Not Using extra-kotlin:
I'm not using the extra-kotlin module because it depends on kotlin-reflect, which has known compatibility issues with Paper loaders. Additionally, shading kotlin-reflect properly into my project has been problematic.

Method Used to Create Configurations:
Here’s the method I use to create and load configurations:

private fun <T> create(clazz: Class<T>, fileName: String): T? {
    try {
        Files.createDirectories(dataFolder)

        val path: Path = dataFolder.resolve(fileName)

        val loader: YamlConfigurationLoader = loader(path)
        val node: CommentedConfigurationNode = loader.load()
        val config = node.get(clazz)

        if (!Files.exists(path)) {
            Files.createFile(path)
            node.set(clazz, config)
        }

        loader.save(node)
        return config
    } catch (exception: IOException) {
        exception.printStackTrace()
    }
    return null
}

Possible Cause:
I suspect the issue might be related to the object mapper since Kotlin has its own quirks compared to Java. If that's the case, the documentation should specify that the extra-kotlin module not only supports data classes but also provides full support for normal Kotlin classes.

Expected Behavior:

Let me know if I’m missing something or if this is unintended behavior.

PS: Since I usually don't express myself very well, this message was formatted by AI and rechecked after by me to make sure both parties understand it clearly.