Skip to content

Commit 0919158

Browse files
authored
Merge pull request #48 from johanvergeer/master
Mapstruct example for Kotlin with Gradle Kotlin DSL
2 parents d6483c5 + 537b6cd commit 0919158

12 files changed

Lines changed: 393 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Currently, the following examples exist:
1717
* _mapstruct-spi-accessor-naming_: Example on how to use the Service Provider Interface (SPI) for a custom accessor naming strategy.
1818
* _mapstruct-protobuf3_: Example on how to use protobuf3 with MapStruct
1919
* _mapstruct-kotlin_: Example on how to use MapStruct with Kotlin using KAPT (Kotlin Annotation Processing Tool)
20+
* _mapstruct-kotlin-gradle_: Example on how to use MapStruct with Kotlin and Gradle Kotlin DSL using KAPT
2021
* _mapstruct-jpa-parent-child_: Example on how to use @Context in relation to parent / child relations in JPA)
2122
* _mapstruct-lookup-entity-with-composed-key_: Shows how an object with composite key can be read from the database in a mapping method.
2223

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
3+
plugins {
4+
kotlin("jvm") version "1.2.71"
5+
kotlin("kapt") version "1.2.71"
6+
}
7+
8+
group = "org.mapstruct.examples"
9+
version = "1.0.0-SNAPSHOT"
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
compile(kotlin("stdlib-jdk8"))
17+
compile("org.mapstruct:mapstruct-jdk8:1.2.0.Final")
18+
kapt("org.mapstruct:mapstruct-processor:1.2.0.Final")
19+
20+
testCompile("org.junit.jupiter:junit-jupiter-api:5.3.1")
21+
testCompile("org.junit.jupiter:junit-jupiter-engine:5.3.1")
22+
testCompile("org.assertj:assertj-core:3.11.1")
23+
}
24+
25+
tasks.withType<KotlinCompile> {
26+
kotlinOptions.jvmTarget = "1.8"
27+
}
28+
29+
val test by tasks.getting(Test::class) {
30+
useJUnitPlatform { }
31+
}
53.1 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

mapstruct-kotlin-gradle/gradlew

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mapstruct-kotlin-gradle/gradlew.bat

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = 'mapstruct-kotlin-gradle'
2+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.mapstruct.example.kotlin
2+
3+
import org.mapstruct.example.kotlin.converter.PersonConverter
4+
import org.mapstruct.example.kotlin.model.Person
5+
import org.mapstruct.factory.Mappers
6+
import java.time.LocalDate
7+
8+
fun main(args: Array<String>) {
9+
10+
val converter = Mappers.getMapper(PersonConverter::class.java) // or PersonConverterImpl()
11+
12+
val person = Person("Samuel", "Jackson", "0123 334466", LocalDate.of(1948, 12, 21))
13+
14+
val personDto = converter.convertToDto(person)
15+
println(personDto)
16+
17+
val personModel = converter.convertToModel(personDto)
18+
println(personModel)
19+
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.mapstruct.example.kotlin.converter
2+
3+
import org.mapstruct.InheritInverseConfiguration
4+
import org.mapstruct.Mapper
5+
import org.mapstruct.Mapping
6+
import org.mapstruct.example.kotlin.dto.PersonDto
7+
import org.mapstruct.example.kotlin.model.Person
8+
9+
@Mapper
10+
interface PersonConverter {
11+
12+
@Mapping(source = "phoneNumber", target = "phone")
13+
fun convertToDto(person: Person): PersonDto
14+
15+
@InheritInverseConfiguration
16+
fun convertToModel(personDto: PersonDto): Person
17+
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.mapstruct.example.kotlin.dto
2+
3+
import java.time.LocalDate
4+
5+
data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?) {
6+
7+
// Necessary for MapStruct
8+
constructor() : this(null, null, null, null)
9+
10+
}

0 commit comments

Comments
 (0)