Skip to content

Latest commit

 

History

History
93 lines (74 loc) · 2.42 KB

README.adoc

File metadata and controls

93 lines (74 loc) · 2.42 KB

Kotlin Code Generator Example

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).

Walkthrough

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:

generated/example/Birds.kt
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>()
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 evaluator.evaluate(ModuleSource.modulePath("config.pkl")) with evaluator.evaluate(ModuleSource.path("path/to/config.pkl")).