CodeTest CodeTest - 11 months ago 100
Scala Question

How to model enum types in phantom dsl?

My case class contains enum parameter like as follows:

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

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

How to model this case as in example: ?

Any code samples provided will be helpful.

Answer Source

You need to use EnumColumn, which is created for this very reason. If you want to use the enum as a key, then you also need to create a primitive using the default helper methods.

You can use both flavours of defining an enum.

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)

In your case this would be:

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

To use this as an index, you will need:

implicit val userRolePrimitive = Primitive(UserRole)