If you are used to implement clean architecture in your project you will know that the mappers are a very boring part to write. The mappers are needed to send data across boundaries and you need to be careful while you are writing it, because if you forget to map any field the data will not be displayed.
I want to share how I create it, it´s just a trick but I think will be helpful for almost all if you create them manually.
- Go to the Object model that you need to map, copy and past to a sublime text editor
- In sublime, search for occurrences with the regex flag activated
- Look for “set\w+” and press Alt + Enter, now you can see all the matches of the setter methods selected:
- Press CMD + C or CTRL + C if you are in Linux/Win, return to your IDE and paste the contents
- With the alt key pressend drag the cursor to start the multi-selection feature, and set the caret at the start of the line
- Write “object.”
- Press CMD + -> or End key to go to the finish of the line. write “(otherObject.”
- Return to Sublime and repeat the previous process but for the getters, so look for: “(get|is)\w+\(\)”, press alt + enter, copy and return to the IDE and paste the contents
- Press CMD + -> or End key to go at the end and write “;”
- Go to Code -> Reformat Code to format all the code and complete the rest of the method.
Done! You completed your mapper, What you won?
– You don’t forget any of the fields
– Is less boring than write all the fields one by one