-
Notifications
You must be signed in to change notification settings - Fork 516
org.immutables example with mapstruct #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
norrs
wants to merge
1
commit into
mapstruct:main
Choose a base branch
from
norrs:immutables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Copyright 2020 MapStruct developers | ||
and/or other contributors as indicated by the @authors tag. See the | ||
copyright.txt file in the distribution for a full listing of all | ||
contributors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
<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> | ||
|
||
<groupId>org.mapstruct.examples.immutables</groupId> | ||
<artifactId>mapstruct-examples-immutables</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<packaging>jar</packaging> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version> | ||
<org.immutables.version>2.8.2</org.immutables.version> | ||
<com.fasterxml.jackson.version>2.10.3</com.fasterxml.jackson.version> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct</artifactId> | ||
<version>${org.mapstruct.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- immutables dependencies should not end up on classpath --> | ||
<dependency> | ||
<groupId>org.immutables</groupId> | ||
<artifactId>value</artifactId> | ||
<version>${org.immutables.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- IntelliJ pre 2018.1.1 requires the mapstruct processor to be present as provided dependency --> | ||
<!-- <dependency> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct-processor</artifactId> | ||
<version>${org.mapstruct.version}</version> | ||
<scope>provided</scope> | ||
</dependency>--> | ||
|
||
|
||
<!-- Demostrates immutables are serializable with jackson --> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
<version>${com.fasterxml.jackson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>${com.fasterxml.jackson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.15.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<!-- See https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html --> | ||
<!-- Classpath elements to supply as annotation processor path. If specified, the compiler --> | ||
<!-- will detect annotation processors only in those classpath elements. If omitted, the --> | ||
<!-- default classpath is used to detect annotation processors. The detection itself depends --> | ||
<!-- on the configuration of annotationProcessors. --> | ||
<!-- --> | ||
<!-- According to this documentation, the provided dependency processor is not considered! --> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>org.immutables</groupId> | ||
<artifactId>value</artifactId> | ||
<version>${org.immutables.version}</version> | ||
</path> | ||
<path> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct-processor</artifactId> | ||
<version>${org.mapstruct.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> |
47 changes: 47 additions & 0 deletions
47
mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ApiPerson.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2020 MapStruct developers | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.example.immutables; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import org.immutables.value.Value; | ||
|
||
|
||
@Value.Immutable | ||
@JsonDeserialize(builder = ApiPerson.Builder.class) | ||
@ExampleStyle | ||
public interface ApiPerson { | ||
String getId(); | ||
|
||
String getFirstName(); | ||
|
||
String getFullName(); | ||
|
||
String getMapperDoingFullName(); | ||
|
||
// Required for MapStruct to find the builder | ||
static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
// And Builder resides in generated Immutable implementation, which is private protected. | ||
// See https://immutables.github.io/json.html#jackson | ||
class Builder extends ImmutableApiPerson.Builder { | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/DtoPerson.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright 2020 MapStruct developers | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.example.immutables; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import org.immutables.value.Value; | ||
|
||
@Value.Immutable | ||
@JsonDeserialize(builder = DtoPerson.Builder.class) | ||
@ExampleStyle | ||
public interface DtoPerson { | ||
String getUuid(); | ||
|
||
String getFirstName(); | ||
|
||
String getLastName(); | ||
|
||
default String getFullName() { | ||
return getFirstName() + " " + getLastName(); | ||
} | ||
|
||
// Required for MapStruct to find the builder | ||
static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
// And Builder resides in generated Immutable implementation, which is private protected. | ||
// See https://immutables.github.io/json.html#jackson | ||
class Builder extends ImmutableDtoPerson.Builder { | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ExampleStyle.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright 2020 MapStruct developers | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.example.immutables; | ||
|
||
|
||
import org.immutables.value.Value; | ||
|
||
import static org.immutables.value.Value.Style.ImplementationVisibility.PACKAGE; | ||
|
||
/** | ||
* Our ExampleStyle to make it easier to reuse annotations on our org.immutables interfaces. | ||
* <p> | ||
* Details: https://immutables.github.io/style.html | ||
* <pre> | ||
* builder = "new" is required for serializing with jackson | ||
* visibility to package is nice to hide implementation details from users using our builders | ||
* overshadowImplementation sets return types on builders to be the interface instead of the generated immutable class. ( https://immutables.github.io/immutable.html#builder ) | ||
*/ | ||
@Value.Style(builder = "new", visibility = PACKAGE, overshadowImplementation = true) | ||
public @interface ExampleStyle { | ||
} |
33 changes: 33 additions & 0 deletions
33
mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ResponseMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright 2020 MapStruct developers | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.example.immutables; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
@Mapper | ||
public interface ResponseMapper { | ||
|
||
ResponseMapper INSTANCE = Mappers.getMapper(ResponseMapper.class); | ||
|
||
@Mapping(source = "uuid", target = "id") | ||
@Mapping(expression = "java(person.getFirstName() + \" \" + person.getLastName())", target = "mapperDoingFullName") | ||
ApiPerson mapFrom(DtoPerson person); | ||
} |
59 changes: 59 additions & 0 deletions
59
mapstruct-immutables/src/test/java/org/mapstruct/example/immutables/ResponseMapperTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright 2020 MapStruct developers | ||
* and/or other contributors as indicated by the @authors tag. See the | ||
* copyright.txt file in the distribution for a full listing of all | ||
* contributors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mapstruct.example.immutables; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ResponseMapperTest { | ||
|
||
@Test | ||
public void testResponseMapper() { | ||
DtoPerson.Builder barBuilder = new DtoPerson.Builder() | ||
.uuid("foo") | ||
.firstName("John") | ||
.lastName("Snow"); | ||
|
||
|
||
assertThat(ResponseMapper.INSTANCE.mapFrom(barBuilder.build())) | ||
.isEqualTo(new ApiPerson.Builder() | ||
.id("foo") | ||
.firstName("John") | ||
.fullName("John Snow") | ||
.mapperDoingFullName("John Snow") | ||
.build()); | ||
|
||
} | ||
|
||
@Test | ||
public void testResponseMapperWithSerializingPersonToJson() throws JsonProcessingException { | ||
DtoPerson.Builder personFromDatabase = new DtoPerson.Builder() | ||
.uuid("foo") | ||
.firstName("John") | ||
.lastName("Snow"); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String json = mapper.writeValueAsString(ResponseMapper.INSTANCE.mapFrom(personFromDatabase.build())); | ||
assertThat(json).isEqualTo("{\"id\":\"foo\",\"firstName\":\"John\",\"fullName\":\"John Snow\",\"mapperDoingFullName\":\"John Snow\"}"); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish we didn't need getters here on the immutables, and we could have stayed with fluent getters, but mapstruct requires POJOs with getters I believe.
The builders however supports the fluent setters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Immutables is able to support this. This is from Immutables documentation:
"No annotations are required on abstract accessor methods in order for them to become attributes."
https://immutables.github.io/immutable.html#overview
Can we modify the ImmutablesAccessorNamingStrategy to support abstract accessors to qualify as getters.