mirror of
https://github.com/apple/pkl.git
synced 2026-01-18 17:37:07 +01:00
Fix java/kotlin usage examples (#372)
Co-authored-by: Fruxz <28064149+TheFruxz@users.noreply.github.com>
This commit is contained in:
@@ -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"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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[]
|
||||
|
||||
Reference in New Issue
Block a user