highly WIP

This commit is contained in:
thunderhook 2024-09-20 23:29:37 +02:00
parent 4e0d73db1d
commit 30651b989b
7 changed files with 134 additions and 3 deletions

View File

@ -20,4 +20,8 @@ public enum ConditionStrategy {
* The condition method should be applied to check if a source parameters should be mapped.
*/
SOURCE_PARAMETERS,
/**
* The condition method should be applied whether an element should be added to the iterable target.
*/
ITERABLE_ELEMENTS,
}

View File

@ -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 {
}

View File

@ -11,5 +11,6 @@ package org.mapstruct.ap.internal.gem;
public enum ConditionStrategyGem {
PROPERTIES,
SOURCE_PARAMETERS
SOURCE_PARAMETERS,
ITERABLE_ELEMENTS
}

View File

@ -93,6 +93,9 @@ public class ConditionOptions {
else if ( strategy == ConditionStrategyGem.PROPERTIES ) {
return hasValidStrategyForProperties( condition, method, parameters, messager );
}
else if ( strategy == ConditionStrategyGem.ITERABLE_ELEMENTS ) {
return true;
}
else {
throw new IllegalStateException( "Invalid condition strategy: " + strategy );
}

View File

@ -24,7 +24,7 @@
<#if resultType.arrayType>
<#if existingInstanceMapping>
<#-- 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};
}
return<#if returnType.name != "void"> ${resultName}</#if>;
@ -74,7 +74,9 @@
}
<#else>
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>
<#list afterMappingReferences as callback>

View File

@ -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;
}
}

View File

@ -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" )
);
}
}