Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Currently, the following examples exist:
* _mapstruct-spi-accessor-naming_: Example on how to use the Service Provider Interface (SPI) for a custom accessor naming strategy.
* _mapstruct-protobuf3_: Example on how to use protobuf3 with MapStruct
* _mapstruct-kotlin_: Example on how to use MapStruct with Kotlin using KAPT (Kotlin Annotation Processing Tool)
* _mapstruct-kotlin-gradle_: Example on how to use MapStruct with Kotlin and Gradle Kotlin DSL using KAPT
* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA)
* _mapstruct-lookup-entity-with-composed-key_: Shows how an object with composite key can be read from the database in a mapping method.

Expand Down
31 changes: 31 additions & 0 deletions mapstruct-kotlin-gradle/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.2.71"
kotlin("kapt") version "1.2.71"
}

group = "org.mapstruct.examples"
version = "1.0.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
compile(kotlin("stdlib-jdk8"))
compile("org.mapstruct:mapstruct-jdk8:1.2.0.Final")
kapt("org.mapstruct:mapstruct-processor:1.2.0.Final")

testCompile("org.junit.jupiter:junit-jupiter-api:5.3.1")
testCompile("org.junit.jupiter:junit-jupiter-engine:5.3.1")
testCompile("org.assertj:assertj-core:3.11.1")
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}

val test by tasks.getting(Test::class) {
useJUnitPlatform { }
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
172 changes: 172 additions & 0 deletions mapstruct-kotlin-gradle/gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions mapstruct-kotlin-gradle/gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions mapstruct-kotlin-gradle/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'mapstruct-kotlin-gradle'

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.mapstruct.example.kotlin

import org.mapstruct.example.kotlin.converter.PersonConverter
import org.mapstruct.example.kotlin.model.Person
import org.mapstruct.factory.Mappers
import java.time.LocalDate

fun main(args: Array<String>) {

val converter = Mappers.getMapper(PersonConverter::class.java) // or PersonConverterImpl()

val person = Person("Samuel", "Jackson", "0123 334466", LocalDate.of(1948, 12, 21))

val personDto = converter.convertToDto(person)
println(personDto)

val personModel = converter.convertToModel(personDto)
println(personModel)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.mapstruct.example.kotlin.converter

import org.mapstruct.InheritInverseConfiguration
import org.mapstruct.Mapper
import org.mapstruct.Mapping
import org.mapstruct.example.kotlin.dto.PersonDto
import org.mapstruct.example.kotlin.model.Person

@Mapper
interface PersonConverter {

@Mapping(source = "phoneNumber", target = "phone")
fun convertToDto(person: Person): PersonDto

@InheritInverseConfiguration
fun convertToModel(personDto: PersonDto): Person

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.mapstruct.example.kotlin.dto

import java.time.LocalDate

data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?) {

// Necessary for MapStruct
constructor() : this(null, null, null, null)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.mapstruct.example.kotlin.model

import java.time.LocalDate

data class Person(var firstName: String?, var lastName: String?, var phoneNumber: String?, var birthdate: LocalDate?) {

// Necessary for MapStruct
constructor() : this(null, null, null, null)

}
Loading