Skip to content

Commit

Permalink
convert list field failure
Browse files Browse the repository at this point in the history
  • Loading branch information
kgorozhankin committed Nov 22, 2016
1 parent a4f941d commit 75fa611
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 13 deletions.
19 changes: 11 additions & 8 deletions src/main/java/net/badata/protobuf/converter/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public static Converter create() {
*
* @param fieldsIgnore Map of fields that has to be ignored by this converter instance.
* @return Converter instance.
*
* @deprecated use {@code create(Configuration)} instead.
*/
@Deprecated
Expand Down Expand Up @@ -180,7 +179,7 @@ private List<Field> getDomainFields(final Class clazz) {

fields.addAll(Arrays.asList(clazz.getDeclaredFields()));

if(configuration.withInheritedFields()) {
if (configuration.withInheritedFields()) {
Class superClazz = clazz.getSuperclass();
if (superClazz != null) {
fields.addAll(getDomainFields(superClazz));
Expand Down Expand Up @@ -239,8 +238,10 @@ private <T, E extends Message, K extends Collection> K toProtobuf(final Class<K>
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));
if (domainCollection != null) {
for (T domain : domainCollection) {
protobufCollection.add(toProtobuf(protobufClass, domain));
}
}
return (K) protobufCollection;
}
Expand Down Expand Up @@ -314,17 +315,19 @@ private void fillProtobufField(final FieldResolver fieldResolver, final MappingR
if (FieldUtils.isComplexType(collectionType)) {
Class<? extends Message> protobufCollectionClass = MessageUtils.getMessageCollectionType(
mappingResult.getDestination(), FieldUtils.createProtobufGetterName(fieldResolver));
mappedValue = createProtobufValueList(protobufCollectionClass, (Collection) mappedValue);
mappedValue = createProtobufValueList(protobufCollectionClass, field.getType(),
(Collection) mappedValue);
}
case MAPPED:
default:
fieldWriter.write(fieldResolver, mappedValue);
}
}

private <E extends Message> Collection<?> createProtobufValueList(final Class<E> type, final Collection<?>
domainCollection) {
return createNestedConverter().toProtobuf(domainCollection.getClass(), type, domainCollection);
private <E extends Message> Collection<?> createProtobufValueList(final Class<E> type, final Class<?>
domainCollectionClass, final Collection<?> domainCollection) {
return createNestedConverter()
.toProtobuf((Class<? extends Collection>) domainCollectionClass, type, domainCollection);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ private void createTestDomain() {
testDomain.setFieldConversionValue(fieldConverterTest);
testDomain.setSimpleListValue(Arrays.asList("110"));


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)));
testDomain.setComplexNullableCollectionValue(null);
}

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

Assert.assertTrue(result.getComplexNullableCollectionValue().isEmpty());
}

@Test
Expand Down Expand Up @@ -198,6 +200,8 @@ public void testDomainToProtobuf() {
result.getComplexListValue(0).getIntValue());
Assert.assertEquals(testDomain.getComplexSetValue().iterator().next().getIntValue(),
result.getComplexSetValue(0).getIntValue());

Assert.assertTrue(result.getComplexNullableCollectionValueList().isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,6 @@ public void testMultiMapping() {
Assert.assertEquals((Object) testProtobuf.getMultiMappingListValue(0).getLongValueChanged(),
listChild.getLongValue());
}


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


public Long getLongValue() {
Expand Down Expand Up @@ -151,6 +153,14 @@ public Set<PrimitiveTest> getComplexSetValue() {
public void setComplexSetValue(final Set<PrimitiveTest> complexSetValue) {
this.complexSetValue = complexSetValue;
}

public List<PrimitiveTest> getComplexNullableCollectionValue() {
return complexNullableCollectionValue;
}

public void setComplexNullableCollectionValue(final List<PrimitiveTest> complexNullableCollectionValue) {
this.complexNullableCollectionValue = complexNullableCollectionValue;
}
}

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

0 comments on commit 75fa611

Please sign in to comment.