Demonstrates how to use the Kotlin code generator and consume configuration as statically typed Kotlin objects.
To run this example, type ../gradlew build
(requires JDK 17 or higher).
Let’s generate Kotlin classes for Pkl module xref:src/main/resources/Birds.pkl.
Run the Kotlin code generator:
$ java -cp <classpath-for-pkl-codegen-kotlin> \
-jar pkl-codegen-kotlin.jar \
-o generated \
src/main/resources/Birds.pkl
Note
|
build.gradle.kts demonstrates how to generate code with the Gradle plugin. |
The following code is generated:
data class Birds(
/**
* The birds that we know about.
*/
val birds: Map<String, Bird>
) {
data class Bird(
/**
* The name of the bird
*/
val name: String,
/**
* The bird's features
*/
val features: Features
)
data class Features(
/**
* Can this bird mimick other sounds?
*/
val voiceMimickry: Boolean,
/**
* Can this bird fly?
*/
val flies: Boolean,
/**
* Can this bird swim?
*/
val swims: Boolean
)
}
For Pkl classes that aren’t open
for extension and don’t extend a superclass, Kotlin data classes are generated.
For the remaining classes, regular Kotlin classes with component-wise implementations of equals()
/hashCode()
/toString()
and a copy()
method are generated.
We are now ready to consume Pkl config as statically typed Kotlin objects:
val evaluator = ConfigEvaluator.preconfigured().forKotlin()
val config = evaluator.evaluate(ModuleSource.modulePath("config.pkl"))
val birds = config.to<Birds>()
For the full source code, see src/main/kotlin/example/KotlinCodeGeneratorExample.kt.
Note
|
We have omitted some details here, such as how to compile the generated Kotlin classes, and how to make them known to the IDE. The Gradle plugin automatically takes care of these details. This example reads Pkl files from the class path.
To read Pkl files from the local file system,
replace |