Skip to content

Commit 769e80c

Browse files
kevcodezfiliphr
authored andcommitted
Kotlin Example
1 parent f8f998f commit 769e80c

8 files changed

Lines changed: 283 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Currently, the following examples exist:
1616
* _mapstruct-mapping-with-cycles_: Shows how to map object graphs that can contain cycles
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
19+
* _mapstruct-kotlin_: Example on how to use Mapstruct with Kotlin using KAPT (Kotlin Annotation Processing Tool)
1920

2021
## License
2122

mapstruct-kotlin/README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# MapStruct with Kotlin
2+
3+
Example usage of Mapstruct and Kotlin with JDK-8 Bytecode. This is achieved by using the [Kotlin Annotation Processing Tool (KAPT)](https://kotlinlang.org/docs/reference/kapt.html).
4+
5+
## Code
6+
7+
In this example we want to map between a Person (Model) and a PersonDto (DTO).
8+
9+
```kotlin
10+
data class Person(var firstName: String?, var lastName: String?, var phoneNumber: String?, var birthdate: LocalDate?) {
11+
// Necessary for MapStruct
12+
constructor() : this(null, null, null, null)
13+
}
14+
```
15+
16+
```kotlin
17+
data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?) {
18+
// Necessary for MapStruct
19+
constructor() : this(null, null, null, null)
20+
}
21+
```
22+
23+
The MapStruct converter:
24+
25+
```kotlin
26+
@Mapper
27+
interface PersonConverter {
28+
29+
@Mapping(source = "phoneNumber", target = "phone")
30+
fun convertToDto(person: Person) : PersonDto
31+
32+
@InheritInverseConfiguration
33+
fun convertToModel(personDto: PersonDto) : Person
34+
35+
}
36+
```
37+
38+
Usage:
39+
40+
```kotlin
41+
val converter = Mappers.getMapper(PersonConverter::class.java) // or PersonConverterImpl()
42+
43+
val person = Person("Samuel", "Jackson", "0123 334466", LocalDate.of(1948, 12, 21))
44+
45+
val personDto = converter.convertToDto(person)
46+
println(personDto)
47+
48+
val personModel = converter.convertToModel(personDto)
49+
println(personModel)
50+
```
51+
52+
## Further documentation
53+
54+
Using kapt: https://kotlinlang.org/docs/reference/kapt.html
55+
56+
MapStruct: http://mapstruct.org/
57+
58+
Kotlin: https://kotlinlang.org/
59+
60+
## Maven Setup
61+
62+
Add Mapstruct-JDK8 to your dependencies
63+
64+
```xml
65+
<dependency>
66+
<groupId>org.mapstruct</groupId>
67+
<artifactId>mapstruct-jdk8</artifactId>
68+
<version>${mapstruct.version}</version>
69+
</dependency>
70+
```
71+
72+
Add an execution to the kotlin-maven-plugin with the kapt goal and mapstruct as annotation processor:
73+
74+
```xml
75+
76+
<plugin>
77+
<groupId>org.jetbrains.kotlin</groupId>
78+
<artifactId>kotlin-maven-plugin</artifactId>
79+
<version>${kotlin.version}</version>
80+
<executions>
81+
<execution>
82+
<id>kapt</id>
83+
<goals>
84+
<goal>kapt</goal>
85+
</goals>
86+
<configuration>
87+
<sourceDirs>
88+
<sourceDir>src/main/kotlin</sourceDir>
89+
<sourceDir>src/main/java</sourceDir>
90+
</sourceDirs>
91+
<annotationProcessorPaths>
92+
<annotationProcessorPath>
93+
<groupId>org.mapstruct</groupId>
94+
<artifactId>mapstruct-processor</artifactId>
95+
<version>${mapstruct.version}</version>
96+
</annotationProcessorPath>
97+
</annotationProcessorPaths>
98+
</configuration>
99+
</execution>
100+
...
101+
</executions>
102+
....
103+
</plugin>
104+
```

mapstruct-kotlin/pom.xml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.mapstruct.examples.kotlin</groupId>
8+
<artifactId>mapstruct-examples-kotlin</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>Sample application demonstration the use of Mapstruct and Kotlin</name>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
17+
<kotlin.version>1.1.3-2</kotlin.version>
18+
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
19+
<mapstruct.version>1.2.0.CR1</mapstruct.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.mapstruct</groupId>
25+
<artifactId>mapstruct-jdk8</artifactId>
26+
<version>${mapstruct.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.jetbrains.kotlin</groupId>
31+
<artifactId>kotlin-stdlib</artifactId>
32+
<version>${kotlin.version}</version>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<sourceDirectory>src/main/kotlin</sourceDirectory>
38+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
39+
40+
<plugins>
41+
<plugin>
42+
<groupId>org.jetbrains.kotlin</groupId>
43+
<artifactId>kotlin-maven-plugin</artifactId>
44+
<version>${kotlin.version}</version>
45+
<executions>
46+
<execution>
47+
<id>kapt</id>
48+
<goals>
49+
<goal>kapt</goal>
50+
</goals>
51+
<configuration>
52+
<sourceDirs>
53+
<sourceDir>src/main/kotlin</sourceDir>
54+
<sourceDir>src/main/java</sourceDir>
55+
</sourceDirs>
56+
<annotationProcessorPaths>
57+
<annotationProcessorPath>
58+
<groupId>org.mapstruct</groupId>
59+
<artifactId>mapstruct-processor</artifactId>
60+
<version>${mapstruct.version}</version>
61+
</annotationProcessorPath>
62+
</annotationProcessorPaths>
63+
</configuration>
64+
</execution>
65+
<execution>
66+
<id>compile</id>
67+
<phase>compile</phase>
68+
<goals>
69+
<goal>compile</goal>
70+
</goals>
71+
</execution>
72+
<execution>
73+
<id>test-compile</id>
74+
<phase>test-compile</phase>
75+
<goals>
76+
<goal>test-compile</goal>
77+
</goals>
78+
</execution>
79+
</executions>
80+
</plugin>
81+
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-compiler-plugin</artifactId>
85+
<version>3.5.1</version>
86+
<configuration>
87+
<proc>none</proc>
88+
<source>${kotlin.compiler.jvmTarget}</source>
89+
<target>${kotlin.compiler.jvmTarget}</target>
90+
</configuration>
91+
<executions>
92+
<!-- Replacing default-compile as it is treated specially by maven -->
93+
<execution>
94+
<id>default-compile</id>
95+
<phase>none</phase>
96+
</execution>
97+
<!-- Replacing default-testCompile as it is treated specially by maven -->
98+
<execution>
99+
<id>default-testCompile</id>
100+
<phase>none</phase>
101+
</execution>
102+
<execution>
103+
<id>java-compile</id>
104+
<phase>compile</phase>
105+
<goals>
106+
<goal>compile</goal>
107+
</goals>
108+
</execution>
109+
<execution>
110+
<id>java-test-compile</id>
111+
<phase>test-compile</phase>
112+
<goals> <goal>testCompile</goal> </goals>
113+
</execution>
114+
</executions>
115+
</plugin>
116+
</plugins>
117+
</build>
118+
119+
</project>
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.mapstruct.example.kotlin.model
2+
3+
import java.time.LocalDate
4+
5+
data class Person(var firstName: String?, var lastName: String?, var phoneNumber: String?, var birthdate: LocalDate?) {
6+
7+
// Necessary for MapStruct
8+
constructor() : this(null, null, null, null)
9+
10+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
<module>mapstruct-spi-accessor-naming</module>
3939
<module>mapstruct-protobuf3</module>
4040
<module>mapstruct-updatemethods-1</module>
41+
<module>mapstruct-kotlin</module>
4142
</modules>
4243
</project>

0 commit comments

Comments
 (0)