2

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.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
PainPoints
  • 461
  • 8
  • 20
  • I see, IntelliJ Idea was showing errors on the below line while the code compiles. object role extends EnumColumn(this, UserRole) { override lazy val name = "role" } So, it works. – PainPoints Sep 12 '16 at 23:00

1 Answers1

4

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.

flavian
  • 28,161
  • 11
  • 65
  • 105
  • 1
    If the github example could be updated with all the possible scenarios of modelling data using phantom, it would be very helpful for us all. Great work, thanks Flavian. – PainPoints Sep 13 '16 at 23:22
  • 1
    Nice Answer! But got the working example from here - https://github.com/outworkers/phantom/blob/d8afd1b34ebe514673e346041fb30223caadf469/phantom-dsl/src/test/scala/com/outworkers/phantom/tables/BasicTable.scala#L68 – himanshuIIITian Apr 06 '18 at 06:36