This is a compiler plugin that generates a builder class for Kotlin's data class
. You can get
and put
properties from the builder, similar to a Map
, and finally build
a new instance.
Currently, it supports Kotlin versions 1.8 and 1.9.
plugins {
id("io.github.windedge.kopybuilder") version "$version"
}
For example, consider the following data class:
@KopyBuilder
data class User(
val name: String,
val email: String?
)
It will generate code like this:
public class UserBuilder: io.github.windedge.copybuilder.CopyBuilder<User> {
override fun `get`(key: String): Any? {
//...
}
override fun put(key: String, `value`: Any?) {
//...
}
override fun build(): User = User(name = ..., email = ... )
}
fun User.toCopyBuilder(): CopyBuilder<User> = ...
fun User.copyBuild(initialize: CopyBuilder<User>.() -> Unit): User { /*...*/ }
You can use it as follows:
val user = User(...)
val builder = user.toCopyBuilder()
builder.apply {
put("name", ...)
put("email", ...)
}
val newUser = builder.build()
// Or build with copyBuild directly
val newUser = user.copyBuild {
put("name", ...)
}
You can even use it in a reflection way, making it possible to cooperate with 3rd party libraries:
import io.github.windedge.copybuilder.CopyBuilderHost
if (CopyBuilderHost::class.isInstance(user)) {
@Suppress("CAST_NEVER_SUCCEEDS")
val host = user as CopyBuilderHost<User>
val newUser = host.copyBuild {
put("name", "Max")
}
}
This project is licensed under the MIT License. Please refer to the LICENSE file for details.