PlayEnum

enumeratum.PlayEnum
trait PlayEnum[A <: EnumEntry] extends Enum[A], PlayJsonEnum[A], PlayPathBindableEnum[A], PlayQueryBindableEnum[A], PlayFormFieldEnum[A]

An Enum that has a lot of the Play-related implicits built-in so you can avoid boilerplate.

Note, the binders created here are case-sensitive.

Things included are:

  • implicit JSON format
  • implicit PathBindable (for binding from request path)
  • implicit QueryStringBindable (for binding from query strings)
  • formField for doing things like Form("hello" -> MyEnum.formField)

Example:

scala> import enumeratum._
scala> import play.api.data.Form
scala> import play.api.libs.json._

scala> sealed trait Greeting extends EnumEntry

scala> object Greeting extends PlayEnum[Greeting] {
   |   val values = findValues
   |   case object Hello   extends Greeting
   |   case object GoodBye extends Greeting
   |   case object Hi      extends Greeting
   |   case object Bye     extends Greeting
   | }

scala> val form = Form("greeting" -> Greeting.formField)
scala> form.bind(Map("greeting" -> "Hello")).value
res0: Option[Greeting] = Some(Hello)

scala> Json.toJson(Greeting.Hello) res1: JsValue = "Hello"

Attributes

Graph
Supertypes
trait PlayJsonEnum[A]
trait Enum[A]
class Object
trait Matchable
class Any
Show all

Members list

Value members

Inherited methods

def extraNamesToValuesMap: Map[String, A]

Additional list of names which can be mapped to values, for example to allow mapping of legacy values.

Additional list of names which can be mapped to values, for example to allow mapping of legacy values.

Attributes

Returns

a Map of names to Values

Inherited from:
Enum
inline def findValues: IndexedSeq[A]

Returns a Seq of A objects that the macro was able to find.

Returns a Seq of A objects that the macro was able to find.

You will want to use this in some way to implement your values method. In fact, if you aren't using this method... why are you even bothering with this lib?

Attributes

Inherited from:
EnumCompat (hidden)
def indexOf(member: A): Int

Returns the index number of the member passed in the values picked up by this enum

Returns the index number of the member passed in the values picked up by this enum

Value parameters

member

the member you want to check the index of

Attributes

Returns

the index of the first element of values that is equal (as determined by ==) to member, or -1, if none exists.

Inherited from:
Enum
def values: IndexedSeq[A]

The sequence of values for your Enum. You will typically want to implement this in your extending class as a val so that withName and friends are as efficient as possible.

The sequence of values for your Enum. You will typically want to implement this in your extending class as a val so that withName and friends are as efficient as possible.

Feel free to implement this however you'd like (including messing around with ordering, etc) if that fits your needs better.

Attributes

Inherited from:
EnumCompat (hidden)
def withName(name: String): A

Tries to get an A by the supplied name. The name corresponds to the .name of the case objects implementing A

Tries to get an A by the supplied name. The name corresponds to the .name of the case objects implementing A

Like Enumeration 's withName, this method will throw if the name does not match any of the values' .entryName values.

Attributes

Inherited from:
Enum
def withNameEither(name: String): Either[NoSuchMember[A], A]

Returns an [[Right[A]] ] for a given name, or a [[Left[NoSuchMember]] ] if the name does not match any of the values' .entryName values.

Returns an [[Right[A]] ] for a given name, or a [[Left[NoSuchMember]] ] if the name does not match any of the values' .entryName values.

Attributes

Inherited from:
Enum
def withNameInsensitive(name: String): A

Tries to get an A by the supplied name. The name corresponds to the .name of the case objects implementing A, disregarding case

Tries to get an A by the supplied name. The name corresponds to the .name of the case objects implementing A, disregarding case

Like Enumeration 's withName, this method will throw if the name does not match any of the values' .entryName values.

Attributes

Inherited from:
Enum
def withNameInsensitiveEither(name: String): Either[NoSuchMember[A], A]

Returns an [[Right[A]] ] for a given name, or a [[Left[NoSuchMember]] ] if the name does not match any of the values' .entryName values, disregarding case.

Returns an [[Right[A]] ] for a given name, or a [[Left[NoSuchMember]] ] if the name does not match any of the values' .entryName values, disregarding case.

Attributes

Inherited from:
Enum
def withNameInsensitiveOption(name: String): Option[A]

Optionally returns an A for a given name, disregarding case

Optionally returns an A for a given name, disregarding case

Attributes

Inherited from:
Enum
def withNameLowercaseOnly(name: String): A

Tries to get an A by the supplied name. The name corresponds to the .name of the case objects implementing A transformed to lower case

Tries to get an A by the supplied name. The name corresponds to the .name of the case objects implementing A transformed to lower case

