-
Notifications
You must be signed in to change notification settings - Fork 513
Protobuf example #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Protobuf example #25
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a306656
protobuf example
56d5242
Revert "protobuf example"
788d0bd
proto3 example
cd89052
proto3 example test (fails)
ffb9615
proto3 example test (fails)
70987ed
author tags
5e9c3f2
we do not map nulls yet
19aa9e6
we do not map nulls yet
36697dd
NullValueCheckStrategy
d0f6c95
feedback from pr #25
8412efc
cleanup
90b38d1
formatting
21f6073
get methodname correctly
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
34 changes: 34 additions & 0 deletions
34
...spi-impl/src/main/java/org/mapstruct/example/protobuf/ProtobufAccessorNamingStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| 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; | ||
| } | ||
|
|
||
| } | ||
1 change: 1 addition & 0 deletions
1
...spi-impl/src/main/resources/META-INF/services/org.mapstruct.ap.spi.AccessorNamingStrategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| org.mapstruct.example.protobuf.ProtobufAccessorNamingStrategy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this extension needed for protobuf?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| <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> | ||
62 changes: 62 additions & 0 deletions
62
mapstruct-protobuf3/usage/src/main/java/org/mapstruct/example/mapper/UserMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
mapstruct-protobuf3/usage/src/main/java/org/mapstruct/example/protobuf/Permission.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
38 changes: 38 additions & 0 deletions
38
mapstruct-protobuf3/usage/src/main/java/org/mapstruct/example/protobuf/User.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
GeneratedMessageis an implementation detail and that the builders are implementingMessage.BuilderorMessageLite.Builderdepending on something you pick in the.protoThere was a problem hiding this comment.
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.