I have this example code:
import java.util.UUID
import shapeless.LabelledGeneric
import shapeless.record._
import shapeless.syntax.singleton._
object LabelTest extends App {
case class IncomingThing(name: String, age: Int)
case class DatabaseIncomingThing(name: String, age: Int, id: UUID)
val genIncoming = LabelledGeneric[IncomingThing]
val genDatabase = LabelledGeneric[DatabaseIncomingThing]
val thing = IncomingThing("John", 42)
val structuralVersionOfIncomingThing = genIncoming.to(thing)
val updated = genDatabase.from(structuralVersionOfIncomingThing + ('id ->> UUID.randomUUID()))
println(updated) // DatabaseIncomingThing(John,42,a45081f2-4ed5-4d2b-8fd9-4d8986875ed7)
}
Which is nice because I don't have to write the boilerplate that copies over all the fields from IncomingThing
to DatabaseIncomingThing
. However, I would love not having to maintain both those types since there is a very clear relationship between the two (one has id
, the other doesn't).
Is there a way to create a type from a given case class by either adding or removing one field? I imagine something like
type IncomingThing = withoutField[DatabaseIncomingThing]('id)
Or something to that effect.
Instead of
DatabaseIncomingThing
you can work with raw
HList
On type level
You can create your type class
and use it