Skip to content

Commit

Permalink
include declared fields of domain subclasses while converting
Browse files Browse the repository at this point in the history
  • Loading branch information
afzalzhas committed Jul 31, 2016
1 parent 646cd7b commit 8b0f93d
Show file tree
Hide file tree
Showing 6 changed files with 719 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/main/java/net/badata/protobuf/converter/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@

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;
import java.util.*;

/**
* Converts data from Protobuf messages to domain model objects and vice versa.
Expand Down Expand Up @@ -154,7 +151,7 @@ private <E extends Message> void fillDomain(final Object domain, final E protobu
Class<?> domainClass = domain.getClass();
Mapper fieldMapper = AnnotationUtils.createMapper(protoClassAnnotation);
FieldResolverFactory fieldFactory = AnnotationUtils.createFieldFactory(protoClassAnnotation);
for (Field field : domainClass.getDeclaredFields()) {
for (Field field : getAllFields(domainClass)) {
if (fieldsIgnore.ignored(field)) {
continue;
}
Expand Down Expand Up @@ -258,7 +255,7 @@ private <E extends Message.Builder> void fillProtobuf(final E protobuf, final Ob
Class<?> domainClass = domain.getClass();
Mapper fieldMapper = AnnotationUtils.createMapper(protoClassAnnotation);
FieldResolverFactory fieldFactory = AnnotationUtils.createFieldFactory(protoClassAnnotation);
for (Field field : domainClass.getDeclaredFields()) {
for (Field field : getAllFields(domainClass)) {
if (fieldsIgnore.ignored(field)) {
continue;
}
Expand All @@ -267,6 +264,19 @@ private <E extends Message.Builder> void fillProtobuf(final E protobuf, final Ob
}
}

private List<Field> getAllFields(Class clazz) {
List<Field> fields = new ArrayList<Field>();

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

Class superClazz = clazz.getSuperclass();
if(superClazz != null){
fields.addAll( getAllFields(superClazz) );
}

return fields;
}

private void fillProtobufField(final FieldResolver fieldResolver, final MappingResult mappingResult)
throws WriteException {
ProtobufWriter fieldWriter = new ProtobufWriter((Message.Builder) mappingResult.getDestination());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.badata.protobuf.converter;


import net.badata.protobuf.converter.domain.Coffee;
import net.badata.protobuf.converter.proto.LiquidProto;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;


public class ConverterIncludeSubClassFieldsTest {

@Test
public void testDomainToProtobuf() {
Coffee coffee = new Coffee();
coffee.setMlVolume(100);
coffee.setTemperature(40);
coffee.setSwirling(true);
coffee.setClockwise(false);

LiquidProto.Coffee coffeeProto = Converter.create().toProtobuf(LiquidProto.Coffee.class,coffee);

assertEquals(coffee.getMlVolume(), coffeeProto.getMlVolume());
assertEquals(coffee.getTemperature(), coffeeProto.getTemperature());
assertEquals(coffee.isSwirling(),coffeeProto.getSwirling());
assertEquals(coffee.isClockwise(), coffeeProto.getClockwise());

}
}
29 changes: 29 additions & 0 deletions src/test/java/net/badata/protobuf/converter/domain/Coffee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.badata.protobuf.converter.domain;

import net.badata.protobuf.converter.annotation.ProtoClass;
import net.badata.protobuf.converter.annotation.ProtoField;
import net.badata.protobuf.converter.proto.LiquidProto;

@ProtoClass(LiquidProto.Coffee.class)
public class Coffee extends Liquid {
@ProtoField
private boolean swirling;
@ProtoField
private boolean clockwise;

public boolean isSwirling() {
return swirling;
}

public void setSwirling(boolean swirling) {
this.swirling = swirling;
}

public boolean isClockwise() {
return clockwise;
}

public void setClockwise(boolean clockwise) {
this.clockwise = clockwise;
}
}
27 changes: 27 additions & 0 deletions src/test/java/net/badata/protobuf/converter/domain/Liquid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.badata.protobuf.converter.domain;


import net.badata.protobuf.converter.annotation.ProtoField;

public class Liquid {
@ProtoField
private int mlVolume;
@ProtoField
private float temperature;

public int getMlVolume() {
return mlVolume;
}

public void setMlVolume(int mlVolume) {
this.mlVolume = mlVolume;
}

public float getTemperature() {
return temperature;
}

public void setTemperature(float temperature) {
this.temperature = temperature;
}
}
Loading

0 comments on commit 8b0f93d

Please sign in to comment.