@Buildable
is a tool designed for Kotlin that facilitates the generation of code for mappers, factories, and state classes. By automating the creation of these components, Buildable can save developers a significant amount of time and effort,
@Buildable
provides support for three unique build types.
Generates factory methods for creating instances of classes with complex constructors or dependencies.
Generates mapping functions that facilitate the conversion of data between different data classes. This is particularly useful in situations where you need to map data from API responses to your application's domain models, or between different layers of your application.
In order to use KSP (Kotlin Symbol Processing) and the Buildable library into your project, follow the steps below.
To enable KSP in your module, add the KSP plugin as shown below to your module's build.gradle
file:
Kotlin (KTS)
plugins {
id("com.google.devtools.ksp") version "1.8.10-1.0.9"
}
Groovy
plugins {
id("com.google.devtools.ksp") version "1.8.10-1.0.9"
}
Note: Make sure your current Kotlin version and KSP version is the same.
Add the dependency below into your module's build.gradle
file:
dependencies {
// For using @BuildableFactory
implementation("io.github.behzodhalil:buildable-factory-core:1.1.0")
ksp("io.github.behzodhalil:buildable-factory:1.1.0")
// For using @BuildableMapper
implementation("io.github.behzodhalil:buildable-mapper-core:1.1.0")
ksp("io.github.behzodhalil:buildable-mapper:1.1.0")
}
To access generated codes from KSP, you need to set up the source path like the below into your module's build.gradle
file:
Android Kotlin (KTS)
kotlin {
sourceSets.configureEach {
kotlin.srcDir("$buildDir/generated/ksp/$name/kotlin/")
}
}
Android Groovy
android {
applicationVariants.all { variant ->
kotlin.sourceSets {
def name = variant.name
getByName(name) {
kotlin.srcDir("build/generated/ksp/$name/kotlin")
}
}
}
}
Pure Kotlin (KTS)
kotlin {
sourceSets.main {
kotlin.srcDir("build/generated/ksp/main/kotlin")
}
sourceSets.test {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
Pure Kotlin Groovy
kotlin {
sourceSets {
main.kotlin.srcDirs += 'build/generated/ksp/main/kotlin'
test.kotlin.srcDirs += 'build/generated/ksp/test/kotlin'
}
}
Note: In order to use the generated code, you need to execute the
./gradlew kspDebugKotlin
command or perform a project rebuild.
@BuildableFactory
annotation aims to simplify code generation associated with the Factory Method pattern. This simplifies the management of object creation, ultimately enhancing maintainability and code readability.
@BuildableComponent
annotation is utilized to specify the type for creating a factory pattern implementation.
@BuildableFactory
interface Car {
fun drive()
}
@BuildableComponent
class Nexia : Car {
override fun drive() {
println("Nexia is driving...")
}
}
@BuildableComponent
class Matiz : Car {
override fun drive() {
println("Matiz is driving...")
}
The example codes generate CarFactory
and CarTypes
for easier object management.
CarFactory (generated):
public enum class CarType {
NEXIA,
MATIZ,
}
public fun CarFactory(key: CarType): Car = when (key) {
CarType.NEXIA -> Nexia()
CarType.MATIZ -> Matiz()
}
@BuildableMapper
annotation is designed to streamline code generation for mapping one class to another, making object creation management more efficient and improving overall maintainability and readability within the codebase.
@BuildableMapper(
from = [NotificationDto::class],
to = [NotificationEntity::class]
)
data class NotificationDto(
val name: String
)
data class NotificationEntity(
val name: String
)
The example codes generate NotificationDtoBuildableMapperExtensions.kt
for easier object management.
fun NotificationEntity.toNotificationDto() = NotificationDto(
name = this.name,
)
fun NotificationDto.toNotificationEntity() = NotificationEntity(
name = this.name,
)