Skip to content

Commit db0be77

Browse files
eiswindfiliphr
authored andcommitted
#14 Protobuf example
1 parent 937c2fc commit db0be77

File tree

11 files changed

+465
-0
lines changed

11 files changed

+465
-0
lines changed

mapstruct-protobuf3/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
5+
and/or other contributors as indicated by the @authors tag. See the
6+
copyright.txt file in the distribution for a full listing of all
7+
contributors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.mapstruct.examples.spi</groupId>
27+
<artifactId>protobuf-parent</artifactId>
28+
<version>1.0.0</version>
29+
<packaging>pom</packaging>
30+
31+
<properties>
32+
<os.mavenplugin.version>1.4.0.Final</os.mavenplugin.version>
33+
<protobuf.plugin.version>0.5.0</protobuf.plugin.version>
34+
<protobuf.java.version>3.2.0</protobuf.java.version>
35+
<org.mapstruct.version>1.2.0.Beta1</org.mapstruct.version>
36+
<maven.compiler.source>1.8</maven.compiler.source>
37+
<maven.compiler.target>1.8</maven.compiler.target>
38+
</properties>
39+
40+
<modules>
41+
<module>spi-impl</module>
42+
<module>usage</module>
43+
</modules>
44+
45+
<dependencyManagement>
46+
<dependencies>
47+
<dependency>
48+
<groupId>org.mapstruct</groupId>
49+
<artifactId>mapstruct-jdk8</artifactId>
50+
<version>${org.mapstruct.version}</version>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.mapstruct</groupId>
55+
<artifactId>mapstruct-processor</artifactId>
56+
<version>${org.mapstruct.version}</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>junit</groupId>
61+
<artifactId>junit</artifactId>
62+
<version>4.12</version>
63+
</dependency>
64+
</dependencies>
65+
</dependencyManagement>
66+
67+
<build>
68+
<pluginManagement>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-compiler-plugin</artifactId>
73+
<version>3.5.1</version>
74+
</plugin>
75+
</plugins>
76+
</pluginManagement>
77+
</build>
78+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
5+
and/or other contributors as indicated by the @authors tag. See the
6+
copyright.txt file in the distribution for a full listing of all
7+
contributors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<groupId>org.mapstruct.examples.spi</groupId>
28+
<artifactId>protobuf-parent</artifactId>
29+
<version>1.0.0</version>
30+
</parent>
31+
32+
<artifactId>protobuf-spi-impl</artifactId>
33+
<version>1.0.0</version>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.mapstruct</groupId>
38+
<artifactId>mapstruct-processor</artifactId>
39+
</dependency>
40+
</dependencies>
41+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.mapstruct.example.protobuf;
2+
3+
import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy;
4+
5+
import javax.lang.model.element.Element;
6+
import javax.lang.model.element.ElementKind;
7+
import javax.lang.model.element.ExecutableElement;
8+
import javax.lang.model.element.TypeElement;
9+
import javax.lang.model.type.TypeMirror;
10+
11+
/**
12+
* @author Thomas Kratz
13+
*/
14+
public class ProtobufAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
15+
16+
public static final String PROTOBUF_GENERATED_MESSAGE_V3 = "com.google.protobuf.GeneratedMessageV3";
17+
public static final String LIST_SUFFIX = "List";
18+
19+
@Override
20+
public String getElementName(ExecutableElement adderMethod) {
21+
22+
String methodName = super.getElementName(adderMethod);
23+
Element receiver = adderMethod.getEnclosingElement();
24+
if (receiver != null && receiver.getKind() == ElementKind.CLASS) {
25+
TypeElement type = (TypeElement) receiver;
26+
TypeMirror superType = type.getSuperclass();
27+
if (superType != null && PROTOBUF_GENERATED_MESSAGE_V3.equals(superType.toString())) {
28+
methodName += LIST_SUFFIX;
29+
}
30+
}
31+
return methodName;
32+
}
33+
34+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.mapstruct.example.protobuf.ProtobufAccessorNamingStrategy

