Enumeratum 1.4.0: ValueEnums and Circe integration

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Enumeratum 1.4.0: ValueEnums and Circe integration

Lloyd C
It’s been a while since the last major release of Enumeratum, and in 1.4.0, minor changes include Play 2.5 support, integration library version bumps, and small internal refactorings. 

More excitingly though, the latest version adds support for a new kind of enumeration, ValueEnum, as well as an integration with the Circe JSON library.

Points of interest:
  • Unlike other value enum implementations, Enumeration’s value enums perform uniqueness checks at compile time to make sure you have unique values across your enum members.
  • Circe integration allows you to send and receive JSON data between your front end and your server using the same code
Example:

import enumeratum.values._

sealed abstract class ContentType(val value: Long, name: String) extends LongEnumEntry

case object ContentType extends LongPlayEnum[ContentType] {

  val values
= findValues

 
case object Text extends ContentType(value = 1L, name = "text")
 
case object Image extends ContentType(value = 2L, name = "image")
 
case object Video extends ContentType(value = 3L, name = "video")
 
case object Audio extends ContentType(value = 4L, name = "audio")

 
/* case object Sticker extends ContentType(value = 4L, name = "sticker")
   *   => Fails at compile time because 4L is already used with the following error:
   *
   *   It does not look like you have unique values. Found the following values correspond to more than one members: Map(4 -> List(object Audio, object Sticker))
   */


}

assert(ContentType.withValue(1) == ContentType.Text)

ContentType.withValue(10) // => java.util.NoSuchElementException:

// Use with Play-JSON
import play.api.libs.json.{ JsNumber, JsString, Json => PlayJson }
ContentType.values.foreach { item =>
   
assert(PlayJson.toJson(item) == JsNumber(item.value))
}


For more information, checkout:
As always, if you have any problems, questions, suggestions, or better yet, PRs, please do not hesitate to get in touch on Github.

Lloyd

--
You received this message because you are subscribed to the Google Groups "scala-announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.