#3165 Support adders for array / iterable to collection

This commit is contained in:
Etien Rožnik 2023-05-01 09:42:58 +02:00 committed by GitHub
parent a8df94cc20
commit 4843123e6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 113 additions and 2 deletions

View File

@ -527,7 +527,7 @@ public class PropertyMapping extends ModelElement {
Assignment result = rightHandSide;
String adderIteratorName = sourcePropertyName == null ? targetPropertyName : sourcePropertyName;
if ( result.getSourceType().isCollectionType() ) {
if ( result.getSourceType().isIterableType() ) {
result = new AdderWrapper( result, method.getThrownTypes(), isFieldAssignment(), adderIteratorName );
}
else if ( result.getSourceType().isStreamType() ) {

View File

@ -39,7 +39,15 @@ public class AdderWrapper extends AssignmentWrapper {
// localVar is iteratorVariable
String desiredName = Nouns.singularize( adderIteratorName );
rhs.setSourceLoopVarName( rhs.createUniqueVarName( desiredName ) );
adderType = first( getSourceType().determineTypeArguments( Collection.class ) );
if ( getSourceType().isCollectionType() ) {
adderType = first( getSourceType().determineTypeArguments( Collection.class ) );
}
else if ( getSourceType().isArrayType() ) {
adderType = getSourceType().getComponentType();
}
else { // iterable
adderType = first( getSourceType().determineTypeArguments( Iterable.class ) );
}
}
@Override

View File

@ -149,6 +149,12 @@ public class SourceRHS extends ModelElement implements Assignment {
else if ( sourceType.isStreamType() ) {
return first( sourceType.determineTypeArguments( Stream.class ) );
}
else if ( sourceType.isArrayType() ) {
return sourceType.getComponentType();
}
else if ( sourceType.isIterableType() ) {
return first( sourceType.determineTypeArguments( Iterable.class ) );
}
}
return sourceType;
}

View File

@ -0,0 +1,65 @@
/*
* 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.bugs._3165;
import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.ArrayList;
import java.util.List;
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface Issue3165Mapper {
Issue3165Mapper INSTANCE = Mappers.getMapper( Issue3165Mapper.class );
Target toTarget(Source source);
class Source {
private String[] pets;
private Iterable<String> cats;
public Source(String[] pets, Iterable<String> cats) {
this.pets = pets;
this.cats = cats;
}
public String[] getPets() {
return pets;
}
public Iterable<String> getCats() {
return cats;
}
}
class Target {
private List<String> pets;
private List<String> cats;
Target() {
this.pets = new ArrayList<>();
this.cats = new ArrayList<>();
}
public List<String> getPets() {
return pets;
}
public void addPet(String pet) {
pets.add( pet );
}
public List<String> getCats() {
return cats;
}
public void addCat(String cat) {
cats.add( cat );
}
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.bugs._3165;
import java.util.Arrays;
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;
@WithClasses({
Issue3165Mapper.class
})
@IssueKey("3165")
class Issue3165MapperTest {
@ProcessorTest
void supportsAdderWhenMappingArrayAndIterableToCollection() {
Issue3165Mapper.Source src = new Issue3165Mapper.Source(
new String[] { "cat", "dog", "mouse" },
Arrays.asList( "ivy", "flu", "freya" )
);
Issue3165Mapper.Target target = Issue3165Mapper.INSTANCE.toTarget( src );
assertThat( target.getPets() ).containsExactly( "cat", "dog", "mouse" );
assertThat( target.getCats() ).containsExactly( "ivy", "flu", "freya" );
}
}