MCPcopy Index your code
hub / github.com/entur/mapstruct-spi-protobuf

github.com/entur/mapstruct-spi-protobuf @release-1.60.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release release-1.60.0 ↗ · + Follow
212 symbols 538 edges 22 files 10 documented · 5%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Mapstruct SPI implementation for protocol buffers

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.


<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
  • ByteString <-> byte[]

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.

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
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:


<annotationProcessorPaths>
    <path>
        <groupId>no.entur.mapstruct.spi</groupId>
        <artifactId>protobuf-spi-impl</artifactId> 
        <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>


17


        <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 general dependency.

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

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Method 190
Class 14
Interface 5
Enum 3

Languages

Java100%

Modules by API surface

usage/src/main/java/no/entur/mapstruct/spi/protobuf/User.java89 symbols
support-standard/src/test/java/no/entur/abt/mapstruct/ProtobufStandardMappingsTest.java22 symbols
spi-impl/src/main/java/no/entur/mapstruct/spi/protobuf/ProtobufAccessorNamingStrategy.java20 symbols
support-lite/src/test/java/no/entur/abt/mapstruct/ProtobufStandardMappingsTest.java13 symbols
support-core/src/main/java/no/entur/abt/mapstruct/common/ProtobufStandardMappings.java11 symbols
spi-impl/src/main/java/no/entur/mapstruct/spi/protobuf/ProtobufEnumMappingStrategy.java9 symbols
usage/src/test/java/no/entur/mapstruct/example/MappingTest.java7 symbols
spi-impl/src/main/java/no/entur/mapstruct/spi/protobuf/ProcessingEnvOptionsHolder.java6 symbols
usage/src/main/java/no/entur/mapstruct/spi/protobuf/Department.java4 symbols
usage/src/main/java/no/entur/mapstruct/example/UserMapper.java4 symbols
support-standard/src/main/java/no/entur/abt/mapstruct/ProtobufStandardMappings.java4 symbols
support-lite/src/main/java/no/entur/abt/mapstruct/ProtobufStandardMappings.java4 symbols

For agents

$ claude mcp add mapstruct-spi-protobuf \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page