Coder Social home page Coder Social logo

mapstruct-kotlin's Introduction

mapstruct-kotlin

โš ๏ธ Since 1.4 MapStruct has support for using constructor arguments when instantiating mapping targets. This also works with Kotlin data classes.

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
}

Usage

First apply kapt plugin

apply plugin: 'kotlin-kapt'

Then add these to your project as dependency

api("com.github.pozo:mapstruct-kotlin:1.3.1.2")
kapt("com.github.pozo:mapstruct-kotlin-processor:1.3.1.2")

Check out the directory example for a basic usage example.

Versioning

For example in case of 1.3.1.1 the first part 1.3.1 is the mapstruct version number and the last digit 1 reserved for future patches.

Project structure

  • mapstruct-kotlin-builder contains only the KotlinBuilder annotation
  • mapstruct-kotlin-processor responsible for generating the builders for the kotlin data classes with the help of a custom DefaultBuilderProvider
  • example responsible for demonstrating this library usage

Build and run the example application

./gradlew -p example clean build

TODO

  • Map with custom types are not working
  • Look over kotlin-builder-annotation project and replace with class generating module (builder-processor)
  • Writing tests
  • Versioning and release process

Licensing

Please see LICENSE file

Contact

Zoltan Polgar - [email protected]

Please do not hesitate to contact me if you have any further questions.

mapstruct-kotlin's People

Contributors

pozo avatar sunragav avatar gmribas avatar driver733 avatar m-kay avatar mtraynham avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.