sksamuel / hoplite

A boilerplate-free Kotlin config library for loading configuration files as data classes
Apache License 2.0
923 stars 74 forks source link

Keys with periods are not parsed correctly #388

Closed erichulburd closed 1 year ago

erichulburd commented 1 year ago

Hello, thanks for the useful library. I believe we hit an error in toml parsing that I thought I'd let you know about. We are on version 1.4.16. Unsure if this is an issue in the latest version.

Given the following parsing function

data class Configuration (
    val value: Int = 5,
    val values: Map<String, String> = mapOf(),
)

fun parseTopicsConfig(configFile: String): Configuration {
    val configLoader = parseConfig(configFile)
    val c: Configuration = configLoader.loadConfigOrThrow()
    if (errors.size != 0) {
        Logger.getGlobal().severe(errors.toString())
        throw Exception("configuration is invalid")
    }

    return c
}

fun parseConfig(configFile: String): ConfigLoader {
    val fileName = Paths.get(configFile).toAbsolutePath().toString()
    val contents = File(fileName).readText(Charsets.UTF_8)
    return ConfigLoader.Builder()
        .addSource(TomlPropertySource(contents))
        .addSource(
            EnvironmentVariablesPropertySource(
                useUnderscoresAsSeparator = true,
                allowUppercaseNames = true,
            )
        )
        .build()
}

the following will fail to parse:

value = 10
[values]
"cleanup.policy" = "compact"
"delete.retention.ms" = "604800000"

however the following does parse as expected:

value = 10
[values]
"cleanup" = "compact"
"delete" = "604800000"

It seems to me that keys with periods in them cause in issue. In the first case config.values["cleanup.policy"] == null, whereas in the second case config.values["cleanup"] == "compact" as expected.

sksamuel commented 1 year ago

Hi,

I've added a test for this here: hoplite-toml/src/test/kotlin/com/sksamuel/hoplite/toml/PeriodKeyTest.kthoplite-toml/src/test/kotlin/com/sksamuel/hoplite/toml/PeriodKeyTest.kt

It passes, so I think you just need to upgrade to latest version. If you still have issues please reopen the ticket.