mapstruct-protobuf3/usage/pom.xml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
5+
and/or other contributors as indicated by the @authors tag. See the
6+
copyright.txt file in the distribution for a full listing of all
7+
contributors.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
21+
-->
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<groupId>org.mapstruct.examples.spi</groupId>
28+
<artifactId>protobuf-parent</artifactId>
29+
<version>1.0.0</version>
30+
</parent>
31+
32+
<artifactId>protobuf-usage</artifactId>
33+
<version>1.0.0-SNAPSHOT</version>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.mapstruct</groupId>
38+
<artifactId>mapstruct-jdk8</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.google.protobuf</groupId>
42+
<artifactId>protobuf-java</artifactId>
43+
<version>${protobuf.java.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<version>4.12</version>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<extensions>
55+
<extension>
56+
<groupId>kr.motd.maven</groupId>
57+
<artifactId>os-maven-plugin</artifactId>
58+
<version>${os.mavenplugin.version}</version>
59+
</extension>
60+
</extensions>
61+
62+
<plugins>
63+
<plugin>
64+
<groupId>org.xolstice.maven.plugins</groupId>
65+
<artifactId>protobuf-maven-plugin</artifactId>
66+
<version>${protobuf.plugin.version}</version>
67+
<executions>
68+
<execution>
69+
<phase>generate-sources</phase>
70+
<goals>
71+
<goal>compile</goal>
72+
<goal>compile-custom</goal>
73+
</goals>
74+
<configuration>
75+
<protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}
76+
</protocArtifact>
77+
<pluginId>grpc-java</pluginId>
78+
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.2.0:exe:${os.detected.classifier}
79+
</pluginArtifact>
80+
</configuration>
81+
</execution>
82+
</executions>
83+
</plugin>
84+
<plugin>
85+
<artifactId>maven-compiler-plugin</artifactId>
86+
<configuration>
87+
<annotationProcessorPaths>
88+
<path>
89+
<groupId>org.mapstruct</groupId>
90+
<artifactId>mapstruct-processor</artifactId>
91+
<version>${org.mapstruct.version}</version>
92+
</path>
93+
<path>
94+
<!-- Add your SPI implementation. -->
95+
<groupId>org.mapstruct.examples.spi</groupId>
96+
<artifactId>protobuf-spi-impl</artifactId>
97+
<version>1.0.0</version>
98+
<!--
99+
In real life, this should be a fixed version from a different
100+
reactor project and not be part of the same multi-module project.
101+
Maven wouldn't be able to consider that in the build-order calculation
102+
and might pull in the spi-impl from a previous build (especially
103+
tricky in multi-threaded builds).
104+
For Eclipse, there would be similar problems, as it can't reference
105+
build artifacts of workspace projects in the processor classpath but
106+
only jar-files (in this case from the local M2-repo cache).
107+
-->
108+
</path>
109+
</annotationProcessorPaths>
110+
</configuration>
111+
</plugin>
112+
</plugins>
113+
</build>
114+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
* <p>
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
* <p>
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* <p>
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.example.mapper;
20+
21+
import org.mapstruct.CollectionMappingStrategy;
22+
import org.mapstruct.Mapper;
23+
import org.mapstruct.Mapping;
24+
import org.mapstruct.MappingConstants;
25+
import org.mapstruct.NullValueCheckStrategy;
26+
import org.mapstruct.ValueMapping;
27+
import org.mapstruct.example.mapper.UserMapper.BuilderFactory;
28+
import org.mapstruct.example.protobuf.Permission;
29+
import org.mapstruct.example.protobuf.User;
30+
import org.mapstruct.example.protobuf.UserProtos.PermissionDTO;
31+
import org.mapstruct.example.protobuf.UserProtos.UserDTO;
32+
import org.mapstruct.factory.Mappers;
33+
34+
/**
35+
* @author Thomas Kratz
36+
*/
37+
@Mapper(uses = BuilderFactory.class,
38+
collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
39+
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
40+
public interface UserMapper {
41+
42+
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
43+
44+
45+
@Mapping(source = "permissions", target = "permissionsList")
46+
UserDTO.Builder map(User user);
47+
48+
@Mapping(source = "permissionsList", target = "permissions")
49+
User map(UserDTO userDTO);
50+
51+
@ValueMapping(source = "UNRECOGNIZED", target = MappingConstants.NULL)
52+
Permission map(PermissionDTO permissionDTO);
53+
54+
PermissionDTO map(Permission perm);
55+
56+
57+
class BuilderFactory {
58+
UserDTO.Builder builder() {
59+
return UserDTO.newBuilder();
60+
}
61+
}
62+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.mapstruct.example.protobuf;
2+
3+
/**
4+
* @author Thomas Kratz
5+
*/
6+
public enum Permission {
7+
8+
ADMIN,
9+
USER,
10+
NONE
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.mapstruct.example.protobuf;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author Thomas Kratz
8+
*/
9+
public class User {
10+
11+
private String id;
12+
private String email;
13+
private List<Permission> permissions = new ArrayList<>();
14+
15+
public String getId() {
16+
return id;
17+
}
18+
19+
public void setId(String id) {
20+
this.id = id;
21+
}
22+
23+
public String getEmail() {
24+
return email;
25+
}
26+
27+
public void setEmail(String email) {
28+
this.email = email;
29+
}
30+
31+
public List<Permission> getPermissions() {
32+
return permissions;
33+
}
34+
35+
public void setPermissions(List<Permission> permissions) {
36+
this.permissions = permissions;
37+
}
38+
}

0 commit comments

Comments
 (0)