Skip to content

Commit

Permalink
complex objects collections conversion support
Browse files Browse the repository at this point in the history
  • Loading branch information
kgorozhankin committed May 12, 2016
1 parent 3403885 commit 8ecb588
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 30 deletions.
64 changes: 41 additions & 23 deletions src/main/java/net/badata/protobuf/converter/Converter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.badata.protobuf.converter;

import com.google.protobuf.Message;
import net.badata.protobuf.converter.annotation.ProtoClass;
import net.badata.protobuf.converter.exception.ConverterException;
import net.badata.protobuf.converter.exception.MappingException;
import net.badata.protobuf.converter.exception.TypeRelationException;
import net.badata.protobuf.converter.exception.WriteException;
import net.badata.protobuf.converter.mapping.Mapper;
import net.badata.protobuf.converter.mapping.MappingResult;
import net.badata.protobuf.converter.annotation.ProtoClass;
import net.badata.protobuf.converter.exception.ConverterException;
import net.badata.protobuf.converter.exception.WriteException;
import net.badata.protobuf.converter.utils.AnnotationUtils;
import net.badata.protobuf.converter.utils.FieldUtils;
import net.badata.protobuf.converter.writer.DomainWriter;
Expand All @@ -16,6 +16,8 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
Expand Down Expand Up @@ -62,18 +64,26 @@ protected Converter(final FieldsIgnore fieldsIgnore) {
/**
* Create domain object list from Protobuf dto list.
*
* @param domainClass Expected domain object type.
* @param protobufList Source instance of Protobuf dto list.
* @param <T> Domain type.
* @param <E> Protobuf dto type.
* @param domainClass Expected domain object type.
* @param protobufCollection Source instance of Protobuf dto collection.
* @param <T> Domain type.
* @param <E> Protobuf dto type.
* @return Domain objects list filled with data stored in the Protobuf dto list.
*/
public <T, E extends Message> List<T> toDomain(final Class<T> domainClass, final List<E> protobufList) {
List<T> domainList = new ArrayList<T>();
for (E protobuf : protobufList) {
public <T, E extends Message> List<T> toDomain(final Class<T> domainClass, final Collection<E>
protobufCollection) {
return toDomain(List.class, domainClass, protobufCollection);

}

private <T, E extends Message, K extends Collection> K toDomain(final Class<K> collectionClass,
final Class<T> domainClass, final Collection<E> protobufCollection) {
Collection<T> domainList = List.class.isAssignableFrom(collectionClass) ? new ArrayList<T>() : new
HashSet<T>();
for (E protobuf : protobufCollection) {
domainList.add(toDomain(domainClass, protobuf));
}
return domainList;
return (K) domainList;
}

/**
Expand Down Expand Up @@ -154,18 +164,25 @@ private <T> List<T> createDomainValueList(final Class<T> type, final Object prot
/**
* Create Protobuf dto list from domain object list.
*
* @param protobufClass Expected Protobuf class.
* @param domainList Source domain instance list.
* @param <T> Domain type.
* @param <E> Protobuf dto type.
* @param protobufClass Expected Protobuf class.
* @param domainCollection Source domain collection.
* @param <T> Domain type.
* @param <E> Protobuf dto type.
* @return Protobuf dto list filled with data stored in the domain object list.
*/
public <T, E extends Message> List<E> toProtobuf(final Class<E> protobufClass, final List<T> domainList) {
List<E> protobufList = new ArrayList<E>();
for (T domain : domainList) {
protobufList.add(toProtobuf(protobufClass, domain));
public <T, E extends Message> List<E> toProtobuf(final Class<E> protobufClass, final Collection<T>
domainCollection) {
return toProtobuf(List.class, protobufClass, domainCollection);
}

private <T, E extends Message, K extends Collection> K toProtobuf(final Class<K> collectionClass,
final Class<E> protobufClass, final Collection<T> domainCollection) {
Collection<E> protobufCollection = List.class.isAssignableFrom(collectionClass) ? new ArrayList<E>() : new
HashSet<E>();
for (T domain : domainCollection) {
protobufCollection.add(toProtobuf(protobufClass, domain));
}
return protobufList;
return (K)protobufCollection;
}

/**
Expand Down Expand Up @@ -230,16 +247,17 @@ private void fillProtobufField(final Field field, final MappingResult mappingRes
if (FieldUtils.isComplexType(collectionType)) {
Class<? extends Message> protobufCollectionClass = AnnotationUtils.extractMessageType(
collectionType);
mappedValue = createProtobufValueList(protobufCollectionClass, mappedValue);
mappedValue = createProtobufValueList(protobufCollectionClass, (Collection)mappedValue);
}
case MAPPED:
default:
fieldWriter.write(field, mappedValue);
}
}

private <E extends Message> List<E> createProtobufValueList(final Class<E> type, final Object domainCollection) {
return create(fieldsIgnore).toProtobuf(type, (List<?>) domainCollection);
private <E extends Message> Collection<?> createProtobufValueList(final Class<E> type, final Collection<?>
domainCollection) {
return create(fieldsIgnore).toProtobuf(domainCollection.getClass(), type, domainCollection);
}

}
14 changes: 11 additions & 3 deletions src/test/java/net/badata/protobuf/converter/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ private void createTestProtobuf() {
.setDateLong(System.currentTimeMillis())
.addStringSetValue("11"))
.setNullDefaultValue(ConverterProto.NullDefaultTest.newBuilder()
.setCustomInspectionString("Assumed as null value")
.setDefaultPrimitives(ConverterProto.PrimitiveTest.newBuilder())
.setCustomInspectionString("Assumed as null value")
.setDefaultPrimitives(ConverterProto.PrimitiveTest.newBuilder())
)
.addStringListValue("10")
.addComplexListValue(ConverterProto.PrimitiveTest.newBuilder().setIntValue(1001))
.addComplexSetValue(ConverterProto.PrimitiveTest.newBuilder().setIntValue(1002))
.build();
}

Expand Down Expand Up @@ -86,6 +87,9 @@ private void createTestDomain() {
ConverterDomain.PrimitiveTest primitiveTestItem = new ConverterDomain.PrimitiveTest();
primitiveTestItem.setIntValue(-1001);
testDomain.setComplexListValue(Arrays.asList(primitiveTestItem));
ConverterDomain.PrimitiveTest primitiveTestSItem = new ConverterDomain.PrimitiveTest();
primitiveTestItem.setIntValue(-1002);
testDomain.setComplexSetValue(new HashSet<ConverterDomain.PrimitiveTest>(Arrays.asList(primitiveTestSItem)));
}

private void createIgnoredFieldsMap() {
Expand Down Expand Up @@ -142,6 +146,8 @@ public void testProtobufToDomain() {
Assert.assertEquals(testProtobuf.getStringListValue(0), result.getSimpleListValue().get(0));
Assert.assertEquals(testProtobuf.getComplexListValue(0).getIntValue(),
result.getComplexListValue().get(0).getIntValue());
Assert.assertEquals(testProtobuf.getComplexSetValue(0).getIntValue(),
result.getComplexSetValue().iterator().next().getIntValue());
}

@Test
Expand Down Expand Up @@ -193,12 +199,14 @@ public void testDomainToProtobuf() {
Assert.assertEquals(testDomain.getSimpleListValue().get(0), result.getStringListValue(0));
Assert.assertEquals(testDomain.getComplexListValue().get(0).getIntValue(),
result.getComplexListValue(0).getIntValue());
Assert.assertEquals(testDomain.getComplexSetValue().iterator().next().getIntValue(),
result.getComplexSetValue(0).getIntValue());
}

@Test
public void testFieldIgnoreDomainToProtobuf() {
ConverterProto.ConverterTest result = Converter.create(fieldsIgnore)
.toProtobuf(ConverterProto.ConverterTest.class, testDomain);
.toProtobuf(ConverterProto.ConverterTest.class, testDomain);

Assert.assertNotNull(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static class Test {
private List<String> simpleListValue;
@ProtoField
private List<PrimitiveTest> complexListValue;
@ProtoField(converter = SetListConverterImpl.class)
private Set<PrimitiveTest> complexSetValue;


public Long getLongValue() {
Expand Down Expand Up @@ -133,6 +135,14 @@ public List<PrimitiveTest> getComplexListValue() {
public void setComplexListValue(final List<PrimitiveTest> complexListValue) {
this.complexListValue = complexListValue;
}

public Set<PrimitiveTest> getComplexSetValue() {
return complexSetValue;
}

public void setComplexSetValue(final Set<PrimitiveTest> complexSetValue) {
this.complexSetValue = complexSetValue;
}
}

@ProtoClass(ConverterProto.PrimitiveTest.class)
Expand Down
Loading

0 comments on commit 8ecb588

Please sign in to comment.