Mapping from Object to primitive

1,199 views
Skip to first unread message

George Pai

unread,
Oct 28, 2015, 12:10:01 PM10/28/15
to mapstruct-users
Checking to see if I'm overlooking something.  In my User I have a set of Roles, however in my User view model I have a set of Strings that are the role identifiers.  This is what I basically have now (minus irrelevant and implicit code)

public class User {
   
private Set<Role> roles;
}


public class Role {
   
private String name;
}


public class UserViewModel {
   
private Set<String> roles;
}


@Mapper(uses = "RoleMapper.class")
public interface UserMapper {
   
UserViewModel userToUserViewModel(User user);
}


@Mapper
public abstract class RoleMapper {
   
public String roleToRoleName(Role role) {
       
return role.getName();
   
}
}


Is there an annotation or something I can use instead of explicitly writing out the code?  This is more for curiosity sake than anything else (since the implementation is trivial, and probably refactors better than any annotation would)

Andreas Gudian

unread,
Oct 28, 2015, 2:55:30 PM10/28/15
to George Pai, mapstruct-users
Hi George,

for that particular case (picking a property of a collection element type), we don't have an annotation. For the use case you demonstrate, the suggested solution is exactly the one you chose: add the method yourself.

Generally, we want MapStruct to help people solve the 80 - 90 percent of their mapping problems, and allow them to fill in the gaps themselves manually, with plain Java... :-)

Andreas

--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gunnar Morling

unread,
Oct 29, 2015, 3:36:12 AM10/29/15
to mapstruct-users, geo...@megalobrainiac.com
Hi,

As Andreas is saying, your chosen approach is the "right" one.

We have some vague ideas for supporting an actual expression language (in addition to Java) in expression() values. For collection mappings one then could envision a projection operator which would allow to define the required mapping inline e.g. like this (syntax inspired by Spring EL's projection operator):

    @IterableMapping(expression="![name]")
    Set<String> rolesToStrings(Set<Role> roles);

But that's just an idea at this point which needs much more thinking and actual implementation :)


Am Mittwoch, 28. Oktober 2015 19:55:30 UTC+1 schrieb Andreas Gudian:
Hi George,

for that particular case (picking a property of a collection element type), we don't have an annotation. For the use case you demonstrate, the suggested solution is exactly the one you chose: add the method yourself.

Generally, we want MapStruct to help people solve the 80 - 90 percent of their mapping problems, and allow them to fill in the gaps themselves manually, with plain Java... :-)

Andreas
2015-10-28 17:10 GMT+01:00 George Pai <g...m>:
Checking to see if I'm overlooking something.  In my User I have a set of Roles, however in my User view model I have a set of Strings that are the role identifiers.  This is what I basically have now (minus irrelevant and implicit code)

public class User {
   
private Set<Role> roles;
}


public class Role {
   
private String name;
}


public class UserViewModel {
   
private Set<String> roles;
}


@Mapper(uses = "RoleMapper.class")
public interface UserMapper {
   
UserViewModel userToUserViewModel(User user);
}


@Mapper
public abstract class RoleMapper {
   
public String roleToRoleName(Role role) {
       
return role.getName();
   
}
}


Is there an annotation or something I can use instead of explicitly writing out the code?  This is more for curiosity sake than anything else (since the implementation is trivial, and probably refactors better than any annotation would)

--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-users+unsubscribe@googlegroups.com.
To post to this group, send email to mapstruct-users@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages