A Kotlin KSP (Kotlin Symbol Processing) annotation processor for automatic Draft DSL Builder code generation.
- @Draftable Annotation: Mark data classes to automatically generate Draft builders
- DSL-style Builders: Fluent, type-safe builder pattern using Kotlin DSL syntax
- Dynamic Property Setting: Set properties by name using the
set(propertyName, value)method - Type Safety: Generated code maintains full type safety
In your build.gradle.kts:
plugins {
kotlin("jvm") version "2.3.0"
id("com.google.devtools.ksp") version "2.3.4"
}
dependencies {
implementation("com.bangbang93.kdraft:kdraft-annotations:1.0.0")
ksp("com.bangbang93.kdraft:kdraft-processor:1.0.0")
}
@Draftable
data class User(
val id: Int,
val name: String,
val email: String,
val age: Int
)val user = userDraft {
id = 1
name = "John Doe"
email = "somebody@example.com"
set("age", 30)
}For the User class above, the processor generates:
class UserDraft {
var id: Int = 0
var name: String = ""
var email: String = ""
var age: Int = 0
fun set(propertyName: String, value: Any?) {
when (propertyName) {
"id" -> id = value as Int
"name" -> name = value as String
"email" -> email = value as String
"age" -> age = value as Int
else -> throw IllegalArgumentException("Unknown property: $propertyName")
}
}
fun build(): User = User(id, name, email, age)
}
fun userDraft(block: UserDraft.() -> Unit): User {
return UserDraft().apply(block).build()
}./gradlew build./gradlew :kdraft-sample:run- Annotate a data class with
@Draftable - KSP processor scans for annotated classes during compilation
- For each annotated class, it generates:
- A Draft class with mutable properties
- A DSL function for convenient instance construction
- A
set()method for dynamic property assignment
- Generated code is placed in
build/generated/ksp/main/kotlin/
The processor supports the following types with default values:
| Type | Default Value |
|---|---|
| Int | 0 |
| Long | 0L |
| Short | 0 |
| Byte | 0 |
| Float | 0.0f |
| Double | 0.0 |
| Boolean | false |
| Char | '\u0000' |
| String | "" |
| List | emptyList() |
| Set | emptySet() |
| Map | emptyMap() |
| Nullable types | null |
MIT License