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
Access to the target property name #2688
Comments
Can you please provide an example? |
@Mapper(config = MapperBConfig.class , uses = EValueMapper.class)
@Component
public abstract class AttribMapper {
@Mapping(source = "indicator" , target = "ïndicator")
@Mapping(source = "indicator2" , target = "shellType")
public abstract TargetAttributes TargetAttributesMapper( SourceAttributes src, @MappingTarget TargetAttributes target);
@Condition
public boolean conditioncheck(String value)
{
if(contains in map attribute check) // basically am trying to map to the target attribute if that attribute is required by consumer
return true
else return false
}
} This method should get invoke for all attributes mapping. Could you please suggest here. |
If I understand correctly what you want is something like I've got a However writing the mapping yourself might not be that much additional code, and mapstruct can still use it in the mappingchain like this: @Mapper
public abstract class ConditionalMapMapper {
static class Item {
Map<String, String> value;
// getters and setters
}
static class ItemDto {
Map<String, String> value;
// getters and setters
}
abstract Item map(ItemDto dto, @MappingTarget Item item);
void map(Map<String, String> source, @MappingTarget Map<String, String> target) {
for ( Entry<String, String> entry : source.entrySet() ) {
if ( target.containsKey( entry.getKey() ) ) {
target.put( entry.getKey(), entry.getValue() );
}
}
}
} The map method with the source and target map will be used in the implementation to map from/to a map. public class ConditionalMapMapperImpl extends ConditionalMapMapper {
@Override
Item map(ItemDto dto, Item item) {
if ( dto == null ) {
return null;
}
if ( dto.getValue() != null ) {
if ( item.getValue() == null ) {
item.setValue( new LinkedHashMap<String, String>() );
}
map( dto.getValue(), item.getValue() );
}
else {
item.setValue( null );
}
return item;
}
} Hope this helps. |
No , I am looking something if Target attribute name is present in my attribute list(list which has selected target attribute name for each diff scenario ) to map then only i should map else I should skip mapping. |
So something like: @Mapper
public abstract class ConditionalMapMapper {
static class Item {
Map<String, String> value;
// getters and setters
}
static class ItemDto {
Map<String, String> value;
// getters and setters
}
abstract Item map(ItemDto dto, @MappingTarget Item item, @Context List<String> attributeList);
void map(Map<String, String> source, @MappingTarget Map<String, String> target, @Context List<String> attributeList) {
for ( Entry<String, String> entry : source.entrySet() ) {
if ( attributeList.contains( entry.getKey() ) ) {
target.put( entry.getKey(), entry.getValue() );
}
}
}
} The map method with the source and target map will be used in the implementation to map from/to a map. public class ConditionalMapMapperImpl extends ConditionalMapMapper {
@Override
Item map(ItemDto dto, Item item, List<String> attributeList) {
if ( dto == null ) {
return null;
}
if ( dto.getValue() != null ) {
if ( item.getValue() == null ) {
item.setValue( new LinkedHashMap<String, String>() );
}
map( dto.getValue(), item.getValue(), attributeList );
}
else {
item.setValue( null );
}
return item;
}
} Am I getting closer or still misunderstanding you? |
Sorry for the delay . This is what we are looking. package mapstruct;
@Mapper(uses = TesterMapper.PresenceUtill.class)
public interface TesterMapper {
TesterMapper inst = Mappers.getMapper(TesterMapper.class);
@Mapping(source ="name" , target ="name" )
@Mapping(source ="secName" , target ="sName" , conditionExpression = "java(variablePresenceCheck(\"sName\"))" )
@Mapping(source ="isHuman" , target ="isHuman" )
MapsStructTarget mapstrucMapper (MapStructSource src);
class PresenceUtill {
@Condition
public boolean checkmethod(String str , String source)
{
if (null != str)
{
System.out.println(str);
System.out.println(source);
return true;
}
return false;
}
@Condition
public boolean checkmethod(Boolean str)
{
if (null != str)
{
return true;
}
return false;
}
}
default boolean variablePresenceCheck(String str) {
if (I will check this variable name is present in my attrib list ) { return true;
}
return false; }
} Using conditionExpression I can achieve this , by passing the name of the variable explicitly in conditionExpression. |
@filiphr , I don't believe there is an annotation to reference the property name yet, is there? |
Thanks for the information . We can use conditional expression when we have diff names. But when we have the same names for the entire classes , in that case we are not using @mapping annotations. Could you please suggest for this scenario ? can we achieve this by overriding any of the existing methods for now |
You are right @Zegveld there is no annotation that would allow access to the target property name. Supporting I will rename this issue. @HemalathaGowdar you can still use e.g.
|
Yes , we can . But the problem is we have so many files with lot of attributes . That new annotation to support in @condition methods . If you could help me with this new feature timelines . Ours is enhancement project ,we can wait for this feature. This would help us. |
Unfortunately we can't provide timelines, since all of us are working on this on a part time voluntary basis and we try to focus on the most requested features for new releases. In case you are interested in seeing this feature sooner you can always provide a PR that we can review and integrate. |
This is exactly the problem I'm facing now. In my case for a given class I've a list of non editable properties which is configurable and may change at runtime and this Thank you all. |
When am giving conditional Expression , the NULL check (Null value check strategy) is getting replaced by conditional expression method. I need NULL check too irrespective of conditionalExpression. Could u please suggest |
@HemalathaGowdar please do not use this issue to ask questions about unrelated things. The documentation for this is in the Conditional Mapping section of our documentation. If something is not clear please open a discussion, ask on StackOverflow or out Gitter Room. |
Access to the target property name mapstruct#2688 mapstruct#2688 As agreed in: mapstruct#2831
Thanks for your work on this @ivlcic. This has been integrated into main. |
This seems to correspond to what our project is needing. Is there a planned date for the release of |
@FlorianCassayre we have some other issues that we would like to tackle before we release a first beta release for 1.6. I hope that I can speed this up |
Thanks for the reply, I understand; we will follow that closely and will be looking forward to that beta release. |
Hi guys how's 1.6.0 progressing? |
@ivlcic it is going slower that I would like. Unfortunately I do not have ample of time, so trying to find the time to wrap things up and do an initial release |
Expose target property to method arguments
The text was updated successfully, but these errors were encountered: