- Notifications
You must be signed in to change notification settings - Fork22
Protobuf accessor naming strategy for Mapstruct
License
entur/mapstruct-spi-protobuf
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This naming strategy helpsmapstruct generate working mapping code between your domain classesand protobuf classes. Bothfullblown Java protobufandprotolite classes suported.
Requires on mapstruct 1.4.0+.
ExtendsDefaultProtobufAccessorNamingStrategy and provides necessary information to map all fields automatically *except*
- oneof
which require manual mapping.
ImplementsEnumMappingStrategy and provides complete enum constant mappings if you follow Googles style guide forenumshttps://developers.google.com/protocol-buffers/docs/style#enums
If needed you can specify a different postfix for the 0 value enum by passing inmapstructSpi.enumPostfixOverrides asa compilerArg in the format of:
-AmapstructSpi.enumPostfixOverrides=com.package.root.a=POSTFIX_1,com.package.root.b=POSTFIX_2
Otherwise, this will default toUNSPECIFIED as per the Google style guide.
<plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>no.entur.mapstruct.spi</groupId> <artifactId>protobuf-spi-impl</artifactId> <version>LATEST.VERSION</version> </path> </annotationProcessorPaths> <compilerArgs> <arg>-AmapstructSpi.enumPostfixOverrides=com.company.name=INVALID</arg> </compilerArgs> </configuration></plugin>
Standard mapping functions between often used proto types and java types:
- Timestamp <-> Instant
- Duration <-> Duration
- Date <-> LocalDate
- TimeOfDay <-> LocalTime
- byte[] <-> ByteString
Seeprotobuf-support-standard and/orprotobuf-support-lite folders for aready-to-use mapstruct mapper.
NB: Make sure you addcollectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED to your mapping interfacesas protobuf stubs use the builder pattern.
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)public interface ... {NB: Make sure you use thesame 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:
<annotationProcessorPaths> <path> <groupId>no.entur.mapstruct.spi</groupId> <artifactId>protobuf-spi-impl</artifactId><!-- Make sure mapstruct version here is the same as below--> <version>LATEST.VERSION</version> </path></annotationProcessorPaths><dependencies><dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${org.mapstruct.version}</version></dependency></dependencies>
Complete example:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>17</source> <target>17</target> <annotationProcessorPaths> <path> <groupId>no.entur.mapstruct.spi</groupId> <artifactId>protobuf-spi-impl</artifactId> <version>LATEST.VERSION</version> </path> </annotationProcessorPaths> </configuration> <dependencies> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${org.mapstruct.version}</version> </dependency> </dependencies></plugin>
Note: See Maven setup regarding using the same version of mapstruct both in the annotation processor and the generaldependency.
implementation"org.mapstruct:mapstruct:${mapstructVersion}"annotationProcessor"org.mapstruct:mapstruct-processor:${mapstructVersion}"annotationProcessor"no.entur.mapstruct.spi:protobuf-spi-impl:LATEST.VERSION"
http://mapstruct.org/documentation/stable/reference/html/index.html#using-spi
About
Protobuf accessor naming strategy for Mapstruct
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.