Create a mapper easily

  2 mins read

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.

  1. Go to the Object model that you need to map, copy and past to a sublime text editor
  2. In sublime, search for occurrences with the regex flag activated
  3. regex

  4. Look for “set\w+” and press Alt + Enter, now you can see all the matches of the setter methods selected:
  5. setters selected

  6. Press CMD + C or CTRL + C if you are in Linux/Win, return to your IDE and paste the contents
  7. With the alt key pressend drag the cursor to start the multi-selection feature, and set the caret at the start of the line
  8. Write “object.”
  9. caret at start

  10. Press CMD + -> or End key to go to the finish of the line. write “(otherObject.”
  11. 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
  12. paste geters

  13. Press CMD + -> or End key to go at the end and write “;”
  14. Go to Code -> Reformat Code to format all the code and complete the rest of the method.
  15. mapper done

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

Written by:

Christian Panadero Martinez