Re-imagining mapstruct in D
Mapstruct is a Java library that makes it easy to map one type to another. I've been using it at work to map DTOs to JPA entities and vice versa. While it might seem unnecessary, it's one of those things I adopted just in case our REST responses deviate from our database structure. I won't go into details as to why one should or shouldn't use Mapstruct because it's beyond the scope of this article. What I want to talk about, instead, is the port itself. Motivation One of mapstruct's selling points is that it generates mappings at compile time. I simply write an interface such as this one: @Mapper(componentModel = "spring") public interface Mapper { @Mapping UserDTO toUserDTO(User entity); } With User and UserDTO being: class User { long id; String username; String password; //getters and setters } class UserDTO { long id; String username; //getters and setters } Then after building the project, an im