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

Konf examples #43

Closed
DanySK opened this issue Oct 24, 2019 · 2 comments
Closed

Konf examples #43

DanySK opened this issue Oct 24, 2019 · 2 comments

Comments

@DanySK
Copy link
Contributor

DanySK commented Oct 24, 2019

Hi,
where can I find a library of examples for konf? I'm trying to get past the base example presented, but I can't understand how to do more advanced stuff with the library.
I want to parse e.g.:

tests:
  - description: "latex guide should build"
    configuration:
      tasks: [ "buildLatex" ]
      options: [ "--debug" ]
    expectation:
      file_exists: [ "output.pdf" ]

I tried the following:

data class Configuration(val tasks: List<String>, val options: List<String>)
data class Expectation(val file_exists: List<String>)
data class Test(val description: String, val configuration: Configuration, val expectation: Expectation)
object Root : ConfigSpec("") {
    val tests by required<List<Test>>()
}
val testConfiguration = Config().from.yaml.inputStream(myInputStream)
println(testConfiguration[Root.tests])

But this causes:

java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at kotlin.reflect.jvm.internal.calls.CallerImpl$Constructor.call(CallerImpl.kt:41)
	at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:106)
	at kotlin.reflect.jvm.internal.KCallableImpl.callDefaultMethod$kotlin_reflection(KCallableImpl.kt:152)
	at kotlin.reflect.jvm.internal.KCallableImpl.callBy(KCallableImpl.kt:110)
	at kotlin.reflect.full.KClasses.createInstance(KClasses.kt:283)
	at io.kotlintest.runner.jvm.JvmKt.instantiateSpec(jvm.kt:15)
	at io.kotlintest.runner.jvm.TestEngine.createSpec(TestEngine.kt:122)
	at io.kotlintest.runner.jvm.TestEngine.access$createSpec(TestEngine.kt:19)
	at io.kotlintest.runner.jvm.TestEngine$submitSpec$1.run(TestEngine.kt:105)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: com.uchuhimo.konf.NoSuchItemException: cannot find item tests in config
	at com.uchuhimo.konf.BaseConfig.getOrNull(BaseConfig.kt:248)
	at com.uchuhimo.konf.BaseConfig.getOrNull(BaseConfig.kt:245)
	at com.uchuhimo.konf.BaseConfig.getOrNull$default(BaseConfig.kt:188)
	at com.uchuhimo.konf.BaseConfig.get(BaseConfig.kt:168)
	at my.call.site

It's unclear to me why and how can I debug this.

Thanks!

@DanySK
Copy link
Contributor Author

DanySK commented Oct 24, 2019

Same result with:

data class Configuration(val tasks: List<String>, val options: List<String>) {
    companion object : ConfigSpec("configuration") {
        val tasks by TestSpec.ConfigurationSpec.required<List<String>>()
        val options by TestSpec.ConfigurationSpec.optional<List<String>>(emptyList())
    }
}
data class Expectation(val file_exists: List<String>) {
    companion object : ConfigSpec("expectation") {
        val file_exists by TestSpec.ExpectationSpec.optional<List<String>>(emptyList())
    }
}
data class Test(val description: String, val configuration: Configuration, val expectation: Expectation) {
    companion object : ConfigSpec("") {
        val description by required<String>()
        val configuration by required<Configuration>()
        val expectation by required<Expectation>()
    }
}
object Root : ConfigSpec("") {
    val tests by required<List<Test>>()
}

I think the most unclear thing to me is how specifications get bound to objects besides built-in ones

@DanySK
Copy link
Contributor Author

DanySK commented Oct 24, 2019

I'm closing this by myself: specs were not being loaded into the Config. Here is the solution for parsing the example I posted, for future reference should it be useful to anybody else:

data class Configuration(val tasks: List<String>, val options: List<String>) {
    companion object : ConfigSpec() {
        val tasks by TestSpec.ConfigurationSpec.required<List<String>>()
        val options by TestSpec.ConfigurationSpec.optional<List<String>>(emptyList())
    }
}
data class Expectation(val file_exists: List<String>) {
    companion object : ConfigSpec() {
        val file_exists by TestSpec.ExpectationSpec.optional<List<String>>(emptyList())
    }
}
data class Test(val description: String, val configuration: Configuration, val expectation: Expectation) {
    companion object : ConfigSpec() {
        val description by required<String>()
        val configuration by required<Configuration>()
        val expectation by required<Expectation>()
    }
}
object Root : ConfigSpec("") {
    val tests by required<List<Test>>()
}
val testConfiguration = Config({
                addSpec(Root)
                addSpec(Test)
                addSpec(Expectation)
                addSpec(Configuration)
            }).from.yaml.inputStream(it.open())
println(testConfiguration[Root.tests])

prints out:
[ Test(description=latex guide by M. Gates should build, configuration=Configuration(tasks=[buildLatex], options=[--debug]), expectation=Expectation(file_exists=[output.pdf])) ]

@DanySK DanySK closed this as completed Oct 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant