In my last post I talked quite a lot about isomorphisms, optics and testing. However you might be thinking, why would I want to do that? Well, if you are implementing an API with a POST request chances are that it is useful for you :)
Let’s imagine that you are receiving information about a person in a POST request. Your swagger file would look like this (only significant parts depicted)
This will be translated into code as the following case class. At Twilio, this process is handled by the open source tool GuardRail
The function defined in the swagger.yaml file to be executed on the PUT request, putPerson, looks like
If you want to store that person information inside your system, chances are that the id and the rest of the information will go together in a class like
Question is, how you convert from one to another? You can of course do it manually, but a way more elegant approach is to define an isomorphism from Person + Id to PersonFull :)
Implementing Property Testing for this is quite simple by using the tools that we saw on the previous post.
As you can see, this technique is not a mere mathematical curiosity but something to be adopted in your code to do real stuff.