https://github.com/blipinsk/data-class-builder
🏗 Automatically generating builders 👷♂️for Kotlin data classes
https://github.com/blipinsk/data-class-builder
annotation-processor annotations data-class kotlin
Last synced: 3 months ago
JSON representation
🏗 Automatically generating builders 👷♂️for Kotlin data classes
- Host: GitHub
- URL: https://github.com/blipinsk/data-class-builder
- Owner: blipinsk
- License: apache-2.0
- Created: 2019-12-08T22:15:05.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-17T09:14:32.000Z (over 5 years ago)
- Last Synced: 2025-04-10T05:02:18.678Z (10 months ago)
- Topics: annotation-processor, annotations, data-class, kotlin
- Language: Kotlin
- Homepage:
- Size: 1.48 MB
- Stars: 18
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README

[  ](https://bintray.com/blipinsk/maven/data-class-builder/_latestVersion)
[  ](https://search.maven.org/search?q=g:com.bartoszlipinski%20AND%20a:data-class-builder)
🏗 Automatically generating builders :construction_worker: for Kotlin `data classes`.
(incremental)
Usage
=====
*For a working usage of this library see the `sample/` module.*
1. (*optional - for **Android** projects*) add to your app's `build.gradle`:
```groovy
android {
kotlinOptions.jvmTarget = '1.8'
}
```
2. Add to `@DataClassBuilder` annotation your `data class`
```kotlin
@DataClassBuilder
data class User(
val name: String = "Anonymous",
val age: Int,
val photoUrl: String? = null
) {
companion object //<-- OPTIONAL companion object
}
```
3. Use the generated builder to create your `data class`:
```kotlin
//1. Java-style builder
val jane = dataClassBuilder(User::class)
.name("Jane")
.age(30)
.build()
```
or
```kotlin
//2. Kotlin DSL builder
val jane = buildDataClass(User::class) {
name = "Jane"
age = 30
}
```
or
```kotlin
//3. Java-style builder through companion (generated if companion is present)
val jane = User.dataClassBuilder()
.name("Jane")
.age(30)
.build()
```
or
```kotlin
//4. Kotlin DSL builder through companion (generated if companion is present)
val jane = User.buildDataClass {
name = "Jane"
age = 30
}
```
Java interop
-----------
For easy interoperation with Java code add this `companion object` to your `data class`:
```kotlin
@DataClassBuilder
data class User(
val name: String = "Anonymous",
val age: Int,
val photoUrl: String? = null
) {
companion object {
@JvmStatic
fun builder() = dataClassBuilder(User::class) //NOT specifying return type explicitly on purpose
}
}
```
(_something like this might be generated by the library in one of the future implementations_)
Including In Your Project
-------------------------
Add in your `build.gradle`:
```xml
repositories {
maven { url 'https://dl.bintray.com/blipinsk/maven/' }
}
dependencies {
kapt "com.bartoszlipinski:data-class-builder-compiler:0.1.0"
implementation "com.bartoszlipinski:data-class-builder:0.1.0"
}
```
Important
---------
The presence of `data class` parameters is verified **in runtime**.
E.g.
1. For `data class`:
```kotlin
@DataClassBuilder
data class User(
val name: String = "Anonymous",
val age: Int // <-- no default value specified
)
```
2. When creating:
```kotlin
buildDataClass(User::class) {
name = "John"
// not setting the necessary `age`
}
```
3. :point_up: this will crash :boom: **in runtime** :point_up:
Developed by
============
* Bartosz Lipiński
License
=======
Copyright 2020 Bartosz Lipiński
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.