theiterators / kebs

Scala library to eliminate boilerplate
MIT License
155 stars 15 forks source link

Fix slick's proven shape problems #6

Open luksow opened 7 years ago

luksow commented 7 years ago
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?

marcin-rzeznicki commented 7 years ago

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

luksow commented 7 years ago

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.