GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Access to the target property name · Issue #2688 · mapstruct/mapstruct

Access to the target property name  #2688

New issue
Copy link
New issue
Copy link
Closed
Closed
Access to the target property name #2688
Copy link
Labels
feature
Milestone
1.6.0.Beta1

Description

@HemalathaGowdar
HemalathaGowdar
opened on Dec 13, 2021
Issue body actions

Expose target property to method arguments

Reacted by BabkenSahakyan

Activity

Next
  1. filiphr commented on Dec 13, 2021

    @filiphr
    filiphr
    on Dec 13, 2021
    Member
    More actions

    Can you please provide an example?

  2. filiphr
    added
    closing-when-no-responseMarking issues that are ready to be closedMarking issues that are ready to be closed
    and removed
    closing-when-no-responseMarking issues that are ready to be closedMarking issues that are ready to be closed
    on Dec 13, 2021
  3. HemalathaGowdar commented on Dec 13, 2021

    @HemalathaGowdar
    HemalathaGowdar
    on Dec 13, 2021
    Author
    More actions
    @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.
    I also tried Conditionexpression to check whether this attribute in teh consumer list to map or not . This also not coming in the generated impl class . I am basically trying for presence check of that attribute in the consumer list. if yes , I should do mapping else ignore mapping part

    Could you please suggest here.

  4. Zegveld commented on Dec 20, 2021

    @Zegveld
    Zegveld
    on Dec 20, 2021
    Contributor
    More actions

    If I understand correctly what you want is something like I've got a Map<String, Object> named source and another Map<String, Object> named target and I only want to map the entries from source for which there is already an entry present in target?
    Trying to do this using conditions with Iterables or Maps is not supported yet, see #1610 for more information.

    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.

  5. filiphr
    added
    closing-when-no-responseMarking issues that are ready to be closedMarking issues that are ready to be closed
    on Dec 25, 2021
  6. HemalathaGowdar commented on Dec 27, 2021

    @HemalathaGowdar
    HemalathaGowdar
    on Dec 27, 2021
    Author
    More actions

    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.

  7. Zegveld commented on Dec 27, 2021

    @Zegveld
    Zegveld
    on Dec 27, 2021
    Contributor
    More actions

    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?

  8. HemalathaGowdar commented on Jan 18, 2022

    @HemalathaGowdar
    HemalathaGowdar
    on Jan 18, 2022
    Author
    More actions

    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.
    Do we have something in @condition annotation too - I need the name of the target variable on each mappings. so that I can evaluate whether I need to set this variable or skip

  9. Zegveld
    removed
    closing-when-no-responseMarking issues that are ready to be closedMarking issues that are ready to be closed
    on Jan 18, 2022
  10. Zegveld commented on Jan 18, 2022

    @Zegveld
    Zegveld
    on Jan 18, 2022
    Contributor
    More actions

    @filiphr , I don't believe there is an annotation to reference the property name yet, is there?
    So this would be a feature request to implement a method argument annotation much like how @TargetType works for passing the class, perhaps something like @TargetProperty.

  11. Zegveld
    added
    feature
    on Jan 18, 2022
  12. HemalathaGowdar commented on Jan 18, 2022

    @HemalathaGowdar
    HemalathaGowdar
    on Jan 18, 2022
    Author
    More actions

    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

  13. filiphr commented on Jan 18, 2022

    @filiphr
    filiphr
    on Jan 18, 2022
    Member
    More actions

    You are right @Zegveld there is no annotation that would allow access to the target property name. Supporting @TargetProperty for a string method argument is something that we could add.

    I will rename this issue.

    @HemalathaGowdar you can still use @Mapping even if the source and target names are identical.

    e.g.

    @Mapping(target = "id", conditionExpression = "java(...)")
    
  14. filiphr
    changed the title [-]Condition annotated method not being invoked in generated IMPL class[/-] [+]Access to the target property name [/+] on Jan 18, 2022
  15. 1 remaining item

  16. filiphr commented on Jan 22, 2022

    @filiphr
    filiphr
    on Jan 22, 2022
    Member
    More actions

    If you could help me with this new feature timelines

    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.

  17. BabkenSahakyan commented on Feb 20, 2022

    @BabkenSahakyan
    BabkenSahakyan
    on Feb 20, 2022
    More actions

    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 @Condition methods seems the only appropriate place to do this checking. With a support of @MappingTarget and @TargetProperty problem will be nicely solved.

    Thank you all.

  18. HemalathaGowdar commented on Mar 4, 2022

    @HemalathaGowdar
    HemalathaGowdar
    on Mar 4, 2022
    Author
    More actions

    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

  19. filiphr commented on Mar 6, 2022

    @filiphr
    filiphr
    on Mar 6, 2022
    Member
    More actions

    @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.

  20. ivlcic
    added a commit that references this issue on Apr 28, 2022

    Implemented and fixed:

    2f15166
  21. ivlcic
    added a commit that references this issue on May 30, 2022

    mapstruct#2688: Access to the target property name.

    cece0df
  22. filiphr
    added this to the 1.6.0 milestone on May 30, 2022
  23. filiphr
    added a commit that references this issue on Aug 1, 2022

    mapstruct#2688: Support accessing to the target property name

    8fa286f
  24. filiphr commented on Aug 1, 2022

    @filiphr
    filiphr
    on Aug 1, 2022
    Member
    More actions

    Thanks for your work on this @ivlcic. This has been integrated into main.

    Reacted by BabkenSahakyan
  25. filiphr
    closed this as completedon Aug 1, 2022
  26. FlorianCassayre commented on Nov 11, 2022

    @FlorianCassayre
    FlorianCassayre
    on Nov 11, 2022
    More actions

    This seems to correspond to what our project is needing. Is there a planned date for the release of 1.6.0 (even a release candidate)?

  27. filiphr commented on Nov 12, 2022

    @filiphr
    filiphr
    on Nov 12, 2022
    Member
    More actions

    @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

  28. FlorianCassayre commented on Nov 12, 2022

    @FlorianCassayre
    FlorianCassayre
    on Nov 12, 2022
    More actions

    Thanks for the reply, I understand; we will follow that closely and will be looking forward to that beta release.

  29. ivlcic commented on May 16, 2023

    @ivlcic
    ivlcic
    on May 16, 2023
    Contributor
    More actions

    Hi guys how's 1.6.0 progressing?

  30. filiphr commented on May 20, 2023

    @filiphr
    filiphr
    on May 20, 2023
    Member
    More actions

    @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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      • 1.6.0.Beta1

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.