I am writing an HTTP REST API, and I want strongly typed model classes in Scala, for example. if I have a Car model, I want to create the following RESTful /car API:
1) For POST (create a new car):
case class Car(manufacturer: String, name: String, year: Int)
2) For PUT (edit an existing car) and GET s, I want to also mark the id tag:
case class Car(id: Long, manufacturer: String, name: String, year: Int)
3) For PATCH es (partial editing of an existing car) I need this partial object:
case class Car(id: Long, manufacturer: Option[String], name: Option[String], year: Option[Int])
But saving the three models is essentially the same thing is redundant and error prone (for example, if I edit one model, I need to remember other models).
Is there a prominent way to support all 3 models? I am fine with answers that also use macros.
I managed to combine the first two, as shown below.
trait Id { val id: Long } type PersistedCar = Car with Id
source share