Avoid creating almost identical case classes

239 Views Asked by At

often running into this situation

slick generated case classes

case class Person(firstname:String,lastname:String)
case class Address(zip:String,city:String,state:String)

commonly front end JSON contract has Person along with Address. traditionally requiring to create a class as

case class PersonWithAddress(firstname:String,lastname:String,address:Address)

Such requirement is common across the boards.

Its bit cumbersome to create and maintain such placeholder classes. Any nifty tricks can help create such data structure on the fly which can be JSON friendly. Is there a case for using Shapeless here ? (note: JSON transformations are happening using playframework JSON lib)

1

There are 1 best solutions below

0
On

You could define your JSON case class as case class PersonWithAddress(person: Person, address:Address) but then you'd need to write a custom serializer if you wanted to flatten the first name and last name (instead of leaving them nested inside a sub document).

That's probably more code than simply repeating a case class, but at least you'd get some compile-time checking if the underlying case classes were to change (which you don't have with copy/pasted case classes).