Fix java/kotlin usage examples (#372)

Co-authored-by: Fruxz <28064149+TheFruxz@users.noreply.github.com>
This commit is contained in:
Daniel Chao
2024-03-28 07:57:28 -07:00
committed by GitHub
parent c28e478d5a
commit 759d4806c0
3 changed files with 6 additions and 4 deletions

View File

@@ -10,12 +10,13 @@ sourceSets {
java {
srcDir(file("modules/pkl-core/examples"))
srcDir(file("modules/pkl-config-java/examples"))
srcDir(file("modules/java-binding/examples"))
}
val kotlin = project.extensions
.getByType<KotlinJvmProjectExtension>()
.sourceSets[name]
.kotlin
kotlin.srcDir(file("modules/pkl-config-kotlin/examples"))
kotlin.srcDir(file("modules/kotlin-binding/examples"))
}
}

View File

@@ -13,7 +13,7 @@ public class JavaConfigExample {
Config config;
try (var evaluator = ConfigEvaluator.preconfigured()) { // <1>
config = evaluator.evaluate(
ModuleSource.text("pigeon { age = 5; diet = \"Seeds\" }")); // <2>
ModuleSource.text("pigeon { age = 5; diet = new Listing { \"Seeds\" } }")); // <2>
}
var pigeon = config.get("pigeon"); // <3>
var age = pigeon.get("age").as(int.class); // <4>

View File

@@ -3,6 +3,7 @@
import org.pkl.config.java.ConfigEvaluator
import org.pkl.config.kotlin.forKotlin
import org.pkl.config.kotlin.to
import org.pkl.core.ModuleSource
import org.junit.jupiter.api.Test
// the pkl/pkl-examples repo has a similar example
@@ -12,7 +13,7 @@ class KotlinConfigExample {
// tag::usage[]
val evaluator = ConfigEvaluator.preconfigured().forKotlin() // <1>
val config = evaluator.use { // <2>
it.evaluateText("""pigeon { age = 5; diet = "Seeds" }""")
it.evaluate(ModuleSource.text("""pigeon { age = 5; diet = new Listing { "Seeds" } }"""))
}
val pigeon = config["pigeon"] // <3>
val age = pigeon["age"].to<Int>() // <4>
@@ -25,7 +26,7 @@ class KotlinConfigExample {
// tag::nullable[]
val evaluator = ConfigEvaluator.preconfigured().forKotlin()
val config = evaluator.use {
it.evaluateText("name = null") // <1>
it.evaluate(ModuleSource.text("name = null")) // <1>
}
val name = config["name"].to<String?>() // <2>
// end::nullable[]