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
78 changes: 78 additions & 0 deletions mapstruct-protobuf3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
and/or other contributors as indicated by the @authors tag. See the
copyright.txt file in the distribution for a full listing of all
contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.mapstruct.examples.spi</groupId>
<artifactId>protobuf-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<properties>
<os.mavenplugin.version>1.4.0.Final</os.mavenplugin.version>
<protobuf.plugin.version>0.5.0</protobuf.plugin.version>
<protobuf.java.version>3.2.0</protobuf.java.version>
<org.mapstruct.version>1.2.0.Beta1</org.mapstruct.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<modules>
<module>spi-impl</module>
<module>usage</module>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
41 changes: 41 additions & 0 deletions mapstruct-protobuf3/spi-impl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
and/or other contributors as indicated by the @authors tag. See the
copyright.txt file in the distribution for a full listing of all
contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.mapstruct.examples.spi</groupId>
<artifactId>protobuf-parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>protobuf-spi-impl</artifactId>
<version>1.0.0</version>

<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.mapstruct.example.protobuf;

import org.mapstruct.ap.spi.DefaultAccessorNamingStrategy;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;

/**
* @author Thomas Kratz
*/
public class ProtobufAccessorNamingStrategy extends DefaultAccessorNamingStrategy {

public static final String PROTOBUF_GENERATED_MESSAGE_V3 = "com.google.protobuf.GeneratedMessageV3";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again something I read on the protbuf website (I have no real experience). It says that GeneratedMessage is an implementation detail and that the builders are implementing Message.Builder or MessageLite.Builder depending on something you pick in the .proto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no clue how to check that with that part of the java api, sorry.

public static final String LIST_SUFFIX = "List";

@Override
public String getElementName(ExecutableElement adderMethod) {

String methodName = super.getElementName(adderMethod);
Element receiver = adderMethod.getEnclosingElement();
if (receiver != null && receiver.getKind() == ElementKind.CLASS) {
TypeElement type = (TypeElement) receiver;
TypeMirror superType = type.getSuperclass();
if (superType != null && PROTOBUF_GENERATED_MESSAGE_V3.equals(superType.toString())) {
methodName += LIST_SUFFIX;
}
}
return methodName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.mapstruct.example.protobuf.ProtobufAccessorNamingStrategy
114 changes: 114 additions & 0 deletions mapstruct-protobuf3/usage/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
and/or other contributors as indicated by the @authors tag. See the
copyright.txt file in the distribution for a full listing of all
contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.mapstruct.examples.spi</groupId>
<artifactId>protobuf-parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>protobuf-usage</artifactId>
<version>1.0.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.java.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this extension needed for protobuf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It allows platform detection so that maven can pull the correct binaries for protoc
${os.detected.classifier}

<artifactId>os-maven-plugin</artifactId>
<version>${os.mavenplugin.version}</version>
</extension>
</extensions>

<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf.plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.2.0:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<!-- Add your SPI implementation. -->
<groupId>org.mapstruct.examples.spi</groupId>
<artifactId>protobuf-spi-impl</artifactId>
<version>1.0.0</version>
<!--
In real life, this should be a fixed version from a different
reactor project and not be part of the same multi-module project.
Maven wouldn't be able to consider that in the build-order calculation
and might pull in the spi-impl from a previous build (especially
tricky in multi-threaded builds).
For Eclipse, there would be similar problems, as it can't reference
build artifacts of workspace projects in the processor classpath but
only jar-files (in this case from the local M2-repo cache).
-->
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.example.mapper;

import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.NullValueCheckStrategy;
import org.mapstruct.ValueMapping;
import org.mapstruct.example.mapper.UserMapper.BuilderFactory;
import org.mapstruct.example.protobuf.Permission;
import org.mapstruct.example.protobuf.User;
import org.mapstruct.example.protobuf.UserProtos.PermissionDTO;
import org.mapstruct.example.protobuf.UserProtos.UserDTO;
import org.mapstruct.factory.Mappers;

/**
* @author Thomas Kratz
*/
@Mapper(uses = BuilderFactory.class,
collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
public interface UserMapper {

UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);


@Mapping(source = "permissions", target = "permissionsList")
UserDTO.Builder map(User user);

@Mapping(source = "permissionsList", target = "permissions")
User map(UserDTO userDTO);

@ValueMapping(source = "UNRECOGNIZED", target = MappingConstants.NULL)
Permission map(PermissionDTO permissionDTO);

PermissionDTO map(Permission perm);


class BuilderFactory {
UserDTO.Builder builder() {
return UserDTO.newBuilder();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mapstruct.example.protobuf;

/**
* @author Thomas Kratz
*/
public enum Permission {

ADMIN,
USER,
NONE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.mapstruct.example.protobuf;

import java.util.ArrayList;
import java.util.List;

/**
* @author Thomas Kratz
*/
public class User {

private String id;
private String email;
private List<Permission> permissions = new ArrayList<>();

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public List<Permission> getPermissions() {
return permissions;
}

public void setPermissions(List<Permission> permissions) {
this.permissions = permissions;
}
}
Loading