https://github.com/getspherelabs/buildable
Buildable is a code generation tool that automates the creation of mappers, factories, and state classes for Kotlin
https://github.com/getspherelabs/buildable
Last synced: about 2 months ago
JSON representation
Buildable is a code generation tool that automates the creation of mappers, factories, and state classes for Kotlin
- Host: GitHub
- URL: https://github.com/getspherelabs/buildable
- Owner: getspherelabs
- License: mit
- Created: 2023-04-19T09:08:34.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-14T03:55:59.000Z (about 2 years ago)
- Last Synced: 2025-03-23T20:05:26.503Z (2 months ago)
- Language: Kotlin
- Homepage:
- Size: 738 KB
- Stars: 16
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - getspherelabs/buildable - Buildable is a code generation tool that automates the creation of mappers, factories, and state classes for Kotlin (Kotlin)
README
Buildable
`@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,
## Build Types
`@Buildable` provides support for three unique build types.
### Factory
Generates factory methods for creating instances of classes with complex constructors or dependencies.
### Mapper
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.
### State (Working in Progress)
## Configuring Gradle
In order to use [KSP (Kotlin Symbol Processing)](https://kotlinlang.org/docs/ksp-quickstart.html) and the [Buildable](https://github.com/getspherelabs/buildable) library into your project, follow the steps below.
### Enable KSP in your module
To enable KSP in your module, add the KSP plugin as shown below to your module's `build.gradle` file:
Kotlin (KTS)
```kotlin
plugins {
id("com.google.devtools.ksp") version "1.8.10-1.0.9"
}
```Groovy
```kotlin
plugins {
id("com.google.devtools.ksp") version "1.8.10-1.0.9"
}
```> **Note**: Make sure your current Kotlin version and [KSP version](https://github.com/google/ksp/releases) is the same.
## Add the Buildable library to your module
Add the dependency below into your **module**'s `build.gradle` file:
```gradle
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")
}
```
### Add source path (KSP)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
kotlin {
sourceSets.configureEach {
kotlin.srcDir("$buildDir/generated/ksp/$name/kotlin/")
}
}
```Android Groovy
```gradle
android {
applicationVariants.all { variant ->
kotlin.sourceSets {
def name = variant.name
getByName(name) {
kotlin.srcDir("build/generated/ksp/$name/kotlin")
}
}
}
}
```Pure Kotlin (KTS)
```gradle
kotlin {
sourceSets.main {
kotlin.srcDir("build/generated/ksp/main/kotlin")
}
sourceSets.test {
kotlin.srcDir("build/generated/ksp/test/kotlin")
}
}
```Pure Kotlin Groovy
```gradle
kotlin {
sourceSets {
main.kotlin.srcDirs += 'build/generated/ksp/main/kotlin'
test.kotlin.srcDirs += 'build/generated/ksp/test/kotlin'
}
}
```## Usage and Examples
> **Note**: In order to use the generated code, you need to execute the `./gradlew kspDebugKotlin` command or perform a project rebuild.
### BuildableFactory
`@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.```kotlin
@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)**:
```kotlin
public enum class CarType {
NEXIA,
MATIZ,
}public fun CarFactory(key: CarType): Car = when (key) {
CarType.NEXIA -> Nexia()
CarType.MATIZ -> Matiz()
}
```### BuildableMapper
`@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.
```kotlin
@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.
```kotlin
fun NotificationEntity.toNotificationDto() = NotificationDto(
name = this.name,
)fun NotificationDto.toNotificationEntity() = NotificationEntity(
name = this.name,
)
```