Skip to content

Commit

Permalink
#100 Implement return null in update mode
Browse files Browse the repository at this point in the history
  • Loading branch information
slemesle committed Apr 26, 2017
1 parent 3893b5c commit 00a45e1
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void buildMappingMethod(JavaWriter writer, List<InOutType> inOutTypes, S

isPrimitiveOrImmutable = isPrimitiveOrImmutable || inOutType.inIsPrimitive();
if (!isPrimitiveOrImmutable) {
root = root.child(controlNotNull(getInVar(inOutType.in()), inOutTypeOrigin.isOutPutAsParam()));
root = root.child(controlNotNull(getInVar(inOutType.in())));
root.body(topRoot.body);
} else {
root = root.child(topRoot.body);
Expand All @@ -157,6 +157,11 @@ private void buildMappingMethod(JavaWriter writer, List<InOutType> inOutTypes, S
inId++;
}

if (outputAsParam){
// Set out to null if all in types are null
methodNode.lastChild().child(setOutNullAllBlocks(inOutTypes));
}

tryBlockPtr = null;
if (mapperWrapper.isUseCyclicMapping()) {
ptr = methodNode.child;
Expand Down Expand Up @@ -459,7 +464,7 @@ private MappingSourceNode buildEmbeddedMapping(Field customField, BeanWrapper in
field.append('.').append(beanPtr.getGetterFor(lastVisitedField)).append("()");

if (sourceEmbedded) {
ptr = ptr.body(controlNotNull(field.toString(), false));
ptr = ptr.body(controlNotNull(field.toString()));
} else {
ptr = ptr.child(controlNull(field.toString()));
ptr.body(set(previousFieldPath + '.' + beanPtr.getSetterFor(lastVisitedField), "new " + beanPtr.getTypeForGetter(lastVisitedField) + "(" + context.newParams() + ")"));
Expand Down Expand Up @@ -500,7 +505,7 @@ private MappingSourceNode buildEmbeddedMapping(Field customField, BeanWrapper in
try {


MappingBuilder mappingBuilder = null, interceptor = null;
MappingBuilder mappingBuilder = null;
if (customField.mappingRegistry() != null) {
mappingBuilder = customField.mappingRegistry().getMapper(inOutType);

Expand Down Expand Up @@ -529,9 +534,7 @@ private MappingSourceNode buildEmbeddedMapping(Field customField, BeanWrapper in
ptr.body(mappingBuilder.build(context, vars)) :
ptr.child(mappingBuilder.build(context, vars));
generateStack(context);
if (interceptor != null) { // Call custom interceptor after mapping
ptr = ptr.child(interceptor.build(context, vars));
}

} else {
handleNotSupported(inOutType, ptr);
}
Expand All @@ -543,7 +546,7 @@ private MappingSourceNode buildEmbeddedMapping(Field customField, BeanWrapper in
}

if (!sourceEmbedded && inBean.getTypeForGetter(customField.from).getKind() == TypeKind.DECLARED) { // Ensure we do not map if source is null
MappingSourceNode ifNode = controlNotNull(getInVar(inBean.typeMirror) + "." + inBean.getGetterFor(customField.from) + "()", false);
MappingSourceNode ifNode = controlNotNull(getInVar(inBean.typeMirror) + "." + inBean.getGetterFor(customField.from) + "()");
ifNode.body(ptrRoot.child);
ptrRoot.child = ifNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ private static PrimitiveType getUnboxedPrimitive(DeclaredType declaredType, Mapp
}

private static MappingSourceNode notNullInField(final SourceNodeVars vars) {
return controlNotNull(vars.inGetter(), false);
return controlNotNull(vars.inGetter());
}

public static MappingBuilder newCustomMapperImmutableForUpdateGraph(final InOutType inOutType, final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.squareup.javawriter.JavaWriter;
import fr.xebia.extras.selma.InstanceCache;
import fr.xebia.extras.selma.SelmaConstants;
import fr.xebia.extras.selma.SelmaUtils;
import fr.xebia.extras.selma.SimpleInstanceCache;

import javax.lang.model.type.TypeMirror;
Expand Down Expand Up @@ -99,6 +100,29 @@ public static MappingSourceNode mapMethod(final MapperGeneratorContext context,
};
}

public static MappingSourceNode setOutNullAllBlocks(final List<InOutType> inOutTypes) {

return new MappingSourceNode() {
@Override void writeNode(JavaWriter writer) throws IOException {
StringBuilder sbIns = new StringBuilder();
sbIns.append(SelmaUtils.class.getCanonicalName()).append(".areNull(");
int id = 0;
for (InOutType inOutType : inOutTypes) {
if (id > 0){
sbIns.append(", ");
}
sbIns.append(getInVar(inOutType.in()));
id++;
}
sbIns.append(")");

writer.beginControlFlow("if (" + sbIns.toString() + ")");
writer.emitStatement("out = null");
writer.endControlFlow();
}
};
}

public static MappingSourceNode controlInCache(final String field, final String outType) {
return new MappingSourceNode() {
@Override void writeNode(JavaWriter writer) throws IOException {
Expand Down Expand Up @@ -126,16 +150,12 @@ public static MappingSourceNode popFromCache() {
};
}

public static MappingSourceNode controlNotNull(final String field, final boolean outPutAsParam) {
public static MappingSourceNode controlNotNull(final String field) {
return new MappingSourceNode() {
@Override void writeNode(JavaWriter writer) throws IOException {
writer.beginControlFlow(String.format("if (%s != null)", field));
// body is Mandatory here
writeBody(writer);
if (outPutAsParam) {
writer.nextControlFlow("else");
writer.emitStatement("out = null");
}
writer.endControlFlow();
}
};
Expand Down Expand Up @@ -228,8 +248,7 @@ void writeNode(JavaWriter writer) throws IOException { // declaring out should

public static MappingSourceNode notSupported(final String message) {
return new MappingSourceNode() {
@Override
void writeNode(JavaWriter writer) throws IOException {
@Override void writeNode(JavaWriter writer) throws IOException {
writer.emitJavadoc("Throw UnsupportedOperationException because we failed to generate the mapping code:\n" + message);
// new lines in message result in uncompilable code.
writer.emitStatement("throw new UnsupportedOperationException(\"%s\")", message.replace("\n", " "));
Expand Down Expand Up @@ -455,4 +474,5 @@ public MappingSourceNode child(MappingSourceNode child) {
this.child = child;
return child;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public interface AggregationMapper {

AggregatedBean mapFromAggregate(FirstBean first, SecondBean second);


@Maps(withCustom = AggregatedInterceptor.class)
AggregatedBean mapFromAggregateWithInterceptor(FirstBean first, SecondBean second);

AggregatedBean mapFromAggregateInUpdate(FirstBean first, SecondBean second, AggregatedBean out);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,48 @@ public void given_2_aggregated_beans_should_call_interceptor(){
Assert.assertEquals(second.getSalary() + AggregatedInterceptor.SALARY_INC, res.getSalary());
Assert.assertEquals(second.getStartDate(), res.getStartDate());
}


@Test
public void given_2_aggregated_beans_in_update_should_update_with_aggregate(){
// Given
FirstBean first = new FirstBean();
first.setFirstName("Toto");
first.setLastName("de toor");
first.setAge(35l);

SecondBean second = new SecondBean();
second.setJobTitle("CFO");
second.setSalary(150000l);
second.setStartDate(new Date());

AggregatedBean out = new AggregatedBean();

AggregationMapper mapper = Selma.builder(AggregationMapper.class).build();

// When
AggregatedBean res = mapper.mapFromAggregateInUpdate(first, second, out);

// Then
Assert.assertEquals(first.getFirstName(), res.getFirstName());
Assert.assertEquals(first.getLastName(), res.getLastName());
Assert.assertEquals(first.getAge(), res.getAge());
Assert.assertEquals(second.getJobTitle(), res.getJobTitle());
Assert.assertEquals(second.getSalary(), res.getSalary());
Assert.assertEquals(second.getStartDate(), res.getStartDate());
}

@Test
public void given_null_beans_in_update_should_return_null(){
// Given
AggregatedBean out = new AggregatedBean();

AggregationMapper mapper = Selma.builder(AggregationMapper.class).build();

// When
AggregatedBean res = mapper.mapFromAggregateInUpdate(null, null, out);

// Then
Assert.assertNull(res);
}
}

0 comments on commit 00a45e1

Please sign in to comment.