Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
S1artie committed Jul 29, 2024
0 parents commit 08c5959
Show file tree
Hide file tree
Showing 41 changed files with 3,238 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "maven" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
ignore:
- dependency-name: "*"
update-types: [ "version-update:semver-major" ]
44 changes: 44 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Maven Central Deployment

on:
workflow_dispatch

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven

- name: Set up Maven Central
uses: actions/setup-java@v3
with: # running setup-java again overwrites the settings.xml
distribution: 'temurin'
java-version: '17'
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
server-username: OSSRH_USERNAME # env variable for username in deploy
server-password: OSSRH_TOKEN # env variable for token in deploy
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase

- name: Build and Publish to Maven Central
run: mvn -B clean deploy -Prelease --file pom.xml
env:
OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
OSSRH_TOKEN: ${{ secrets.OSSRH_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
32 changes: 32 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

on:
push:
branches: [ "**" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven

- name: Build with Maven
run: mvn -B clean install --file pom.xml
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
*target*
*.jar
*.war
*.ear
*.class
.DS_Store

# eclipse specific git ignore
.project
.metadata
.factorypath
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# IntelliJ specific git ignore
.idea/
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Mapstruct SPI implementation for protocol buffers mapping

This project provides a SPI implementation for [Mapstruct](http://mapstruct.org/) to generate mapping code from protocol
buffers to the following targets:

- Plain Old Java Objects (POJOs)
- [Immutables](https://immutables.github.io/) value objects
- Java records

Unit tests exist to validate all of these mappings. The SPI implementation generally requires Mapstruct 1.5.5 and Java
1.8+ (of course if you want to map to records, Java 14+ is required).

The enum mapping strategy assumes that Google's enum value naming scheme is used, as described
here: https://developers.google.com/protocol-buffers/docs/style#enum

This SPI implementation also includes a [pull request](https://github.com/mapstruct/mapstruct/pull/2219) from the
Mapstruct repository that was not merged yet, but fixes a
deficiency with Mapstructs' own org.immutables support when using inner classes and @Value.Enclosing.

## Usage

Your protobuf mapping interfaces must be annotated with `@Mapper`
and `collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED`
because the protobuf classes use a builder pattern.

```java

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

```

Include the mapstruct dependency and the annotation processor in your Maven project:

```xml

<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>de.firehead</groupId>
<artifactId>mapstruct-spi-protobuf</artifactId>
<version>1.0.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

```

Or for Gradle:

```java

implementation"org.mapstruct:mapstruct:${mapstructVersion}"
annotationProcessor"org.mapstruct:mapstruct-processor:${mapstructVersion}"
annotationProcessor"de.firehead:mapstruct-spi-protobuf:1.0.0"

```

106 changes: 106 additions & 0 deletions mapstruct-spi-protobuf-test-immutables/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>de.firehead</groupId>
<artifactId>mapstruct-spi-protobuf-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>mapstruct-spi-protobuf-test-immutables</artifactId>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.javadoc.skip>true</maven.javadoc.skip>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>de.firehead</groupId>
<artifactId>mapstruct-spi-protobuf-test-protos</artifactId>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.1</version>
</extension>
</extensions>

<plugins>
<!-- Generate protobuf Java classes during compile phase -->
<plugin>
<groupId>dev.cookiecode</groupId>
<artifactId>another-protobuf-maven-plugin</artifactId>
<version>2.1.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.immutables</groupId>
<artifactId>value-processor</artifactId>
<version>${immutables.version}</version>
</path>
<path>
<groupId>de.firehead</groupId>
<artifactId>mapstruct-spi-protobuf</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* mapstruct-spi-protobuf
*
* Copyright (C) 2024
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package de.firehead.mapstruct.spi.protobuf.test.immutables;

import org.immutables.value.Value;

/**
* Test immutable object for deep testing purposes.
* This corresponds to the following test proto message:
* <p>
* message DeepTestProtoMessage {
* TestProtoMessage testProtoMessage = 1;
* repeated TestProtoMessage testProtoMessageList = 2;
* map<string, TestProtoMessage> testProtoMessageMap = 3;
* }
*/
@Value.Immutable
public interface DeepTestImmutableObject {

TestImmutableObject getTestProtoMessagePlain();

java.util.List<TestImmutableObject> getTestProtoMessageList();

java.util.Map<String, TestImmutableObject> getTestProtoMessageMap();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* mapstruct-spi-protobuf
*
* Copyright (C) 2024
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package de.firehead.mapstruct.spi.protobuf.test.immutables;

public enum TestEnum {

VALUE;
}
Loading

0 comments on commit 08c5959

Please sign in to comment.