Skip to content

#3303 Mapping from Map to Map and specify source and target keys of type String #3367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,60 @@ public Map<Long, Date> stringStringMapToLongDateMap(Map<String, String> source)
----
====

It is also possible to reassign keys of a map-based mapping method when the keys are of type `java.lang.String`. The following shows an example:

.Map mapping method with reassigning keys
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
public interface CsvMapMapper {

@Mapping(target = "payments", source = "Raw_Payments")
@Mapping(target = "count", source = "Raw_Count")
Map<String, Long> mapCsvContent(Map<String, String> source);
}
----
====

The generated mapper will look like:

.Map mapping method with reassigning keys generated implementation
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
// GENERATED CODE
public class CsvMapMapperImpl implements CsvMapMapper {

private final Map<String, String> mapCsvContentKeyMappings;

public CsvMapMapperImpl() {
this.mapCsvContentKeyMappings = new HashMap<>();
this.mapCsvContentKeyMappings.put( "Raw_Payments", "payments" );
this.mapCsvContentKeyMappings.put( "Raw_Count", "count" );
}

@Override
public Map<String, Long> mapCsvContent(Map<String, String> source) {
if ( source == null ) {
return null;
}

Map<String, Long> map = new LinkedHashMap<String, Long>();

for ( Map.Entry<String, String> entry : source.entrySet() ) {
String key = entry.getKey();
Long value = Long.parseLong( entry.getValue() );
map.put( this.mapCsvContentKeyMappings.getOrDefault( key, key ), value );
}

return map;
}
}
----
====

[[collection-mapping-strategies]]
=== Collection mapping strategies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Set;

import org.mapstruct.ap.internal.model.common.ConstructorFragment;
import org.mapstruct.ap.internal.model.common.ModelElement;
import org.mapstruct.ap.internal.model.common.Type;

Expand All @@ -20,11 +21,11 @@
*/
public class AnnotatedConstructor extends ModelElement implements Constructor {

private String name;
private final String name;
private final List<AnnotationMapperReference> mapperReferences;
private final List<Annotation> annotations;
private final NoArgumentConstructor noArgumentConstructor;
private final Set<SupportingConstructorFragment> fragments;
private final Set<ConstructorFragment> fragments;

public static AnnotatedConstructor forComponentModels(String name,
List<AnnotationMapperReference> mapperReferences,
Expand All @@ -37,7 +38,7 @@ public static AnnotatedConstructor forComponentModels(String name,
noArgumentConstructor = (NoArgumentConstructor) constructor;
}
NoArgumentConstructor noArgConstructorToInBecluded = null;
Set<SupportingConstructorFragment> fragmentsToBeIncluded = Collections.emptySet();
Set<ConstructorFragment> fragmentsToBeIncluded = Collections.emptySet();
if ( includeNoArgConstructor ) {
if ( noArgumentConstructor != null ) {
noArgConstructorToInBecluded = noArgumentConstructor;
Expand All @@ -60,7 +61,7 @@ else if ( noArgumentConstructor != null ) {

private AnnotatedConstructor(String name, List<AnnotationMapperReference> mapperReferences,
List<Annotation> annotations, NoArgumentConstructor noArgumentConstructor,
Set<SupportingConstructorFragment> fragments) {
Set<ConstructorFragment> fragments) {
this.name = name;
this.mapperReferences = mapperReferences;
this.annotations = annotations;
Expand Down Expand Up @@ -100,7 +101,7 @@ public NoArgumentConstructor getNoArgumentConstructor() {
return noArgumentConstructor;
}

public Set<SupportingConstructorFragment> getFragments() {
public Set<ConstructorFragment> getFragments() {
return fragments;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.internal.model;

import java.util.Collections;
import java.util.Set;

import org.mapstruct.ap.internal.model.common.ModelElement;
import org.mapstruct.ap.internal.model.common.Type;

/**
* Represents a constructor that is used for mapping keys of a {@link MapMappingMethod}.
*
* @author Oliver Erhart
*/
public class MapKeyMappingConstructor extends ModelElement implements Constructor {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used?


private final String name;
private final String delegateName;
private final boolean invokeSuperConstructor;

public MapKeyMappingConstructor(String name, String delegateName, boolean invokeSuperConstructor) {
this.name = name;
this.delegateName = delegateName;
this.invokeSuperConstructor = invokeSuperConstructor;
}

@Override
public Set<Type> getImportTypes() {
return Collections.emptySet();
}

@Override
public String getName() {
return name;
}

public String getDelegateName() {
return delegateName;
}

public boolean isInvokeSuperConstructor() {
return invokeSuperConstructor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.internal.model;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.mapstruct.ap.internal.model.common.ConstructorFragment;
import org.mapstruct.ap.internal.model.common.ModelElement;
import org.mapstruct.ap.internal.model.common.Type;

/**
* Constructor fragment for key mappings for a {@link MapMappingMethod}.
*
* @author Oliver Erhart
*/
public class MapKeyMappingConstructorFragment extends ModelElement implements ConstructorFragment {

private final MapKeyMappingField field;
private final MappingMethod definingMethod;
private final List<KeyMappingEntry> keyMappings;

public MapKeyMappingConstructorFragment(MapKeyMappingField field, MappingMethod definingMethod,
List<KeyMappingEntry> keyMappings) {
this.field = field;
this.definingMethod = definingMethod;
this.keyMappings = keyMappings;
}

@Override
public Set<Type> getImportTypes() {
return Collections.emptySet();
}

public MapKeyMappingField getField() {
return field;
}

public MappingMethod getDefiningMethod() {
return definingMethod;
}

public List<KeyMappingEntry> getKeyMappings() {
return keyMappings;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( field == null ) ? 0 : field.hashCode() );
return result;
}

@Override
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
MapKeyMappingConstructorFragment other = (MapKeyMappingConstructorFragment) obj;

if ( !Objects.equals( field, other.field ) ) {
return false;
}
return true;
}

public static void addAllMapKeyMappingFragmentsIn(List<MapMappingMethod> mapMappingMethods,
Set<ConstructorFragment> targets) {
for ( MapMappingMethod mapMappingMethod : mapMappingMethods ) {
MapKeyMappingConstructorFragment fragment = mapMappingMethod.getMapMappingConstructorFragment();
if ( fragment != null ) {
targets.add( mapMappingMethod.getMapMappingConstructorFragment() );
}
}
}

public static class KeyMappingEntry {

String source;
String target;

public KeyMappingEntry(String source, String target) {
this.source = source;
this.target = target;
}

public String getSource() {
return source;
}

public String getTarget() {
return target;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.internal.model;

import java.util.List;
import java.util.Set;

/**
* Field that is used for mapping keys of a {@link MapMappingMethod}.
*
* @author Oliver Erhart
*/
public class MapKeyMappingField extends Field {

public MapKeyMappingField(String variableName) {
super( null, variableName, true );
}

public static void addAllMapKeyMappingFieldsIn(List<MapMappingMethod> mapMappingMethods, Set<Field> targets) {
for ( MapMappingMethod mapMappingMethod : mapMappingMethods ) {
MapKeyMappingConstructorFragment fragment = mapMappingMethod.getMapMappingConstructorFragment();
if (fragment != null) {
Field field = fragment.getField();
if ( field != null ) {
targets.add( field );
}
}
}
}

}
Loading