scalaenumscassandra-2.1phantom-dsl

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 this example?

Any code samples provided will be helpful.


Solution

  • 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)
    

    Update As of Phantom 2.0.0+

    object role extends EnumColumn[UserRole](this)
    

    You don't need to define any additional implicits, Enums are now natively suported as indexes.