我在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
)
}
和光滑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
无法为特征定义ClassTag类型。有人知道如何解决它吗?
我为此准备了示例应用: https://github.com/mgosk/slick-test