kebs icon indicating copy to clipboard operation
kebs copied to clipboard

Fix slick's proven shape problems

Open luksow opened this issue 7 years ago • 2 comments

case class Member(id: UUID, name: String)

object Member {
  
}

class Members(tag: BaseTable.Tag) extends BaseTable[Member](tag, "position") {
  import driver.api._

  def id: Rep[UUID]                             = column[UUID]("id")
  def name: Rep[String]                             = column[String]("name")

  override def * : ProvenShape[Member] =
    (id, name) <> (Member.tupled, Member.unapply)
}

This does not compile for obvious reasons and needs a change to (id, name) <> ((Member.apply _).tupled, Member.unapply).

Even worse, let's consider this:

case class MemberCreateRequest(name: String)

case class Member(id: UUID, name: String)

object Member {
  def apply(request: MemberCreateRequest): Member = Member(UUID.randomUUID(), request.name)
}

class Members(tag: BaseTable.Tag) extends BaseTable[Member](tag, "position") {
  import driver.api._

  def id: Rep[UUID]                             = column[UUID]("id")
  def name: Rep[String]                             = column[String]("name")

  override def * : ProvenShape[Member] =
    (id, name) <> ((Member.apply _).tupled, Member.unapply)
}

It won't compile again due to ambiguous reference.

Can kebs help with that?

luksow avatar May 19 '17 16:05 luksow

I think we can automatically generate * override based on case-class and column structure in the table, if that's what's on your mind. But for this to be complete (eg. no red squiggles in IntelliJ), I believe we should introduce scala-meta based solution. I was thinking of a few cases where we would need scala-meta anyway eg. generation of Free / tagless patterns. Stay tuned

marcin-rzeznicki avatar Jun 20 '17 09:06 marcin-rzeznicki

That would be fancy but the main thing I want to avoid is ambiguity of the second example. It's not a problem for me to write:

override def * : ProvenShape[Member] = (id, name) ~> ohMightKebsPleaseHelp()

Besides that I guess it's nearly impossible to generate that if you have nested case classes in proven shape.

luksow avatar Jun 20 '17 10:06 luksow