mapping exclusions
This commit is contained in:
@@ -50,4 +50,9 @@ public interface MappingConfiguration {
|
||||
*/
|
||||
MappingConfiguration setGenericConverter(GenericConverter converter);
|
||||
|
||||
/**
|
||||
* Configures that this mapping should be excluded (ignored and not executed).
|
||||
*/
|
||||
void setExclude();
|
||||
|
||||
}
|
||||
@@ -122,7 +122,7 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
* @param nestedMapper the nested mapper
|
||||
*/
|
||||
public void addNestedMapper(Mapper<?, ?> nestedMapper) {
|
||||
Class[] typeInfo = getRequiredTypeInfo(nestedMapper);
|
||||
Class<?>[] typeInfo = getRequiredTypeInfo(nestedMapper);
|
||||
addNestedMapper(typeInfo[0], typeInfo[1], nestedMapper);
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ public class SpelMapper implements Mapper<Object, Object> {
|
||||
* @param targetType the target nested property type
|
||||
* @param nestedMapper the nested mapper
|
||||
*/
|
||||
public void addNestedMapper(Class sourceType, Class targetType, Mapper<?, ?> nestedMapper) {
|
||||
public void addNestedMapper(Class<?> sourceType, Class<?> targetType, Mapper<?, ?> nestedMapper) {
|
||||
this.conversionService.addGenericConverter(sourceType, targetType, new MapperConverter(nestedMapper));
|
||||
}
|
||||
|
||||
|
||||
@@ -38,18 +38,14 @@ class SpelMapping implements MappingConfiguration {
|
||||
|
||||
private GenericConverter converter;
|
||||
|
||||
private boolean exclude;
|
||||
|
||||
public SpelMapping(Expression source, Expression target) {
|
||||
this.source = source;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public String getSourceExpressionString() {
|
||||
return this.source.getExpressionString();
|
||||
}
|
||||
|
||||
public String getTargetExpressionString() {
|
||||
return this.target.getExpressionString();
|
||||
}
|
||||
// implementing MappingConfiguration
|
||||
|
||||
public MappingConfiguration setConverter(Converter<?, ?> converter) {
|
||||
return setGenericConverter(new ConverterGenericConverter(converter));
|
||||
@@ -64,8 +60,25 @@ class SpelMapping implements MappingConfiguration {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setExclude() {
|
||||
this.exclude = true;
|
||||
}
|
||||
|
||||
// public methods
|
||||
|
||||
public String getSourceExpressionString() {
|
||||
return this.source.getExpressionString();
|
||||
}
|
||||
|
||||
public String getTargetExpressionString() {
|
||||
return this.target.getExpressionString();
|
||||
}
|
||||
|
||||
public void map(EvaluationContext sourceContext, EvaluationContext targetContext,
|
||||
Collection<MappingFailure> failures) {
|
||||
if (exclude) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Object value = this.source.getValue(sourceContext);
|
||||
if (this.converter != null) {
|
||||
|
||||
@@ -81,6 +81,23 @@ public class SpelMapperTests {
|
||||
assertEquals(31, target.age);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapAutomaticWithExclusions() {
|
||||
Map<String, Object> source = new HashMap<String, Object>();
|
||||
source.put("name", "Keith");
|
||||
source.put("test", "3");
|
||||
source.put("favoriteSport", "FOOTBALL");
|
||||
|
||||
Person target = new Person();
|
||||
|
||||
mapper.addMapping("test").setExclude();
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("Keith", target.name);
|
||||
assertEquals(0, target.age);
|
||||
assertEquals(Sport.FOOTBALL, target.favoriteSport);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapSameSourceFieldToMultipleTargets() {
|
||||
Map<String, Object> source = new HashMap<String, Object>();
|
||||
@@ -270,7 +287,6 @@ public class SpelMapperTests {
|
||||
return names[0] + " P. " + names[1];
|
||||
}
|
||||
});
|
||||
|
||||
mapper.map(source, target);
|
||||
|
||||
assertEquals("Keith P. Donald", target.name);
|
||||
|
||||
Reference in New Issue
Block a user