Kotlin multiplatform builder generator.
How to use?
- Have open source repositories connected to project:
buildscript {
repositories {
gradlePluginPortal()
// ...
}
}
repositories {
mavenCentral()
// ...
}
- Add buildata plugin to your build
plugins {
kotlin("multiplatform") version "1.4.21"
kotlin("jvm") version "1.4.21" // alternatively
// ...
id("io.github.virelion.buildata") version <LIBRARY_VERSION>
}
- Add buildata runtime to your dependencies
kotlin {
// ...
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.virelion.buildata:buildata-runtime:<LIBRARY_VERSION>")
}
}
// ...
}
}
- Add annotation to your
data class
import io.github.virelion.buildata.Buildable
// ...
@Buildable
data class MyDataClass(
val property: String
)
- Codegen will generate a builder you can use:
val myDataClass = MyDataClass::class.build {
property = "Example"
}
val builder = MyDataClass::class.builder()
val myDataClass = builder {
property = "Example"
}.build()
Features
Default values support
Builders will honor default values during the building.
@Buildable
data class MyDataClass(
val propertyWithDefault: String = "DEFAULT",
val property: String
)
val myDataClass = MyDataClass::class.build {
property = "Example"
}
myDataClass.property // returns "Example"
myDataClass.propertyWithDefault // returns "DEFAULT"
Composite builders support
You can mark item as @Buildable
to allow accessing inner builders.
@Buildable
data class InnerItem(
val property: String
)
@Buildable
data class ParentDataClass(
val innerItem: @Buildable InnerItem
)
val parentDataClassBuilder = ParentDataClass::class.builder()
parentDataClassBuilder {
innerItem {
property = "Example"
}
}
val parentDataClass = parentDataClassBuilder.build()
parentDataClass.innerItem.property // returns "Example"
This feature allows for creating complex builder structures for tree like data class
and make mutation easy during tree building process.