3

我在 Play 应用程序中从 Slick 1.0 升级到 Slick 2.0 时遇到问题。

在我的应用程序中,我有一些用户定义的类型示例:

sealed trait UserStatus

case object NewUser extends UserStatus
case object ActiveUser extends UserStatus
case object BlockedUser extends UserStatus

object UserStatus extends SerializableEnum[UserStatus] {
  def mapping = Map[String, UserStatus](
    "NEW" -> NewUser,
    "ACTIVE" -> ActiveUser,
    "BLOCKED" -> BlockedUser
  )
}

和 slick 1.0 的通用映射器:

trait SerializableEnum[T] {
  def mapping: Map[String, T]

  def reverseMapping = mapping.map(_.swap)

  implicit def enumWrites = new Writes[T] {
    def writes(o: T): JsValue = JsString(reverseMapping(o))
  }

  implicit val enumReads = new Reads[T] {
    def reads(json: JsValue): JsResult[T] = json match {
      case JsString(s) => JsSuccess(mapping(s))
      case _ => JsError("Enum type should be of proper type")
    }
  }
  implicit val enumTypeMapper = MappedTypeMapper.base[T, String](reverseMapping, mapping)
}

迁移到 Slick 2.0 后,新的映射器不起作用:

trait SerializableEnum[T] {
  def mapping: Map[String, T]

  def reverseMapping:Map[T,String] = mapping.map(_.swap)

  implicit def enumWrites = new Writes[T] {
    def writes(o: T): JsValue = JsString(reverseMapping(o))
  }

  implicit val enumReads = new Reads[T] {
    def reads(json: JsValue): JsResult[T] = json match {
      case JsString(s) => JsSuccess(mapping(s))
      case _ => JsError("Enum type should be of proper type")
    }
  }
  implicit val enumTypeMapper = MappedColumnType.base[T, String](reverseMapping, mapping)
}

编译器说:

No ClassTag available for T

无法为 trait 定义 ClassTag 类型。有人知道如何解决吗?

我为此准备了示例应用程序: https ://github.com/mgosk/slick-test

4

0 回答 0