Since mapstruct 1.3.0.Beta2 it's possible to use builders for immutable classes. According to the documentation you can implement your custom builder provider logic. This project take advantage of this and provide a custom BuilderProvider
for kotlin data classes.
So instead of this (source)
data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?) {
// Necessary for MapStruct
constructor() : this(null, null, null, null)
}
We can do this
@KotlinBuilder
data class PersonDto(val firstName: String, val lastName: String, val phone: String, val birthdate: LocalDate)
With a mapper
@Mapper
interface PersonMapper {
fun map(person: Person): PersonDto
}
Please keep in your mind that this repository still a proof of concept, and absolutely not ready for production. A contribution, idea, discussion is always welcomed
A version which is using the mapstruct 1.3.0.Beta2 is available on jitpack.io.
First of all add maven { url 'https://jitpack.io' }
to your repositories, and then add these to your dependencies
api("com.github.pozo.mapstruct-kotlin:mapstruct-kotlin:1.3.0.Beta2")
kapt("com.github.pozo.mapstruct-kotlin:mapstruct-kotlin-processor:1.3.0.Beta2")
annotation
contains only theKotlinBuilder
annotationbuilder-processor
responsible for generating the builders for the kotlin data classesprocessor
responsible for extending theDefaultBuilderProvider
functionality according to the documentationtest
responsible for demonstrating this library usage
The building order is important since the processor
project depends on builder-processor
and mapstruct.
./gradlew clean build publishToMavenLocal
./gradlew -p example clean build
Map with custom types are not workingLook over kotlin-builder-annotation project and replace with class generating module (builder-processor)- Writing tests
- Versioning and release process
Please see LICENSE file
Zoltan Polgar - [email protected]
Please do not hesitate to contact me if you have any further questions.