如何在 phantom dsl 中模拟枚举类型?

How to model enum types in phantom dsl?

我的案例 class 包含如下枚举参数:

case class User(role: UserRole.UserRole, name: String)

object UserRole extends Enumeration {
  type UserRole = Value
  val ADMIN, USER = Value
}

如何像 this example 中那样对这种情况进行建模?

提供的任何代码示例都会有所帮助。

您需要使用 EnumColumn,它正是出于这个原因而创建的。如果你想使用枚举作为键,那么你还需要使用默认的辅助方法创建一个原语。

您可以使用两种方式来定义枚举。

object Records extends Enumeration {
  type Records = Value
  val TypeOne, TypeTwo, TypeThree = Value
}

object NamedRecords extends Enumeration {
  type NamedRecords = Value
  val One = Value("one")
  val Two = Value("two")
}

object enum extends EnumColumn[Records.type](this, Records)

在你的情况下,这将是:

object role extends EnumColumn[UserRole.type](this, UserRole)

要将其用作索引,您需要:

implicit val userRolePrimitive = Primitive(UserRole)

更新 自 Phantom 2.0.0+

object role extends EnumColumn[UserRole](this)

您不需要定义任何额外的隐式,现在原生支持枚举作为索引。