Like Enumeration 's withName, this method will throw if the name does not match any of the values' .entryName values.

Attributes

Inherited from:
Enum
def withNameLowercaseOnlyEither(name: String): Either[NoSuchMember[A], A]

Returns an [[Right[A]] ] for a given name, or a [[Left[NoSuchMember]] ] if the name does not match any of the values' .entryName values, disregarding case.

Returns an [[Right[A]] ] for a given name, or a [[Left[NoSuchMember]] ] if the name does not match any of the values' .entryName values, disregarding case.

Attributes

Inherited from:
Enum
def withNameLowercaseOnlyOption(name: String): Option[A]

Optionally returns an A for a given name assuming the value is lower case

Optionally returns an A for a given name assuming the value is lower case

Attributes

Inherited from:
Enum
def withNameOption(name: String): Option[A]

Optionally returns an A for a given name.

Optionally returns an A for a given name.

Attributes

Inherited from:
Enum
def withNameUppercaseOnly(name: String): A

Tries to get an A by the supplied name. The name corresponds to the .name of the case objects implementing A transformed to upper case

Tries to get an A by the supplied name. The name corresponds to the .name of the case objects implementing A transformed to upper case

Like Enumeration 's withName, this method will throw if the name does not match any of the values' .entryName values.

Attributes

Inherited from:
Enum
def withNameUppercaseOnlyEither(name: String): Either[NoSuchMember[A], A]

Returns an [[Right[A]] ] for a given name, or a [[Left[NoSuchMember]] ] if the name does not match any of the values' .entryName values, disregarding case.

Returns an [[Right[A]] ] for a given name, or a [[Left[NoSuchMember]] ] if the name does not match any of the values' .entryName values, disregarding case.

Attributes

Inherited from:
Enum
def withNameUppercaseOnlyOption(name: String): Option[A]

Optionally returns an A for a given name assuming the value is upper case

Optionally returns an A for a given name assuming the value is upper case

Attributes

Inherited from:
Enum

Inherited fields

val formField: Mapping[A]

Form field for this enum

Form field for this enum

Attributes

Inherited from:
PlayFormFieldEnum
lazy val fromPath: PathBindableExtractor[A]

Binder for play.api.routing.sird router

Binder for play.api.routing.sird router

Example:

scala> import play.api.routing.sird._
scala> import play.api.routing._
scala> import play.api.mvc._
scala> import enumeratum._

scala> sealed trait Greeting extends EnumEntry

scala> object Greeting extends PlayEnum[Greeting] {
   |   val values = findValues
   |   case object Hello   extends Greeting
   |   case object GoodBye extends Greeting
   |   case object Hi      extends Greeting
   |   case object Bye     extends Greeting
   | }

scala> val router = Router.from {
   |   case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action {
   |     Results.Ok(s"$greeting")
   |   }
   | }
scala> router.routes
res0: Router.Routes = <function1>

Attributes

Inherited from:
PlayPathBindableEnum
final lazy val lowerCaseNamesToValuesMap: Map[String, A]

Map of A object names in lower case to A s for case-insensitive comparison

Map of A object names in lower case to A s for case-insensitive comparison

Attributes

Inherited from:
Enum
lazy val namesToValuesMap: Map[String, A]

Map of A object names to A s

Map of A object names to A s

Attributes

Inherited from:
Enum
final lazy val upperCaseNameValuesToMap: Map[String, A]

Map of A object names in upper case to A s for case-insensitive comparison

Map of A object names in upper case to A s for case-insensitive comparison

Attributes

Inherited from:
Enum
final lazy val valuesToIndex: Map[A, Int]

Map of A to their index in the values sequence.

Map of A to their index in the values sequence.

A performance optimisation so that indexOf can be found in constant time.

Attributes

Inherited from:
Enum

Implicits

Inherited implicits

implicit def contraJsonWrites[B <: A]: Writes[B]

Attributes

Inherited from:
PlayJsonEnum
implicit def contraKeyWrites[K <: A]: KeyWrites[K]

Attributes

Inherited from:
PlayJsonEnum
implicit val jsonFormat: Format[A]

Attributes

Inherited from:
PlayJsonEnum
implicit val keyReads: KeyReads[A]

Attributes

Inherited from:
PlayJsonEnum
implicit val keyWrites: KeyWrites[A]

Attributes

Inherited from:
PlayJsonEnum
implicit val pathBindable: PathBindable[A]

Implicit path binder for Play's default router

Implicit path binder for Play's default router

Attributes

Inherited from:
PlayPathBindableEnum
implicit val queryBindable: QueryStringBindable[A]

Attributes

Inherited from:
PlayQueryBindableEnum