publicissapient-france / selma

Selma Java bean mapping that compiles
http://selma-java.org
Apache License 2.0
212 stars 38 forks source link

Page/Pageable and Selma #164

Open surdsey opened 7 years ago

surdsey commented 7 years ago

Hello, I didn't found how to make selma generate converter for spring org.springframework.data.domain.Page. Since method are not generic I didn't found a clean solution to do that without code clones for each mapper translation method. And the collections translation didn't match exactly the Page pruposes. Is there an hidden solution to generate that kind of method? Page asUserPage (Page) Page asUserDtoPage (Page)

Thank you for your help

slemesle commented 7 years ago

Hi, If you need Selma to generate the code for a Spring converter you should probably use an abstract mapper.

@Mapper
public abstract class PageMapper implements Converter<User,UserDto> {

   abstract UserDto asUserDto(User user);
   abstract User asUser(UserDto dto);

   @Override
    public UserDto convert(User entity) {
        return this.asUserDto(entity);
    }

}

Doing this the generated Mapper will also be Spring Converter. So you can use page.map(mapper);

Another way would be to create a new converter calling the mapper. Since Page is an iterator having methods like :

@Mapper
public interface UserDtoPageMapper {
  Page<User> asUserPage (Page<UserDto);
  Page<UserDto> asUserDtoPage (Page<User>);
}

But this one will only work in snapshot build because support for Iterable has been added recently.

I hope this will help

MathieuGilet commented 6 years ago

Hi, It appears that your first solution is not working as Selma generates something like :

@Override
public final UserDto asUserDto(User inUser) {
    UserDto out = null;
    if (inUser != null) {
      out = this.convert(inUser);
    }
    return out;
}

This will create a loop : asUserDto -> convert -> asUserDto - convert ... Is there a way to tell Selma to ignore some method for abstract mappers ?

Thank you