Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Protobuf accessor naming strategy for Mapstruct

License

NotificationsYou must be signed in to change notification settings

entur/mapstruct-spi-protobuf

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+.

ProtobufAccessorNamingStrategy

ExtendsDefaultProtobufAccessorNamingStrategy and provides necessary information to map all fields automatically *except*

  • oneof

which require manual mapping.

ProtobufEnumMappingStrategy

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>

Support - Mapping functions:

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.

Usage

See example project

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 ... {

Maven

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>

Gradle

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"

More information:

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

About

Protobuf accessor naming strategy for Mapstruct

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors11

Languages


[8]ページ先頭

©2009-2025 Movatter.jp