Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using .at(path).toValue errors if any config files are missing the entry #76

Open
dtanner opened this issue Feb 16, 2022 · 2 comments
Open
Labels

Comments

@dtanner
Copy link

dtanner commented Feb 16, 2022

Test case here: https://github.com/dtanner/konf-question/blob/get_at_question/src/test/kotlin/question/ConfigTest.kt#L32

I'd like to avoid using ConfigSpec classes, and instead use the toValue() function to instantiate configuration directly into target classes. For the case where I want to grab a single property, this works fine if the config files I'm loading from all contain all the property I'm looking for, but if any of them don't, it errors. e.g. relevant snippets from the linked test case:

config1.conf:

server {
  host = "0.0.0.0"
}

config2-missing-host.conf:

server {
  # if i uncomment the host here, things work as expected.
  # host = "1.1.1.1"
}

test:

package question

import com.uchuhimo.konf.Config
import com.uchuhimo.konf.source.hocon
import com.uchuhimo.konf.toValue
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test

class ConfigTest {

    data class ServerConfig(val host: String)

    @Test
    fun `foo hocon`() {
        val config = Config()
            .from.hocon.resource("config1.conf")
            .from.hocon.resource("config2-missing-host.conf", optional = true)

        val serverConfig = config.at("server").toValue<ServerConfig>()
        serverConfig.host shouldBe "0.0.0.0"

        /*
        The following line throws:

        item root is unset
        com.uchuhimo.konf.UnsetValueException: item root is unset
        at app//com.uchuhimo.konf.BaseConfig.getOrNull(BaseConfig.kt:187)
        at app//com.uchuhimo.konf.BaseConfig.getOrNull$default(BaseConfig.kt:179)
        at app//com.uchuhimo.konf.BaseConfig.get(BaseConfig.kt:159)
        at app//com.uchuhimo.konf.BaseConfig$property$1.getValue(BaseConfig.kt:527)
         */
        val host = config.at("server.host").toValue<String>()
        host shouldBe "0.0.0.0"
    }

}

Is there a different/better way to get at individual properties and objects without using Spec files? Or is this reasonable to work as expected? Thanks.

@solonovamax
Copy link

@dtanner afaik, the correct way to query a config value using the unsafe api is with

val host = config.get<String>("server.host")

This is what is shown in the README

@stale
Copy link

stale bot commented May 24, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

@stale stale bot added the stale label May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants