Skip to content
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

Added example for mapping a List<String> to List<StringValue> #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mapstruct-protobuf3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<os.mavenplugin.version>1.4.0.Final</os.mavenplugin.version>
<protobuf.plugin.version>0.5.0</protobuf.plugin.version>
<protobuf.java.version>3.2.0</protobuf.java.version>
<protobuf.java.version>3.8.0</protobuf.java.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down
4 changes: 2 additions & 2 deletions mapstruct-protobuf3/usage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@
<goal>compile-custom</goal>
</goals>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}
<protocArtifact>com.google.protobuf:protoc:3.11.2:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.2.0:exe:${os.detected.classifier}
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.27.0:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.mapstruct.example.mapper;

import com.google.protobuf.*;
import org.mapstruct.Mapper;

@Mapper
public interface ProtoMapper
{

default BoolValue booleanToBoolValue(final Boolean bool) {
return bool == null ? null : BoolValue.of(bool);
}

default Boolean boolValueToBoolean(final BoolValue boolValue) {
return boolValue == null ? null : boolValue.getValue();
}

default Float floatValueToFloat(final FloatValue floatValue) {
return floatValue == null ? null : floatValue.getValue();
}

default FloatValue floatToFloatValue(final Float floatValue) {
return floatValue == null ? null : FloatValue.newBuilder().setValue(floatValue).build();
}

default Integer int32ValueToInteger(final Int32Value int32Value) {
return int32Value == null ? null : int32Value.getValue();
}

default Int32Value integerToInt32Value(final Integer integer) {
return integer == null ? null : Int32Value.newBuilder().setValue(integer).build();
}

default String stringValueToString(final StringValue stringValue) {
return stringValue != null ? stringValue.getValue() : null;
}

default StringValue stringToStringValue(final String string) {
return string != null ? StringValue.of(string) : null;
}

default Long int64ValueToLong(final Int64Value value) {
return value != null ? value.getValue() : null;
}

default Int64Value longToInt64Value(final Long value) {
return value != null ? Int64Value.of(value) : null;
}

default byte[] bytesValueToBytes(final BytesValue value) {
return value != null ? value.getValue().toByteArray() : null;
}

default BytesValue bytesToBytesValue(final byte[] value) {
return value != null ? BytesValue.of(ByteString.copyFrom(value)) : null;
}

default DoubleValue doubleToDoubleValue(final Double value) {
return value != null ? DoubleValue.of(value) : null;
}

default Double doubleValueToDouble(final DoubleValue value) {
return value != null ? value.getValue() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
package org.mapstruct.example.mapper;

import com.google.protobuf.StringValue;
import org.mapstruct.AfterMapping;
import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValueCheckStrategy;
import org.mapstruct.ValueMapping;
import org.mapstruct.example.protobuf.Department;
Expand All @@ -36,7 +39,8 @@
* @author Thomas Kratz
*/
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
uses = ProtoMapper.class)
public interface UserMapper {

UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
Expand All @@ -50,6 +54,7 @@ public interface UserMapper {
@Mapping(source = "permissionsList", target = "permissions")
@Mapping(source = "mainDepartmentsList", target = "mainDepartments")
@Mapping(source = "departmentsList", target = "departments")
@Mapping(source = "alternateEmailsList", target = "alternateEmails")
User map(UserDTO userDTO);

@ValueMapping(source = "UNRECOGNIZED", target = MappingConstants.NULL)
Expand All @@ -60,4 +65,12 @@ public interface UserMapper {

Department map(DepartmentDTO departmentDTO);
DepartmentDTO map(Department department);

@AfterMapping
default void after(User user, @MappingTarget final UserDTO.Builder builder) {
int idx = 0;
for (String item : user.getAlternateEmails()) {
builder.addAlternateEmails(idx++, StringValue.of(item));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class User {
private List<Permission> permissions = new ArrayList<>();
private List<Department> mainDepartments = new ArrayList<>();
private List<Department> departments = new ArrayList<>();
private List<String> alternateEmails = new ArrayList<>();

public String getId() {
return id;
Expand Down Expand Up @@ -46,11 +47,19 @@ public void setDepartments(List<Department> departments) {
this.departments = departments;
}

public List<Department> getMainDepartments() {
return mainDepartments;
}
public List<Department> getMainDepartments() {
return mainDepartments;
}

public void setMainDepartments(List<Department> mainDepartments) {
this.mainDepartments = mainDepartments;
}
public void setMainDepartments(List<Department> mainDepartments) {
this.mainDepartments = mainDepartments;
}

public List<String> getAlternateEmails() {
return alternateEmails;
}

public void setAlternateEmails(List<String> alternateEmails) {
this.alternateEmails = alternateEmails;
}
}
3 changes: 3 additions & 0 deletions mapstruct-protobuf3/usage/src/main/proto/User.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ syntax = "proto3";
option java_package = "org.mapstruct.example.protobuf";
option java_outer_classname = "UserProtos";

import "google/protobuf/wrappers.proto";

message UserDTO {
string id = 1;
string email = 2;
repeated PermissionDTO permissions = 3;
repeated DepartmentDTO main_departments = 4;
repeated DepartmentDTO departments = 5;
repeated google.protobuf.StringValue alternateEmails = 6;
}

enum PermissionDTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.mapstruct.example;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.StringValue;
import org.junit.Assert;
import org.junit.Test;
import org.mapstruct.example.mapper.UserMapper;
Expand All @@ -42,6 +43,7 @@ public void test() throws InvalidProtocolBufferException {
user.getPermissions().add(Permission.ADMIN);
user.getMainDepartments().add(new Department("SALES"));
user.getDepartments().add(new Department("AFTER_MARKET"));
user.getAlternateEmails().add("[email protected]");

UserDTO dto = UserMapper.INSTANCE.map(user);
UserDTO deserialized = UserDTO.parseFrom(dto.toByteArray());
Expand All @@ -56,6 +58,8 @@ public void test() throws InvalidProtocolBufferException {

Assert.assertEquals(1,back.getDepartments().size());
Assert.assertEquals("AFTER_MARKET",back.getDepartments().get(0).getName());

Assert.assertEquals("[email protected]", back.getAlternateEmails().get(0));
}


Expand Down