Edmondo
Edmondo

Reputation: 20080

Domain classes and slick generated case classes conversion with Shapeless

Slick codegen generates an handy Tables.scala with all the classes for the rows and the tables. We like this approach because we can use relational design and exploit the full power of the SQL dialect we choose.

However, we are writing a lot of boiler plate code to convert from our REST API Model / Domain API Model to slick XYZ row for the following reasons:

  1. There are additional parameters in the Slick case class than in the domain case class (these additional parameters are extracted, for example, from the path of the REST resource)
  2. We would not like to expose path-dependent types to other layers / microservices. They only need to know the REST API model
  3. We typically convert date and time classes from java.sql to java.time equivalent

Can we automatize the conversion so we do not have to write explicitely every time two conversion functions, from domain model to slick case classes and viceversa. In particular:

  1. How do write a generic function that provides an implicit conversion between two equivalent case classes with the same HLIST representation?

  2. How do we write a generic implicit conversion between two case classes with a different HLIST representation, provided a set of implicit conversions in scope for the different members (java.sql.Timestamp -> java.time.ZonedDateTime)

  3. How do we write a function to convert between two case classes where the output one has a Repr = HList1 :: HList2 and the first one has a Repr = HList2 (i.e. how do we prepend / append to HList2 the parameters extracted from the request?)

Upvotes: 2

Views: 260

Answers (1)

Shane Delmore
Shane Delmore

Reputation: 1575

There are a handful of libraries that do this which you could use or read the source to learn from. For example https://index.scala-lang.org/davegurnell/bulletin/bulletin/0.7.0

Upvotes: 2

Related Questions