将ADT映射到Slick中的多个列

3

我正在尝试使用Slick将继承自“sealed trait”的ADT(“case class”)映射为同一表的多列,例如:

sealed trait OrValue
case class IntValue(value: Int)
case class StringValue(value: String)

case class Thing(id: Int, value: OrValue)

class Things(tag: Tag) extends Table[Thing](tag, "things") {
  def id = column[Int]("id", O.PrimaryKey)
  def intValue = column[Option[Int]]("intValue")
  def stringValue = column[Option[String]]("stringValue")

  def toThing(_id: String, _intValue: Option[Int], _stringValue: Option[String]): Thing = Thing(id, ((_intValue, _stringValue) match {
    case (Some(a), None) => IntValue(a)
    case (None, Some(a)) => StringValue(a)
  }

  def fromThing(t: Thing): (String, Option[Int], Option[String]) = ??? // elided

  def * = (id, intValue, stringValue) <> ((toThing _).tupled, fromThing)
}

这段代码无法编译:

[error]  found   : [U]slick.lifted.MappedProjection[Thing,U]
[error]  required: slick.lifted.ProvenShape[Thing]
[error]            ) <> ((toThing _).tupled, fromThing _)

我是否采取了错误的方法? 表示ADT的惯用方式是什么?

2个回答

1
问题出在方法 toThingfromThing 的签名上。
应该是这样的:
sealed trait OrValue
case class IntValue(value: Int)
case class StringValue(value: String)

case class Thing(id: Int, value: OrValue)

class Things(tag: Tag) extends Table[Thing](tag, "things") {
  def id = column[Int]("id", O.PrimaryKey)
  def intValue = column[Option[Int]]("intValue")
  def stringValue = column[Option[String]]("stringValue")

  def toThing(source: (Int, Option[Int], Option[String])): Thing = source match {
    case (id, intValue, stringValue) => ??? //TODO implement
  }
  def fromThing(t: Thing): Option[(Int, Option[Int], Option[String])] = ??? //TODO implement

  def * = (id, intValue, stringValue) <> (toThing _, fromThing _)
}

-1

你不应该将映射和案例类命名为相同的名称。例如,将map类更改为Things


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接