mirror of
https://github.com/apple/pkl.git
synced 2026-04-21 07:51:35 +02: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 {
|
java {
|
||||||
srcDir(file("modules/pkl-core/examples"))
|
srcDir(file("modules/pkl-core/examples"))
|
||||||
srcDir(file("modules/pkl-config-java/examples"))
|
srcDir(file("modules/pkl-config-java/examples"))
|
||||||
|
srcDir(file("modules/java-binding/examples"))
|
||||||
}
|
}
|
||||||
val kotlin = project.extensions
|
val kotlin = project.extensions
|
||||||
.getByType<KotlinJvmProjectExtension>()
|
.getByType<KotlinJvmProjectExtension>()
|
||||||
.sourceSets[name]
|
.sourceSets[name]
|
||||||
.kotlin
|
.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;
|
Config config;
|
||||||
try (var evaluator = ConfigEvaluator.preconfigured()) { // <1>
|
try (var evaluator = ConfigEvaluator.preconfigured()) { // <1>
|
||||||
config = evaluator.evaluate(
|
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 pigeon = config.get("pigeon"); // <3>
|
||||||
var age = pigeon.get("age").as(int.class); // <4>
|
var age = pigeon.get("age").as(int.class); // <4>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import org.pkl.config.java.ConfigEvaluator
|
import org.pkl.config.java.ConfigEvaluator
|
||||||
import org.pkl.config.kotlin.forKotlin
|
import org.pkl.config.kotlin.forKotlin
|
||||||
import org.pkl.config.kotlin.to
|
import org.pkl.config.kotlin.to
|
||||||
|
import org.pkl.core.ModuleSource
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
// the pkl/pkl-examples repo has a similar example
|
// the pkl/pkl-examples repo has a similar example
|
||||||
@@ -12,7 +13,7 @@ class KotlinConfigExample {
|
|||||||
// tag::usage[]
|
// tag::usage[]
|
||||||
val evaluator = ConfigEvaluator.preconfigured().forKotlin() // <1>
|
val evaluator = ConfigEvaluator.preconfigured().forKotlin() // <1>
|
||||||
val config = evaluator.use { // <2>
|
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 pigeon = config["pigeon"] // <3>
|
||||||
val age = pigeon["age"].to<Int>() // <4>
|
val age = pigeon["age"].to<Int>() // <4>
|
||||||
@@ -25,7 +26,7 @@ class KotlinConfigExample {
|
|||||||
// tag::nullable[]
|
// tag::nullable[]
|
||||||
val evaluator = ConfigEvaluator.preconfigured().forKotlin()
|
val evaluator = ConfigEvaluator.preconfigured().forKotlin()
|
||||||
val config = evaluator.use {
|
val config = evaluator.use {
|
||||||
it.evaluateText("name = null") // <1>
|
it.evaluate(ModuleSource.text("name = null")) // <1>
|
||||||
}
|
}
|
||||||
val name = config["name"].to<String?>() // <2>
|
val name = config["name"].to<String?>() // <2>
|
||||||
// end::nullable[]
|
// end::nullable[]
|
||||||
|
|||||||
Reference in New Issue
Block a user