项目作者: entur

项目描述 :
Protobuf accessor naming strategy for Mapstruct
高级语言: Java
项目地址: git://github.com/entur/mapstruct-spi-protobuf.git
创建时间: 2018-12-03T14:18:44Z
项目社区:https://github.com/entur/mapstruct-spi-protobuf

开源协议:European Union Public License 1.2

下载


Mapstruct SPI implementation for protocol buffers CircleCI

This naming strategy helps mapstruct generate working mapping code between your domain classes
and protobuf classes. Both fullblown Java protobuf
and protolite classes suported.

Requires on mapstruct 1.4.0+.

ProtobufAccessorNamingStrategy

Extends DefaultProtobufAccessorNamingStrategy and provides necessary information to map all fields automatically
except

  • oneof

which require manual mapping.

ProtobufEnumMappingStrategy

Implements EnumMappingStrategy and provides complete enum constant mappings if you follow Googles style guide for
enums https://developers.google.com/protocol-buffers/docs/style#enums

If needed you can specify a different postfix for the 0 value enum by passing in mapstructSpi.enumPostfixOverrides as
a compilerArg in the format of:

-AmapstructSpi.enumPostfixOverrides=com.package.root.a=POSTFIX_1,com.package.root.b=POSTFIX_2

Otherwise, this will default to UNSPECIFIED as per the Google style guide.

  1. <plugin>
  2. <artifactId>maven-compiler-plugin</artifactId>
  3. <configuration>
  4. <annotationProcessorPaths>
  5. <path>
  6. <groupId>no.entur.mapstruct.spi</groupId>
  7. <artifactId>protobuf-spi-impl</artifactId>
  8. <version>LATEST.VERSION</version>
  9. </path>
  10. </annotationProcessorPaths>
  11. <compilerArgs>
  12. <arg>-AmapstructSpi.enumPostfixOverrides=com.company.name=INVALID</arg>
  13. </compilerArgs>
  14. </configuration>
  15. </plugin>

Support - Mapping functions:

Standard mapping functions between often used proto types and java types:

  • Timestamp <-> Instant
  • Duration <-> Duration
  • Date <-> LocalDate
  • TimeOfDay <-> LocalTime
  • byte[] <-> ByteString

See protobuf-support-standard and/or protobuf-support-lite folders for a
ready-to-use mapstruct mapper.

Usage

See example project

NB: Make sure you add collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED to your mapping interfaces
as protobuf stubs use the builder pattern.

  1. @Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
  2. public interface ... {

Maven

NB: Make sure you use the same version of mapstruct both in the annotation process and the general dependency.
mapstruct-spi-protobuf generally depends on the latest released version of mapstruct.

Add the following section to you maven-compiler-plugin plugin configuration:

  1. <annotationProcessorPaths>
  2. <path>
  3. <groupId>no.entur.mapstruct.spi</groupId>
  4. <artifactId>protobuf-spi-impl</artifactId> <!-- Make sure mapstruct version here is the same as below -->
  5. <version>LATEST.VERSION</version>
  6. </path>
  7. </annotationProcessorPaths>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.mapstruct</groupId>
  11. <artifactId>mapstruct</artifactId>
  12. <version>${org.mapstruct.version}</version>
  13. </dependency>
  14. </dependencies>

Complete example:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <configuration>
  5. <source>1.8</source>
  6. <target>1.8</target>
  7. <annotationProcessorPaths>
  8. <path>
  9. <groupId>no.entur.mapstruct.spi</groupId>
  10. <artifactId>protobuf-spi-impl</artifactId>
  11. <version>LATEST.VERSION</version>
  12. </path>
  13. </annotationProcessorPaths>
  14. </configuration>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.mapstruct</groupId>
  18. <artifactId>mapstruct</artifactId>
  19. <version>${org.mapstruct.version}</version>
  20. </dependency>
  21. </dependencies>
  22. </plugin>

Gradle

Note: See Maven setup regarding using the same version of mapstruct both in the annotation processor and the general
dependency.

  1. implementation"org.mapstruct:mapstruct:${mapstructVersion}"
  2. annotationProcessor"org.mapstruct:mapstruct-processor:${mapstructVersion}"
  3. annotationProcessor"no.entur.mapstruct.spi:protobuf-spi-impl:LATEST.VERSION"

More information:

http://mapstruct.org/documentation/stable/reference/html/index.html#using-spi