mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
highly WIP
This commit is contained in:
parent
4e0d73db1d
commit
30651b989b
@ -20,4 +20,8 @@ public enum ConditionStrategy {
|
|||||||
* The condition method should be applied to check if a source parameters should be mapped.
|
* The condition method should be applied to check if a source parameters should be mapped.
|
||||||
*/
|
*/
|
||||||
SOURCE_PARAMETERS,
|
SOURCE_PARAMETERS,
|
||||||
|
/**
|
||||||
|
* The condition method should be applied whether an element should be added to the iterable target.
|
||||||
|
*/
|
||||||
|
ITERABLE_ELEMENTS,
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FIXME
|
||||||
|
*
|
||||||
|
* @author Oliver Erhart
|
||||||
|
* @since 1.7
|
||||||
|
* @see Condition @Condition
|
||||||
|
*/
|
||||||
|
@Target({ ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.CLASS)
|
||||||
|
@Condition(appliesTo = ConditionStrategy.ITERABLE_ELEMENTS)
|
||||||
|
public @interface IterableElementCondition {
|
||||||
|
|
||||||
|
}
|
@ -11,5 +11,6 @@ package org.mapstruct.ap.internal.gem;
|
|||||||
public enum ConditionStrategyGem {
|
public enum ConditionStrategyGem {
|
||||||
|
|
||||||
PROPERTIES,
|
PROPERTIES,
|
||||||
SOURCE_PARAMETERS
|
SOURCE_PARAMETERS,
|
||||||
|
ITERABLE_ELEMENTS
|
||||||
}
|
}
|
||||||
|
@ -93,6 +93,9 @@ public class ConditionOptions {
|
|||||||
else if ( strategy == ConditionStrategyGem.PROPERTIES ) {
|
else if ( strategy == ConditionStrategyGem.PROPERTIES ) {
|
||||||
return hasValidStrategyForProperties( condition, method, parameters, messager );
|
return hasValidStrategyForProperties( condition, method, parameters, messager );
|
||||||
}
|
}
|
||||||
|
else if ( strategy == ConditionStrategyGem.ITERABLE_ELEMENTS ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalStateException( "Invalid condition strategy: " + strategy );
|
throw new IllegalStateException( "Invalid condition strategy: " + strategy );
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
<#if resultType.arrayType>
|
<#if resultType.arrayType>
|
||||||
<#if existingInstanceMapping>
|
<#if existingInstanceMapping>
|
||||||
<#-- we can't clear an existing array, so we've got to clear by setting values to default -->
|
<#-- we can't clear an existing array, so we've got to clear by setting values to default -->
|
||||||
for (int ${index2Name} = 0; ${index2Name} < ${resultName}.length; ${index2Name}++ ) {
|
// test
|
||||||
${resultName}[${index2Name}] = ${resultElementType.null};
|
${resultName}[${index2Name}] = ${resultElementType.null};
|
||||||
}
|
}
|
||||||
return<#if returnType.name != "void"> ${resultName}</#if>;
|
return<#if returnType.name != "void"> ${resultName}</#if>;
|
||||||
@ -74,7 +74,9 @@
|
|||||||
}
|
}
|
||||||
<#else>
|
<#else>
|
||||||
for ( <@includeModel object=sourceElementType/> ${loopVariableName} : ${sourceParameter.name} ) {
|
for ( <@includeModel object=sourceElementType/> ${loopVariableName} : ${sourceParameter.name} ) {
|
||||||
<@includeModel object=elementAssignment targetBeanName=resultName targetWriteAccessorName="add" targetType=resultElementType/>
|
if ( countryIsNotNull( employeeDto ) ) {
|
||||||
|
<@includeModel object=elementAssignment targetBeanName=resultName targetWriteAccessorName="add" targetType=resultElementType/>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</#if>
|
</#if>
|
||||||
<#list afterMappingReferences as callback>
|
<#list afterMappingReferences as callback>
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.conditional.iterable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.mapstruct.IterableElementCondition;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.ap.test.conditional.Employee;
|
||||||
|
import org.mapstruct.ap.test.conditional.EmployeeDto;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Oliver Erhart
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface IterableElementConditionMapper {
|
||||||
|
|
||||||
|
IterableElementConditionMapper INSTANCE =
|
||||||
|
Mappers.getMapper( IterableElementConditionMapper.class );
|
||||||
|
|
||||||
|
@Mapping(target = "nin", source = "name")
|
||||||
|
@Mapping(target = "ssid", source = "uniqueIdNumber")
|
||||||
|
Employee map(EmployeeDto employee);
|
||||||
|
|
||||||
|
List<Employee> map(List<EmployeeDto> employees);
|
||||||
|
|
||||||
|
@IterableElementCondition
|
||||||
|
default boolean countryIsNotNull(EmployeeDto value) {
|
||||||
|
return value.getCountry() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.conditional.iterable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.test.conditional.Employee;
|
||||||
|
import org.mapstruct.ap.test.conditional.EmployeeDto;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Oliver Erhart
|
||||||
|
*/
|
||||||
|
@IssueKey("1610")
|
||||||
|
@WithClasses({
|
||||||
|
Employee.class,
|
||||||
|
EmployeeDto.class
|
||||||
|
})
|
||||||
|
public class IterableElementConditionTest {
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
@WithClasses({
|
||||||
|
IterableElementConditionMapper.class
|
||||||
|
})
|
||||||
|
public void conditionalMethodWithSourceParameter() {
|
||||||
|
IterableElementConditionMapper mapper = IterableElementConditionMapper.INSTANCE;
|
||||||
|
|
||||||
|
EmployeeDto dtoWithoutCountry = new EmployeeDto();
|
||||||
|
dtoWithoutCountry.setName( "Tester" );
|
||||||
|
dtoWithoutCountry.setUniqueIdNumber( "SSID-001" );
|
||||||
|
dtoWithoutCountry.setCountry( null );
|
||||||
|
|
||||||
|
EmployeeDto dtoWithCountry = new EmployeeDto();
|
||||||
|
dtoWithCountry.setName( "Tester" );
|
||||||
|
dtoWithCountry.setUniqueIdNumber( "SSID-001" );
|
||||||
|
dtoWithCountry.setCountry( "Austria" );
|
||||||
|
|
||||||
|
ArrayList<EmployeeDto> employees = new ArrayList<>();
|
||||||
|
employees.add( dtoWithoutCountry );
|
||||||
|
employees.add( dtoWithCountry );
|
||||||
|
|
||||||
|
List<Employee> result = mapper.map( employees );
|
||||||
|
assertThat( result )
|
||||||
|
.singleElement()
|
||||||
|
.satisfies(
|
||||||
|
d -> assertThat(d.getName()).isEqualTo( "Tester" )
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user