diff --git a/.travis.yml b/.travis.yml index 5e002d80..dfeb84fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,13 +3,23 @@ language: scala jdk: - oraclejdk8 scala: + - 2.10.6 - 2.11.8 + - 2.12.0 script: - - sbt clean +test:compile - - sbt coverage +test coverageReport && sbt coverageAggregate +# Only run aggregation 2.11, for 2.12, only run the special dummy project + - if [[ $TRAVIS_SCALA_VERSION == "2.12"* ]]; then + sbt ++$TRAVIS_SCALA_VERSION scala_2_12/test; + elif [[ $TRAVIS_SCALA_VERSION == "2.11"* ]]; then + sbt ++$TRAVIS_SCALA_VERSION coverage test coverageReport && + sbt ++$TRAVIS_SCALA_VERSION coverageAggregate; + else + sbt ++$TRAVIS_SCALA_VERSION clean test; + fi after_success: - - sbt coveralls - - '[ "${TRAVIS_PULL_REQUEST}" = "false" ] && sbt coreJVM/updateImpactSubmit || true' + - if [[ $TRAVIS_SCALA_VERSION == "2.11"* ]]; then + sbt ++$TRAVIS_SCALA_VERSION coveralls; + fi cache: directories: - $HOME/.sbt/0.13 diff --git a/build.sbt b/build.sbt index 07c1e616..5e7da22c 100644 --- a/build.sbt +++ b/build.sbt @@ -2,45 +2,65 @@ import com.typesafe.sbt.SbtGit.{GitKeys => git} lazy val theVersion = "1.4.18-SNAPSHOT" lazy val theScalaVersion = "2.11.8" -lazy val scalaVersions = Seq("2.10.6", "2.11.8") +/* + 2.12.0 support is currently defined as a separate project (scala_2_12) for convenience while + integration libraries are still gaining 2.12.0 support + */ +lazy val scalaVersions = Seq("2.10.6", "2.11.8") + +lazy val scalaTestVersion = "3.0.0" + +// Library versions +lazy val reactiveMongoVersion = "0.12.0" +lazy val circeVersion = "0.6.0" +lazy val uPickleVersion = "0.4.3" +lazy val argonautVersion ="6.1" def thePlayVersion(scalaVersion: String) = CrossVersion.partialVersion(scalaVersion) match { case Some((2, scalaMajor)) if scalaMajor >= 11 => "2.5.9" case Some((2, scalaMajor)) if scalaMajor == 10 => "2.4.8" case _ => throw new IllegalArgumentException(s"Unsupported Scala version $scalaVersion") - } -lazy val scalaTestVersion = "3.0.0" -lazy val reactiveMongoVersion = "0.12.0" +} + +lazy val baseProjectRefs = + Seq(macrosJs, macrosJvm, coreJs, coreJvm, coreJVMTests).map(Project.projectToRef) + +lazy val integrationProjectRefs = Seq( + enumeratumPlay, + enumeratumPlayJson, + enumeratumUPickleJs, + enumeratumUPickleJvm, + enumeratumCirceJs, + enumeratumCirceJvm, + enumeratumReactiveMongoBson, + enumeratumArgonaut +).map(Project.projectToRef) lazy val root = Project(id = "enumeratum-root", base = file("."), settings = commonWithPublishSettings) .settings( name := "enumeratum-root", crossScalaVersions := scalaVersions, - crossVersion := CrossVersion.binary - ) - .settings( - git.gitRemoteRepo := "git@github.com:lloydmeta/enumeratum.git" - ) - .settings( + crossVersion := CrossVersion.binary, + git.gitRemoteRepo := "git@github.com:lloydmeta/enumeratum.git", // Do not publish the root project (it just serves as an aggregate) publishArtifact := false, publishLocal := {} ) - .aggregate(macrosJs, - macrosJvm, - coreJs, - coreJvm, - coreJVMTests, - enumeratumPlay, - enumeratumPlayJson, - enumeratumUPickleJs, - enumeratumUPickleJvm, - enumeratumCirceJs, - enumeratumCirceJvm, - enumeratumReactiveMongoBson, - enumeratumArgonaut) + .aggregate(baseProjectRefs ++ integrationProjectRefs: _*) + +lazy val scala_2_12 = Project(id = "scala_2_12", + base = file("scala_2_12"), + settings = commonSettings ++ publishSettings) + .settings(name := "enumeratum-scala_2_12", + scalaVersion := "2.12.0", //not sure if this and below are needed + crossScalaVersions := Seq("2.12.0"), + crossVersion := CrossVersion.binary, + // Do not publish this project (it just serves as an aggregate) + publishArtifact := false, + publishLocal := {}) + .aggregate(baseProjectRefs ++ Seq(enumeratumCirceJs, enumeratumCirceJvm).map(Project.projectToRef): _*) // base plus known 2.12 friendly libs lazy val core = crossProject .crossType(CrossType.Pure) @@ -51,7 +71,6 @@ lazy val core = crossProject .settings(testSettings: _*) .settings(commonWithPublishSettings: _*) .dependsOn(macros) - lazy val coreJs = core.js lazy val coreJvm = core.jvm @@ -133,7 +152,7 @@ lazy val enumeratumUPickle = crossProject else CrossVersion.binary } - Seq(impl.ScalaJSGroupID.withCross("com.lihaoyi", "upickle", cross) % "0.4.1") + Seq(impl.ScalaJSGroupID.withCross("com.lihaoyi", "upickle", cross) % uPickleVersion) } ++ { val additionalMacroDeps = CrossVersion.partialVersion(scalaVersion.value) match { @@ -166,7 +185,7 @@ lazy val enumeratumCirce = crossProject else CrossVersion.binary } - Seq(impl.ScalaJSGroupID.withCross("io.circe", "circe-core", cross) % "0.5.1") + Seq(impl.ScalaJSGroupID.withCross("io.circe", "circe-core", cross) % circeVersion) } ) .settings(testSettings: _*) @@ -180,7 +199,7 @@ lazy val enumeratumArgonaut = settings = commonWithPublishSettings) .settings( libraryDependencies ++= Seq( - "io.argonaut" %% "argonaut" % "6.1" + "io.argonaut" %% "argonaut" % argonautVersion ) ) .settings(testSettings: _*) @@ -190,17 +209,18 @@ lazy val commonSettings = Seq( organization := "com.beachape", version := theVersion, incOptions := incOptions.value.withLogRecompileOnMacro(false), - scalaVersion := theScalaVersion, - scalafmtConfig := Some(file(".scalafmt.conf")) + scalaVersion := theScalaVersion ) ++ - scoverageSettings ++ - reformatOnCompileSettings ++ compilerSettings ++ resolverSettings ++ ideSettings -lazy val commonWithPublishSettings = +lazy val commonSettingsWithTrimmings = commonSettings ++ + scoverageSettings + +lazy val commonWithPublishSettings = + commonSettingsWithTrimmings ++ publishSettings lazy val resolverSettings = Seq( @@ -246,9 +266,9 @@ lazy val publishSettings = Seq( https://beachape.com , - publishTo <<= version { v => + publishTo := { val nexus = "https://oss.sonatype.org/" - if (v.trim.endsWith("SNAPSHOT")) + if (version.value.trim.endsWith("SNAPSHOT")) Some("snapshots" at nexus + "content/repositories/snapshots") else Some("releases" at nexus + "service/local/staging/deploy/maven2") @@ -286,18 +306,7 @@ lazy val benchmarking = publishArtifact := false, publishLocal := {} ) - .dependsOn(macrosJs, - macrosJvm, - coreJs, - coreJvm, - coreJVMTests, - enumeratumPlay, - enumeratumPlayJson, - enumeratumUPickleJs, - enumeratumUPickleJvm, - enumeratumCirceJs, - enumeratumCirceJvm, - enumeratumReactiveMongoBson) + .dependsOn((baseProjectRefs ++ integrationProjectRefs).map(ClasspathDependency(_, None)): _*) .enablePlugins(JmhPlugin) .settings(libraryDependencies += "org.slf4j" % "slf4j-simple" % "1.7.21") @@ -337,6 +346,4 @@ def withCompatUnmanagedSources(jsJvmCrossProject: Boolean, } else { unmanagedMainDirsSetting } -} - -scalafmtConfig := Some(file(".scalafmt.conf")) +} \ No newline at end of file diff --git a/enumeratum-argonaut/src/main/scala/enumeratum/ArgonautEnum.scala b/enumeratum-argonaut/src/main/scala/enumeratum/ArgonautEnum.scala index 8cc544ca..4f938dde 100644 --- a/enumeratum-argonaut/src/main/scala/enumeratum/ArgonautEnum.scala +++ b/enumeratum-argonaut/src/main/scala/enumeratum/ArgonautEnum.scala @@ -3,8 +3,8 @@ package enumeratum import argonaut._ /** - * Created by alonsodomin on 14/10/2016. - */ + * Created by alonsodomin on 14/10/2016. + */ trait ArgonautEnum[A <: EnumEntry] { this: Enum[A] => implicit val argonautEncoder: EncodeJson[A] = Argonauter.encoder(this) diff --git a/enumeratum-argonaut/src/main/scala/enumeratum/Argonauter.scala b/enumeratum-argonaut/src/main/scala/enumeratum/Argonauter.scala index 8b4bbe80..c8791883 100644 --- a/enumeratum-argonaut/src/main/scala/enumeratum/Argonauter.scala +++ b/enumeratum-argonaut/src/main/scala/enumeratum/Argonauter.scala @@ -4,8 +4,8 @@ import argonaut._ import Argonaut._ /** - * Created by alonsodomin on 14/10/2016. - */ + * Created by alonsodomin on 14/10/2016. + */ object Argonauter { private def encoder0[A <: EnumEntry](f: A => String): EncodeJson[A] = @@ -25,7 +25,7 @@ object Argonauter { stringDecoder(cursor).flatMap { enumStr => f(enumStr) match { case Some(a) => okResult(a) - case _ => failResult(s"$enumStr' is not a member of enum $enum", cursor.history) + case _ => failResult(s"$enumStr' is not a member of enum $enum", cursor.history) } } } diff --git a/enumeratum-argonaut/src/main/scala/enumeratum/values/ArgonautValueEnum.scala b/enumeratum-argonaut/src/main/scala/enumeratum/values/ArgonautValueEnum.scala index 2c3c4f78..7f1c3075 100644 --- a/enumeratum-argonaut/src/main/scala/enumeratum/values/ArgonautValueEnum.scala +++ b/enumeratum-argonaut/src/main/scala/enumeratum/values/ArgonautValueEnum.scala @@ -4,8 +4,8 @@ import argonaut._ import Argonaut._ /** - * Created by alonsodomin on 14/10/2016. - */ + * Created by alonsodomin on 14/10/2016. + */ sealed trait ArgonautValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { this: ValueEnum[ValueType, EntryType] => @@ -15,8 +15,8 @@ sealed trait ArgonautValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType] } /** - * ArgonautEnum for IntEnumEntry - */ + * ArgonautEnum for IntEnumEntry + */ trait IntArgonautEnum[EntryType <: IntEnumEntry] extends ArgonautValueEnum[Int, EntryType] { this: ValueEnum[Int, EntryType] => @@ -25,8 +25,8 @@ trait IntArgonautEnum[EntryType <: IntEnumEntry] extends ArgonautValueEnum[Int, } /** - * ArgonautEnum for LongEnumEntry - */ + * ArgonautEnum for LongEnumEntry + */ trait LongArgonautEnum[EntryType <: LongEnumEntry] extends ArgonautValueEnum[Long, EntryType] { this: ValueEnum[Long, EntryType] => @@ -35,8 +35,8 @@ trait LongArgonautEnum[EntryType <: LongEnumEntry] extends ArgonautValueEnum[Lon } /** - * ArgonautEnum for ShortEnumEntry - */ + * ArgonautEnum for ShortEnumEntry + */ trait ShortArgonautEnum[EntryType <: ShortEnumEntry] extends ArgonautValueEnum[Short, EntryType] { this: ValueEnum[Short, EntryType] => @@ -45,8 +45,8 @@ trait ShortArgonautEnum[EntryType <: ShortEnumEntry] extends ArgonautValueEnum[S } /** - * ArgonautEnum for StringEnumEntry - */ + * ArgonautEnum for StringEnumEntry + */ trait StringArgonautEnum[EntryType <: StringEnumEntry] extends ArgonautValueEnum[String, EntryType] { this: ValueEnum[String, EntryType] => @@ -55,8 +55,8 @@ trait StringArgonautEnum[EntryType <: StringEnumEntry] } /** - * ArgonautEnum for CharEnumEntry - */ + * ArgonautEnum for CharEnumEntry + */ trait CharArgonautEnum[EntryType <: CharEnumEntry] extends ArgonautValueEnum[Char, EntryType] { this: ValueEnum[Char, EntryType] => @@ -65,8 +65,8 @@ trait CharArgonautEnum[EntryType <: CharEnumEntry] extends ArgonautValueEnum[Cha } /** - * ArgonautEnum for ByteEnumEntry - */ + * ArgonautEnum for ByteEnumEntry + */ trait ByteArgonautEnum[EntryType <: ByteEnumEntry] extends ArgonautValueEnum[Byte, EntryType] { this: ValueEnum[Byte, EntryType] => diff --git a/enumeratum-argonaut/src/main/scala/enumeratum/values/Argonauter.scala b/enumeratum-argonaut/src/main/scala/enumeratum/values/Argonauter.scala index 2720d1b5..9af51ae7 100644 --- a/enumeratum-argonaut/src/main/scala/enumeratum/values/Argonauter.scala +++ b/enumeratum-argonaut/src/main/scala/enumeratum/values/Argonauter.scala @@ -4,12 +4,13 @@ import argonaut._ import Argonaut._ /** - * Created by alonsodomin on 14/10/2016. - */ + * Created by alonsodomin on 14/10/2016. + */ object Argonauter { def encoder[ValueType: EncodeJson, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType]): EncodeJson[EntryType] = { + enum: ValueEnum[ValueType, EntryType] + ): EncodeJson[EntryType] = { val encodeValue = implicitly[EncodeJson[ValueType]] EncodeJson { entry => encodeValue(entry.value) @@ -17,13 +18,14 @@ object Argonauter { } def decoder[ValueType: DecodeJson, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType]): DecodeJson[EntryType] = { + enum: ValueEnum[ValueType, EntryType] + ): DecodeJson[EntryType] = { val decodeValue = implicitly[DecodeJson[ValueType]] DecodeJson { cursor => decodeValue(cursor).flatMap { value => enum.withValueOpt(value) match { case Some(entry) => okResult(entry) - case _ => failResult(s"$value is not a member of enum $enum", cursor.history) + case _ => failResult(s"$value is not a member of enum $enum", cursor.history) } } } diff --git a/enumeratum-argonaut/src/main/scala/enumeratum/values/package.scala b/enumeratum-argonaut/src/main/scala/enumeratum/values/package.scala index c733d9e3..dd133664 100644 --- a/enumeratum-argonaut/src/main/scala/enumeratum/values/package.scala +++ b/enumeratum-argonaut/src/main/scala/enumeratum/values/package.scala @@ -1,11 +1,11 @@ package enumeratum import argonaut.Argonaut._ -import argonaut.{DecodeJson, EncodeJson} +import argonaut.{ DecodeJson, EncodeJson } /** - * Created by alonsodomin on 15/10/2016. - */ + * Created by alonsodomin on 15/10/2016. + */ package object values { implicit val argonautByteEncoder: EncodeJson[Byte] = EncodeJson { byte => diff --git a/enumeratum-argonaut/src/test/scala/enumeratum/ArgonautSpec.scala b/enumeratum-argonaut/src/test/scala/enumeratum/ArgonautSpec.scala index 32bed574..5094f541 100644 --- a/enumeratum-argonaut/src/test/scala/enumeratum/ArgonautSpec.scala +++ b/enumeratum-argonaut/src/test/scala/enumeratum/ArgonautSpec.scala @@ -1,13 +1,13 @@ package enumeratum -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } import argonaut._ import Argonaut._ /** - * Created by alonsodomin on 14/10/2016. - */ + * Created by alonsodomin on 14/10/2016. + */ class ArgonautSpec extends FunSpec with Matchers { describe("to JSON") { diff --git a/enumeratum-argonaut/src/test/scala/enumeratum/TrafficLight.scala b/enumeratum-argonaut/src/test/scala/enumeratum/TrafficLight.scala index 0b3f4cd0..ed0897c9 100644 --- a/enumeratum-argonaut/src/test/scala/enumeratum/TrafficLight.scala +++ b/enumeratum-argonaut/src/test/scala/enumeratum/TrafficLight.scala @@ -1,13 +1,13 @@ package enumeratum /** - * Created by alonsodomin on 14/10/2016. - */ + * Created by alonsodomin on 14/10/2016. + */ sealed trait TrafficLight extends EnumEntry object TrafficLight extends Enum[TrafficLight] with ArgonautEnum[TrafficLight] { - case object Red extends TrafficLight + case object Red extends TrafficLight case object Yellow extends TrafficLight - case object Green extends TrafficLight + case object Green extends TrafficLight val values = findValues } diff --git a/enumeratum-argonaut/src/test/scala/enumeratum/values/ArgonautValueEnumSpec.scala b/enumeratum-argonaut/src/test/scala/enumeratum/values/ArgonautValueEnumSpec.scala index 6499daa0..717eeedf 100644 --- a/enumeratum-argonaut/src/test/scala/enumeratum/values/ArgonautValueEnumSpec.scala +++ b/enumeratum-argonaut/src/test/scala/enumeratum/values/ArgonautValueEnumSpec.scala @@ -1,12 +1,12 @@ package enumeratum.values -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } import argonaut._ import Argonaut._ /** - * Created by alonsodomin on 14/10/2016. - */ + * Created by alonsodomin on 14/10/2016. + */ class ArgonautValueEnumSpec extends FunSpec with Matchers { testArgonautEnum("LongArgonautEnum", ArgonautMediaType) @@ -16,10 +16,10 @@ class ArgonautValueEnumSpec extends FunSpec with Matchers { testArgonautEnum("StringArgonautEnum", ArgonautHttpMethod) testArgonautEnum("ByteArgonautEnum", ArgonautDigits) - private def testArgonautEnum[ValueType: EncodeJson: DecodeJson, - EntryType <: ValueEnumEntry[ValueType]: EncodeJson: DecodeJson]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType] with ArgonautValueEnum[ValueType, EntryType]): Unit = { + private def testArgonautEnum[ValueType: EncodeJson: DecodeJson, EntryType <: ValueEnumEntry[ValueType]: EncodeJson: DecodeJson]( + enumKind: String, + enum: ValueEnum[ValueType, EntryType] with ArgonautValueEnum[ValueType, EntryType] + ): Unit = { describe(enumKind) { describe("from JSON") { @@ -54,8 +54,8 @@ sealed abstract class ArgonautMediaType(val value: Long, name: String) extends L case object ArgonautMediaType extends LongEnum[ArgonautMediaType] with LongArgonautEnum[ArgonautMediaType] { - case object `text/json` extends ArgonautMediaType(1L, "text/json") - case object `text/html` extends ArgonautMediaType(2L, "text/html") + case object `text/json` extends ArgonautMediaType(1L, "text/json") + case object `text/html` extends ArgonautMediaType(2L, "text/html") case object `application/jpeg` extends ArgonautMediaType(3L, "application/jpeg") val values = findValues @@ -65,12 +65,12 @@ sealed abstract class ArgonautJsonLibs(val value: Int) extends IntEnumEntry case object ArgonautJsonLibs extends IntEnum[ArgonautJsonLibs] with IntArgonautEnum[ArgonautJsonLibs] { - case object Json4s extends ArgonautJsonLibs(1) - case object Argonaut extends ArgonautJsonLibs(2) - case object Circe extends ArgonautJsonLibs(3) - case object PlayJson extends ArgonautJsonLibs(4) + case object Json4s extends ArgonautJsonLibs(1) + case object Argonaut extends ArgonautJsonLibs(2) + case object Circe extends ArgonautJsonLibs(3) + case object PlayJson extends ArgonautJsonLibs(4) case object SprayJson extends ArgonautJsonLibs(5) - case object UPickle extends ArgonautJsonLibs(6) + case object UPickle extends ArgonautJsonLibs(6) val values = findValues } @@ -79,10 +79,10 @@ sealed abstract class ArgonautDevice(val value: Short) extends ShortEnumEntry case object ArgonautDevice extends ShortEnum[ArgonautDevice] with ShortArgonautEnum[ArgonautDevice] { - case object Phone extends ArgonautDevice(1) - case object Laptop extends ArgonautDevice(2) + case object Phone extends ArgonautDevice(1) + case object Laptop extends ArgonautDevice(2) case object Desktop extends ArgonautDevice(3) - case object Tablet extends ArgonautDevice(4) + case object Tablet extends ArgonautDevice(4) val values = findValues } @@ -91,8 +91,8 @@ sealed abstract class ArgonautHttpMethod(val value: String) extends StringEnumEn case object ArgonautHttpMethod extends StringEnum[ArgonautHttpMethod] with StringArgonautEnum[ArgonautHttpMethod] { - case object Get extends ArgonautHttpMethod("GET") - case object Put extends ArgonautHttpMethod("PUT") + case object Get extends ArgonautHttpMethod("GET") + case object Put extends ArgonautHttpMethod("PUT") case object Post extends ArgonautHttpMethod("POST") val values = findValues @@ -100,7 +100,7 @@ case object ArgonautHttpMethod sealed abstract class ArgonautBool(val value: Char) extends CharEnumEntry case object ArgonautBool extends CharEnum[ArgonautBool] with CharArgonautEnum[ArgonautBool] { - case object True extends ArgonautBool('T') + case object True extends ArgonautBool('T') case object False extends ArgonautBool('F') case object Maybe extends ArgonautBool('?') @@ -109,8 +109,8 @@ case object ArgonautBool extends CharEnum[ArgonautBool] with CharArgonautEnum[Ar sealed abstract class ArgonautDigits(val value: Byte) extends ByteEnumEntry case object ArgonautDigits extends ByteEnum[ArgonautDigits] with ByteArgonautEnum[ArgonautDigits] { - case object Uno extends ArgonautDigits(1) - case object Dos extends ArgonautDigits(2) + case object Uno extends ArgonautDigits(1) + case object Dos extends ArgonautDigits(2) case object Tres extends ArgonautDigits(3) val values = findValues diff --git a/enumeratum-circe/src/main/scala/enumeratum/Circe.scala b/enumeratum-circe/src/main/scala/enumeratum/Circe.scala index 21434db1..73e04f98 100644 --- a/enumeratum-circe/src/main/scala/enumeratum/Circe.scala +++ b/enumeratum-circe/src/main/scala/enumeratum/Circe.scala @@ -1,19 +1,19 @@ package enumeratum -import cats.data.Xor +import cats.syntax.either._ import io.circe.Decoder.Result -import io.circe._ +import io.circe.{ Encoder, Decoder, Json, HCursor, DecodingFailure } /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ object Circe { /** - * Returns an Encoder for the given enum - */ + * Returns an Encoder for the given enum + */ def encoder[A <: EnumEntry](enum: Enum[A]): Encoder[A] = new Encoder[A] { final def apply(a: A): Json = stringEncoder.apply(a.entryName) } @@ -31,15 +31,15 @@ object Circe { } /** - * Returns a Decoder for the given enum - */ + * Returns a Decoder for the given enum + */ def decoder[A <: EnumEntry](enum: Enum[A]): Decoder[A] = new Decoder[A] { final def apply(c: HCursor): Result[A] = stringDecoder.apply(c).flatMap { s => val maybeMember = enum.withNameOption(s) maybeMember match { - case Some(member) => Xor.right(member) + case Some(member) => Right(member) case _ => - Xor.left(DecodingFailure(s"$s' is not a member of enum $enum", c.history)) + Left(DecodingFailure(s"$s' is not a member of enum $enum", c.history)) } } } @@ -49,9 +49,9 @@ object Circe { final def apply(c: HCursor): Result[A] = stringDecoder.apply(c).flatMap { s => val maybeMember = enum.withNameLowercaseOnlyOption(s) maybeMember match { - case Some(member) => Xor.right(member) + case Some(member) => Right(member) case _ => - Xor.left(DecodingFailure(s"$s' is not a member of enum $enum", c.history)) + Left(DecodingFailure(s"$s' is not a member of enum $enum", c.history)) } } } @@ -61,9 +61,9 @@ object Circe { final def apply(c: HCursor): Result[A] = stringDecoder.apply(c).flatMap { s => val maybeMember = enum.withNameUppercaseOnlyOption(s) maybeMember match { - case Some(member) => Xor.right(member) + case Some(member) => Right(member) case _ => - Xor.left(DecodingFailure(s"$s' is not a member of enum $enum", c.history)) + Left(DecodingFailure(s"$s' is not a member of enum $enum", c.history)) } } } diff --git a/enumeratum-circe/src/main/scala/enumeratum/CirceEnum.scala b/enumeratum-circe/src/main/scala/enumeratum/CirceEnum.scala index 3926e1a5..e4937180 100644 --- a/enumeratum-circe/src/main/scala/enumeratum/CirceEnum.scala +++ b/enumeratum-circe/src/main/scala/enumeratum/CirceEnum.scala @@ -1,24 +1,24 @@ package enumeratum -import io.circe.{Decoder, Encoder} +import io.circe.{ Decoder, Encoder } /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ /** - * Helper trait that adds implicit Circe encoders and decoders for an [[Enum]]'s members - */ + * Helper trait that adds implicit Circe encoders and decoders for an [[Enum]]'s members + */ trait CirceEnum[A <: EnumEntry] { this: Enum[A] => /** - * Implicit Encoder for this enum - */ + * Implicit Encoder for this enum + */ implicit val circeEncoder: Encoder[A] = Circe.encoder(this) /** - * Implicit Decoder for this enum - */ + * Implicit Decoder for this enum + */ implicit val circeDecoder: Decoder[A] = Circe.decoder(this) } diff --git a/enumeratum-circe/src/main/scala/enumeratum/values/Circe.scala b/enumeratum-circe/src/main/scala/enumeratum/values/Circe.scala index 42b7dfb3..7c76abad 100644 --- a/enumeratum-circe/src/main/scala/enumeratum/values/Circe.scala +++ b/enumeratum-circe/src/main/scala/enumeratum/values/Circe.scala @@ -1,41 +1,43 @@ package enumeratum.values -import cats.data.Xor +import cats.syntax.either._ import io.circe.Decoder.Result import io.circe._ /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ object Circe { /** - * Returns an Encoder for the provided ValueEnum - */ + * Returns an Encoder for the provided ValueEnum + */ def encoder[ValueType: Encoder, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType]): Encoder[EntryType] = { + enum: ValueEnum[ValueType, EntryType] + ): Encoder[EntryType] = { new Encoder[EntryType] { - private val valueEncoder = implicitly[Encoder[ValueType]] + private val valueEncoder = implicitly[Encoder[ValueType]] def apply(a: EntryType): Json = valueEncoder.apply(a.value) } } /** - * Returns a Decoder for the provided ValueEnum - */ + * Returns a Decoder for the provided ValueEnum + */ def decoder[ValueType: Decoder, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType]): Decoder[EntryType] = { + enum: ValueEnum[ValueType, EntryType] + ): Decoder[EntryType] = { new Decoder[EntryType] { private val valueDecoder = implicitly[Decoder[ValueType]] def apply(c: HCursor): Result[EntryType] = valueDecoder.apply(c).flatMap { v => val maybeBound: Option[EntryType] = enum.withValueOpt(v) maybeBound match { - case Some(member) => Xor.Right(member) + case Some(member) => Right(member) case _ => - Xor.Left(DecodingFailure(s"$v is not a member of enum $enum", c.history)) + Left(DecodingFailure(s"$v is not a member of enum $enum", c.history)) } } } diff --git a/enumeratum-circe/src/main/scala/enumeratum/values/CirceValueEnum.scala b/enumeratum-circe/src/main/scala/enumeratum/values/CirceValueEnum.scala index b07bfb29..0c2a4d5f 100644 --- a/enumeratum-circe/src/main/scala/enumeratum/values/CirceValueEnum.scala +++ b/enumeratum-circe/src/main/scala/enumeratum/values/CirceValueEnum.scala @@ -1,29 +1,29 @@ package enumeratum.values -import io.circe.{Decoder, Encoder} +import io.circe.{ Decoder, Encoder } /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ sealed trait CirceValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { this: ValueEnum[ValueType, EntryType] => /** - * Implicit Encoder for this enum - */ + * Implicit Encoder for this enum + */ implicit def circeEncoder: Encoder[EntryType] /** - * Implicit Decoder for this enum - */ + * Implicit Decoder for this enum + */ implicit def circeDecoder: Decoder[EntryType] } /** - * CirceEnum for IntEnumEntry - */ + * CirceEnum for IntEnumEntry + */ trait IntCirceEnum[EntryType <: IntEnumEntry] extends CirceValueEnum[Int, EntryType] { this: ValueEnum[Int, EntryType] => implicit val circeEncoder = Circe.encoder(this) @@ -31,8 +31,8 @@ trait IntCirceEnum[EntryType <: IntEnumEntry] extends CirceValueEnum[Int, EntryT } /** - * CirceEnum for LongEnumEntry - */ + * CirceEnum for LongEnumEntry + */ trait LongCirceEnum[EntryType <: LongEnumEntry] extends CirceValueEnum[Long, EntryType] { this: ValueEnum[Long, EntryType] => implicit val circeEncoder = Circe.encoder(this) @@ -40,8 +40,8 @@ trait LongCirceEnum[EntryType <: LongEnumEntry] extends CirceValueEnum[Long, Ent } /** - * CirceEnum for ShortEnumEntry - */ + * CirceEnum for ShortEnumEntry + */ trait ShortCirceEnum[EntryType <: ShortEnumEntry] extends CirceValueEnum[Short, EntryType] { this: ValueEnum[Short, EntryType] => implicit val circeEncoder = Circe.encoder(this) @@ -49,8 +49,8 @@ trait ShortCirceEnum[EntryType <: ShortEnumEntry] extends CirceValueEnum[Short, } /** - * CirceEnum for StringEnumEntry - */ + * CirceEnum for StringEnumEntry + */ trait StringCirceEnum[EntryType <: StringEnumEntry] extends CirceValueEnum[String, EntryType] { this: ValueEnum[String, EntryType] => implicit val circeEncoder = Circe.encoder(this) @@ -58,8 +58,8 @@ trait StringCirceEnum[EntryType <: StringEnumEntry] extends CirceValueEnum[Strin } /** - * CirceEnum for CharEnumEntry - */ + * CirceEnum for CharEnumEntry + */ trait CharCirceEnum[EntryType <: CharEnumEntry] extends CirceValueEnum[Char, EntryType] { this: ValueEnum[Char, EntryType] => implicit val circeEncoder = Circe.encoder(this) @@ -67,8 +67,8 @@ trait CharCirceEnum[EntryType <: CharEnumEntry] extends CirceValueEnum[Char, Ent } /** - * CirceEnum for ByteEnumEntry - */ + * CirceEnum for ByteEnumEntry + */ trait ByteCirceEnum[EntryType <: ByteEnumEntry] extends CirceValueEnum[Byte, EntryType] { this: ValueEnum[Byte, EntryType] => implicit val circeEncoder = Circe.encoder(this) diff --git a/enumeratum-circe/src/test/scala/enumeratum/CirceSpec.scala b/enumeratum-circe/src/test/scala/enumeratum/CirceSpec.scala index d7d1e852..b8cbb5d1 100644 --- a/enumeratum-circe/src/test/scala/enumeratum/CirceSpec.scala +++ b/enumeratum-circe/src/test/scala/enumeratum/CirceSpec.scala @@ -1,15 +1,15 @@ package enumeratum -import org.scalatest.{FunSpec, Matchers} -import cats.data.Xor +import org.scalatest.{ FunSpec, Matchers } +import cats.syntax.either._ import io.circe.Json import io.circe.syntax._ /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ class CirceSpec extends FunSpec with Matchers { describe("to JSON") { @@ -23,14 +23,16 @@ class CirceSpec extends FunSpec with Matchers { it("should work for lower case") { ShirtSize.values.foreach { entry => entry.asJson(Circe.encoderLowercase(ShirtSize)) shouldBe Json.fromString( - entry.entryName.toLowerCase) + entry.entryName.toLowerCase + ) } } it("should work for upper case") { ShirtSize.values.foreach { entry => entry.asJson(Circe.encoderUppercase(ShirtSize)) shouldBe Json.fromString( - entry.entryName.toUpperCase) + entry.entryName.toUpperCase + ) } } @@ -40,7 +42,7 @@ class CirceSpec extends FunSpec with Matchers { it("should parse to members when given proper JSON") { ShirtSize.values.foreach { entry => - Json.fromString(entry.entryName).as[ShirtSize] shouldBe Xor.Right(entry) + Json.fromString(entry.entryName).as[ShirtSize] shouldBe Right(entry) } } @@ -48,7 +50,7 @@ class CirceSpec extends FunSpec with Matchers { ShirtSize.values.foreach { entry => Json .fromString(entry.entryName.toLowerCase) - .as[ShirtSize](Circe.decoderLowercaseOnly(ShirtSize)) shouldBe Xor.Right(entry) + .as[ShirtSize](Circe.decoderLowercaseOnly(ShirtSize)) shouldBe Right(entry) } } @@ -56,7 +58,7 @@ class CirceSpec extends FunSpec with Matchers { ShirtSize.values.foreach { entry => Json .fromString(entry.entryName.toUpperCase) - .as[ShirtSize](Circe.decoderUppercaseOnly(ShirtSize)) shouldBe Xor.Right(entry) + .as[ShirtSize](Circe.decoderUppercaseOnly(ShirtSize)) shouldBe Right(entry) } } diff --git a/enumeratum-circe/src/test/scala/enumeratum/ShirtSize.scala b/enumeratum-circe/src/test/scala/enumeratum/ShirtSize.scala index 2d8d6674..2575d710 100644 --- a/enumeratum-circe/src/test/scala/enumeratum/ShirtSize.scala +++ b/enumeratum-circe/src/test/scala/enumeratum/ShirtSize.scala @@ -1,17 +1,17 @@ package enumeratum /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ sealed trait ShirtSize extends EnumEntry case object ShirtSize extends CirceEnum[ShirtSize] with Enum[ShirtSize] { - case object Small extends ShirtSize + case object Small extends ShirtSize case object Medium extends ShirtSize - case object Large extends ShirtSize + case object Large extends ShirtSize val values = findValues diff --git a/enumeratum-circe/src/test/scala/enumeratum/values/CirceValueEnumSpec.scala b/enumeratum-circe/src/test/scala/enumeratum/values/CirceValueEnumSpec.scala index 01260026..24053f54 100644 --- a/enumeratum-circe/src/test/scala/enumeratum/values/CirceValueEnumSpec.scala +++ b/enumeratum-circe/src/test/scala/enumeratum/values/CirceValueEnumSpec.scala @@ -1,15 +1,15 @@ package enumeratum.values -import org.scalatest.{FunSpec, Matchers} -import cats.data.Xor -import io.circe.{Decoder, Encoder, Json} +import org.scalatest.{ FunSpec, Matchers } +import cats.syntax.either._ +import io.circe.{ Decoder, Encoder, Json } import io.circe.syntax._ /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ class CirceValueEnumSpec extends FunSpec with Matchers { testCirceEnum("LongCirceEnum", CirceContentType) @@ -21,10 +21,10 @@ class CirceValueEnumSpec extends FunSpec with Matchers { testCirceEnum("IntCirceEnum with val value members", CirceMovieGenre) // Test method that generates tests for most primitve-based ValueEnums when given a simple descriptor and the enum - private def testCirceEnum[ValueType: Encoder: Decoder, - EntryType <: ValueEnumEntry[ValueType]: Encoder: Decoder]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType] with CirceValueEnum[ValueType, EntryType]): Unit = { + private def testCirceEnum[ValueType: Encoder: Decoder, EntryType <: ValueEnumEntry[ValueType]: Encoder: Decoder]( + enumKind: String, + enum: ValueEnum[ValueType, EntryType] with CirceValueEnum[ValueType, EntryType] + ): Unit = { describe(enumKind) { describe("to JSON") { @@ -41,7 +41,7 @@ class CirceValueEnumSpec extends FunSpec with Matchers { it("should parse to members when given proper JSON") { enum.values.foreach { entry => - entry.value.asJson.as[EntryType] shouldBe Xor.Right(entry) + entry.value.asJson.as[EntryType] shouldBe Right(entry) } } @@ -69,7 +69,7 @@ case object CirceContentType val values = findValues - case object Text extends CirceContentType(value = 1L, name = "text") + case object Text extends CirceContentType(value = 1L, name = "text") case object Image extends CirceContentType(value = 2L, name = "image") case object Video extends CirceContentType(value = 3L, name = "video") case object Audio extends CirceContentType(value = 4L, name = "audio") @@ -81,9 +81,9 @@ sealed abstract class CirceDrinks(val value: Short, name: String) extends ShortE case object CirceDrinks extends ShortEnum[CirceDrinks] with ShortCirceEnum[CirceDrinks] { case object OrangeJuice extends CirceDrinks(value = 1, name = "oj") - case object AppleJuice extends CirceDrinks(value = 2, name = "aj") - case object Cola extends CirceDrinks(value = 3, name = "cola") - case object Beer extends CirceDrinks(value = 4, name = "beer") + case object AppleJuice extends CirceDrinks(value = 2, name = "aj") + case object Cola extends CirceDrinks(value = 3, name = "cola") + case object Beer extends CirceDrinks(value = 4, name = "beer") val values = findValues @@ -96,10 +96,10 @@ case object CirceLibraryItem with IntCirceEnum[CirceLibraryItem] { // A good mix of named, unnamed, named + unordered args - case object Book extends CirceLibraryItem(value = 1, name = "book") - case object Movie extends CirceLibraryItem(name = "movie", value = 2) + case object Book extends CirceLibraryItem(value = 1, name = "book") + case object Movie extends CirceLibraryItem(name = "movie", value = 2) case object Magazine extends CirceLibraryItem(3, "magazine") - case object CD extends CirceLibraryItem(4, name = "cd") + case object CD extends CirceLibraryItem(4, name = "cd") val values = findValues @@ -111,8 +111,8 @@ case object CirceOperatingSystem extends StringEnum[CirceOperatingSystem] with StringCirceEnum[CirceOperatingSystem] { - case object Linux extends CirceOperatingSystem("linux") - case object OSX extends CirceOperatingSystem("osx") + case object Linux extends CirceOperatingSystem("linux") + case object OSX extends CirceOperatingSystem("osx") case object Windows extends CirceOperatingSystem("windows") case object Android extends CirceOperatingSystem("android") @@ -156,8 +156,8 @@ sealed abstract class CirceBites(val value: Byte) extends ByteEnumEntry object CirceBites extends ByteEnum[CirceBites] with ByteCirceEnum[CirceBites] { val values = findValues - case object OneByte extends CirceBites(1) - case object TwoByte extends CirceBites(2) + case object OneByte extends CirceBites(1) + case object TwoByte extends CirceBites(2) case object ThreeByte extends CirceBites(3) - case object FourByte extends CirceBites(4) + case object FourByte extends CirceBites(4) } diff --git a/enumeratum-core-jvm-tests/src/test/scala/enumeratum/EnumJVMSpec.scala b/enumeratum-core-jvm-tests/src/test/scala/enumeratum/EnumJVMSpec.scala index b0ffac9f..227e5f06 100644 --- a/enumeratum-core-jvm-tests/src/test/scala/enumeratum/EnumJVMSpec.scala +++ b/enumeratum-core-jvm-tests/src/test/scala/enumeratum/EnumJVMSpec.scala @@ -1,6 +1,6 @@ package enumeratum -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } class EnumJVMSpec extends FunSpec with Matchers { diff --git a/enumeratum-core-jvm-tests/src/test/scala/enumeratum/Eval.scala b/enumeratum-core-jvm-tests/src/test/scala/enumeratum/Eval.scala index c095611e..afeff196 100644 --- a/enumeratum-core-jvm-tests/src/test/scala/enumeratum/Eval.scala +++ b/enumeratum-core-jvm-tests/src/test/scala/enumeratum/Eval.scala @@ -3,17 +3,17 @@ package enumeratum import scala.tools.reflect.ToolBox /** - * Eval with bits and pieces stolen from here and there... - */ + * Eval with bits and pieces stolen from here and there... + */ object Eval { def apply[A]( - string: String, - compileOptions: String = s"-cp ${macroToolboxClassPath.mkString(";")}" + string: String, + compileOptions: String = s"-cp ${macroToolboxClassPath.mkString(";")}" ): A = { import scala.reflect.runtime.currentMirror val toolbox = currentMirror.mkToolBox(options = compileOptions) - val tree = toolbox.parse(string) + val tree = toolbox.parse(string) toolbox.eval(tree).asInstanceOf[A] } @@ -29,13 +29,13 @@ object Eval { def scalaBinaryVersion: String = { val PreReleasePattern = """.*-(M|RC).*""".r - val Pattern = """(\d+\.\d+)\..*""".r - val SnapshotPattern = """(\d+\.\d+\.\d+)-\d+-\d+-.*""".r + val Pattern = """(\d+\.\d+)\..*""".r + val SnapshotPattern = """(\d+\.\d+\.\d+)-\d+-\d+-.*""".r scala.util.Properties.versionNumberString match { case s @ PreReleasePattern(_) => s - case SnapshotPattern(v) => v + "-SNAPSHOT" - case Pattern(v) => v - case _ => "" + case SnapshotPattern(v) => v + "-SNAPSHOT" + case Pattern(v) => v + case _ => "" } } diff --git a/enumeratum-core-jvm-tests/src/test/scala/enumeratum/values/ValueEnumJVMSpec.scala b/enumeratum-core-jvm-tests/src/test/scala/enumeratum/values/ValueEnumJVMSpec.scala index 3c2c1f0c..2ffca097 100644 --- a/enumeratum-core-jvm-tests/src/test/scala/enumeratum/values/ValueEnumJVMSpec.scala +++ b/enumeratum-core-jvm-tests/src/test/scala/enumeratum/values/ValueEnumJVMSpec.scala @@ -1,16 +1,16 @@ package enumeratum.values import enumeratum.Eval -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } import scala.reflect.ClassTag import scala.util.Random /** - * Created by Lloyd on 8/30/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 8/30/16. + * + * Copyright 2016 + */ class ValueEnumJVMSpec extends FunSpec with Matchers { private def stringGenerator = @@ -25,19 +25,25 @@ class ValueEnumJVMSpec extends FunSpec with Matchers { testValuesOf( Stream .continually(Random.nextInt(Short.MaxValue - Short.MinValue) + Short.MinValue) - .map(_.toShort)) + .map(_.toShort) + ) testValuesOf( Stream .continually(Random.nextInt(Byte.MaxValue - Byte.MinValue) + Byte.MinValue) - .map(_.toByte)) + .map(_.toByte) + ) testValuesOf(stringGenerator, "\"", "\"") - testValuesOf(Stream.continually(Random.nextPrintableChar()).filter(Character.isAlphabetic(_)), - "'", - "'") + testValuesOf( + Stream.continually(Random.nextPrintableChar()).filter(Character.isAlphabetic(_)), + "'", + "'" + ) - private def testValuesOf[A: ClassTag](valuesGenerator: => Stream[A], - valuePrefix: String = "", - valueSuffix: String = ""): Unit = { + private def testValuesOf[A: ClassTag]( + valuesGenerator: => Stream[A], + valuePrefix: String = "", + valueSuffix: String = "" + ): Unit = { val typeName = implicitly[ClassTag[A]].runtimeClass.getSimpleName.capitalize @@ -45,9 +51,9 @@ class ValueEnumJVMSpec extends FunSpec with Matchers { it("should return proper members for valid values but throw otherwise") { (1 to 20).foreach { i => - val enumName = s"Generated${typeName}Enum$i" - val names = stringGenerator.take(5) - val values = valuesGenerator.distinct.take(5) + val enumName = s"Generated${typeName}Enum$i" + val names = stringGenerator.take(5) + val values = valuesGenerator.distinct.take(5) val namesToValues = names.zip(values) val memberDefs = namesToValues.map { case (n, v) => s"""case object $n extends $enumName($valuePrefix$v$valueSuffix)""" diff --git a/enumeratum-core/src/main/scala/enumeratum/Enum.scala b/enumeratum-core/src/main/scala/enumeratum/Enum.scala index 740181f6..ae2edd09 100644 --- a/enumeratum-core/src/main/scala/enumeratum/Enum.scala +++ b/enumeratum-core/src/main/scala/enumeratum/Enum.scala @@ -5,151 +5,151 @@ import scala.language.postfixOps import ContextUtils.Context /** - * All the cool kids have their own Enumeration implementation, most of which try to - * do so in the name of implementing exhaustive pattern matching. - * - * This is yet another one. - * - * How to use: - * - * {{{ - * sealed trait DummyEnum extends EnumEntry - * - * object DummyEnum extends Enum[DummyEnum] { - * - * val values = findValues - * - * case object Hello extends DummyEnum - * case object GoodBye extends DummyEnum - * case object Hi extends DummyEnum - * - * } - * - * - * DummyEnum.values should be(Set(Hello, GoodBye, Hi)) - * - * DummyEnum.withName("Hello") should be(Hello) - * }}} - * @tparam A The sealed trait - */ + * All the cool kids have their own Enumeration implementation, most of which try to + * do so in the name of implementing exhaustive pattern matching. + * + * This is yet another one. + * + * How to use: + * + * {{{ + * sealed trait DummyEnum extends EnumEntry + * + * object DummyEnum extends Enum[DummyEnum] { + * + * val values = findValues + * + * case object Hello extends DummyEnum + * case object GoodBye extends DummyEnum + * case object Hi extends DummyEnum + * + * } + * + * + * DummyEnum.values should be(Set(Hello, GoodBye, Hi)) + * + * DummyEnum.withName("Hello") should be(Hello) + * }}} + * @tparam A The sealed trait + */ trait Enum[A <: EnumEntry] { /** - * Map of [[A]] object names to [[A]]s - */ + * Map of [[A]] object names to [[A]]s + */ lazy final val namesToValuesMap: Map[String, A] = values map (v => v.entryName -> v) toMap /** - * 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 + */ lazy final val lowerCaseNamesToValuesMap: Map[String, A] = values map (v => v.entryName.toLowerCase -> v) toMap /** - * 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 + */ lazy final val upperCaseNameValuesToMap: Map[String, A] = values map (v => v.entryName.toUpperCase -> v) toMap /** - * Map of [[A]] to their index in the values sequence. - * - * A performance optimisation so that indexOf can be found in constant time. - */ + * Map of [[A]] to their index in the values sequence. + * + * A performance optimisation so that indexOf can be found in constant time. + */ lazy final val valuesToIndex: Map[A, Int] = values.zipWithIndex.toMap /** - * 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. - */ + * 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. + */ def values: Seq[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. - */ + * 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. + */ def withName(name: String): A = withNameOption(name) getOrElse (throw new NoSuchElementException(buildNotFoundMessage(name))) /** - * Optionally returns an [[A]] for a given name. - */ + * Optionally returns an [[A]] for a given name. + */ def withNameOption(name: String): Option[A] = namesToValuesMap get name /** - * 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. - */ + * 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. + */ def withNameInsensitive(name: String): A = withNameInsensitiveOption(name) getOrElse (throw new NoSuchElementException(buildNotFoundMessage(name))) /** - * 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. - */ + * 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. + */ def withNameUppercaseOnly(name: String): A = withNameUppercaseOnlyOption(name) getOrElse (throw new NoSuchElementException(buildNotFoundMessage(name))) /** - * 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. - */ + * 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. + */ def withNameLowercaseOnly(name: String): A = withNameLowercaseOnlyOption(name) getOrElse (throw new NoSuchElementException(buildNotFoundMessage(name))) /** - * Optionally returns an [[A]] for a given name, disregarding case - */ + * Optionally returns an [[A]] for a given name, disregarding case + */ def withNameInsensitiveOption(name: String): Option[A] = lowerCaseNamesToValuesMap get name.toLowerCase /** - * 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 + */ def withNameUppercaseOnlyOption(name: String): Option[A] = upperCaseNameValuesToMap get name /** - * 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 + */ def withNameLowercaseOnlyOption(name: String): Option[A] = lowerCaseNamesToValuesMap get name /** - * Returns the index number of the member passed in the values picked up by this enum - * - * @param member the member you want to check the index of - * @return the index of the first element of values that is equal (as determined by ==) to member, or -1, if none exists. - */ + * Returns the index number of the member passed in the values picked up by this enum + * + * @param member the member you want to check the index of + * @return the index of the first element of values that is equal (as determined by ==) to member, or -1, if none exists. + */ def indexOf(member: A): Int = valuesToIndex.getOrElse(member, -1) /** - * Method that 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? - */ + * Method that 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? + */ protected def findValues: Seq[A] = macro EnumMacros.findValuesImpl[A] private def buildNotFoundMessage(notFoundName: String): String = { @@ -164,9 +164,8 @@ trait Enum[A <: EnumEntry] { object Enum { /** - * Finds the Enum companion object for a particular EnumEntry - */ - implicit def materializeEnum[A <: EnumEntry]: Enum[A] = - macro EnumMacros.materializeEnumImpl[A] + * Finds the Enum companion object for a particular EnumEntry + */ + implicit def materializeEnum[A <: EnumEntry]: Enum[A] = macro EnumMacros.materializeEnumImpl[A] } diff --git a/enumeratum-core/src/main/scala/enumeratum/EnumEntry.scala b/enumeratum-core/src/main/scala/enumeratum/EnumEntry.scala index 17652979..24cce701 100644 --- a/enumeratum-core/src/main/scala/enumeratum/EnumEntry.scala +++ b/enumeratum-core/src/main/scala/enumeratum/EnumEntry.scala @@ -3,21 +3,21 @@ package enumeratum import java.util.regex.Pattern /** - * Base type for an enum entry for [[Enum]] - * - * By default, the entryName method used for serialising and deseralising Enum values uses - * toString, but feel free to override to fit your needs. - * - * Mix in the supplied stackable traits to convert the entryName to [[EnumEntry.Snakecase Snakecase]], - * [[EnumEntry.Uppercase Uppercase]], and [[EnumEntry.Lowercase Lowercase]] - */ + * Base type for an enum entry for [[Enum]] + * + * By default, the entryName method used for serialising and deseralising Enum values uses + * toString, but feel free to override to fit your needs. + * + * Mix in the supplied stackable traits to convert the entryName to [[EnumEntry.Snakecase Snakecase]], + * [[EnumEntry.Uppercase Uppercase]], and [[EnumEntry.Lowercase Lowercase]] + */ abstract class EnumEntry { /** - * String representation of this Enum Entry. - * - * Override in your implementation if needed - */ + * String representation of this Enum Entry. + * + * Override in your implementation if needed + */ def entryName: String = toString } @@ -29,14 +29,14 @@ object EnumEntry { * * http://stackoverflow.com/a/19832063/1814775 */ - private val snakifyRegexp1 = Pattern.compile("([A-Z]+)([A-Z][a-z])") - private val snakifyRegexp2 = Pattern.compile("([a-z\\d])([A-Z])") + private val snakifyRegexp1 = Pattern.compile("([A-Z]+)([A-Z][a-z])") + private val snakifyRegexp2 = Pattern.compile("([a-z\\d])([A-Z])") private val snakifyReplacement = "$1_$2" /** - * Stackable trait to convert the entryName to snake_case. For UPPER_SNAKE_CASE, - * also mix in [[Uppercase]] after this one. - */ + * Stackable trait to convert the entryName to snake_case. For UPPER_SNAKE_CASE, + * also mix in [[Uppercase]] after this one. + */ trait Snakecase extends EnumEntry { abstract override def entryName: String = camel2snake(super.entryName) @@ -48,40 +48,40 @@ object EnumEntry { } /** - * Stackable trait to convert the entryName to UPPERCASE. - */ + * Stackable trait to convert the entryName to UPPERCASE. + */ trait Uppercase extends EnumEntry { abstract override def entryName: String = super.entryName.toUpperCase } /** - * Stackable trait to convert the entryName to lowercase. - */ + * Stackable trait to convert the entryName to lowercase. + */ trait Lowercase extends EnumEntry { abstract override def entryName: String = super.entryName.toLowerCase } /** - * Helper implicit class that holds enrichment methods - */ + * Helper implicit class that holds enrichment methods + */ implicit class EnumEntryOps[A <: EnumEntry](val enumEntry: A) extends AnyVal { /** - * Checks if the current enum value is contained by the set of enum values in the parameter list. - * - * @param firstEntry First enum of the list. - * @param otherEnums Remaining enums. - * @return `true` if the current value is contained by the parameter list. - */ + * Checks if the current enum value is contained by the set of enum values in the parameter list. + * + * @param firstEntry First enum of the list. + * @param otherEnums Remaining enums. + * @return `true` if the current value is contained by the parameter list. + */ def in(firstEntry: A, otherEnums: A*): Boolean = in(firstEntry +: otherEnums) /** - * Checks if the current enum value is contained by the set of enum values in the parameter list. - * - * @param entries First enum of the list. - * @return `true` if the current value is contained by the parameter list. - */ + * Checks if the current enum value is contained by the set of enum values in the parameter list. + * + * @param entries First enum of the list. + * @return `true` if the current value is contained by the parameter list. + */ def in(entries: Seq[A]): Boolean = entries.contains(enumEntry) } diff --git a/enumeratum-core/src/main/scala/enumeratum/values/ValueEnum.scala b/enumeratum-core/src/main/scala/enumeratum/values/ValueEnum.scala index 3076b0b3..65768a35 100644 --- a/enumeratum-core/src/main/scala/enumeratum/values/ValueEnum.scala +++ b/enumeratum-core/src/main/scala/enumeratum/values/ValueEnum.scala @@ -1,40 +1,40 @@ package enumeratum.values -import enumeratum.{EnumMacros, ValueEnumMacros} +import enumeratum.{ EnumMacros, ValueEnumMacros } import scala.language.experimental.macros sealed trait ValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { /** - * Map of [[ValueType]] to [[EntryType]] members - */ + * Map of [[ValueType]] to [[EntryType]] members + */ final lazy val valuesToEntriesMap: Map[ValueType, EntryType] = values.map(v => v.value -> v).toMap /** - * The sequence of values for your [[Enum]]. You will typically want - * to implement this in your extending class as a `val` so that `withValue` - * 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. - */ + * The sequence of values for your [[Enum]]. You will typically want + * to implement this in your extending class as a `val` so that `withValue` + * 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. + */ def values: Seq[EntryType] /** - * Tries to get an [[EntryType]] by the supplied value. The value corresponds to the .value - * of the case objects implementing [[EntryType]] - * - * Like [[Enumeration]]'s `withValue`, this method will throw if the value does not match any of the values' - * `.value` values. - */ + * Tries to get an [[EntryType]] by the supplied value. The value corresponds to the .value + * of the case objects implementing [[EntryType]] + * + * Like [[Enumeration]]'s `withValue`, this method will throw if the value does not match any of the values' + * `.value` values. + */ def withValue(i: ValueType): EntryType = withValueOpt(i).getOrElse(throw new NoSuchElementException(buildNotFoundMessage(i))) /** - * Optionally returns an [[EntryType]] for a given value. - */ + * Optionally returns an [[EntryType]] for a given value. + */ def withValueOpt(i: ValueType): Option[EntryType] = valuesToEntriesMap.get(i) private lazy val existingEntriesString = values.map(_.value).mkString(", ") @@ -56,165 +56,153 @@ sealed trait ValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { object IntEnum { /** - * Materializes an IntEnum for a given IntEnumEntry - */ - implicit def materialiseIntValueEnum[EntryType <: IntEnumEntry]: IntEnum[EntryType] = - macro EnumMacros.materializeEnumImpl[EntryType] + * Materializes an IntEnum for a given IntEnumEntry + */ + implicit def materialiseIntValueEnum[EntryType <: IntEnumEntry]: IntEnum[EntryType] = macro EnumMacros.materializeEnumImpl[EntryType] } /** - * Value enum with [[IntEnumEntry]] entries - */ + * Value enum with [[IntEnumEntry]] entries + */ trait IntEnum[A <: IntEnumEntry] extends ValueEnum[Int, A] { /** - * Method that 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? - */ - protected def findValues: IndexedSeq[A] = - macro ValueEnumMacros.findIntValueEntriesImpl[A] + * Method that 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? + */ + protected def findValues: IndexedSeq[A] = macro ValueEnumMacros.findIntValueEntriesImpl[A] } object LongEnum { /** - * Materializes a LongEnum for an scope LongEnumEntry - */ - implicit def materialiseLongValueEnum[EntryType <: LongEnumEntry]: LongEnum[EntryType] = - macro EnumMacros.materializeEnumImpl[EntryType] + * Materializes a LongEnum for an scope LongEnumEntry + */ + implicit def materialiseLongValueEnum[EntryType <: LongEnumEntry]: LongEnum[EntryType] = macro EnumMacros.materializeEnumImpl[EntryType] } /** - * Value enum with [[LongEnumEntry]] entries - */ + * Value enum with [[LongEnumEntry]] entries + */ trait LongEnum[A <: LongEnumEntry] extends ValueEnum[Long, A] { /** - * Method that 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? - */ - final protected def findValues: IndexedSeq[A] = - macro ValueEnumMacros.findLongValueEntriesImpl[A] + * Method that 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? + */ + final protected def findValues: IndexedSeq[A] = macro ValueEnumMacros.findLongValueEntriesImpl[A] } object ShortEnum { /** - * Materializes a ShortEnum for an in-scope ShortEnumEntry - */ - implicit def materialiseShortValueEnum[EntryType <: ShortEnumEntry]: ShortEnum[EntryType] = - macro EnumMacros.materializeEnumImpl[EntryType] + * Materializes a ShortEnum for an in-scope ShortEnumEntry + */ + implicit def materialiseShortValueEnum[EntryType <: ShortEnumEntry]: ShortEnum[EntryType] = macro EnumMacros.materializeEnumImpl[EntryType] } /** - * Value enum with [[ShortEnumEntry]] entries - */ + * Value enum with [[ShortEnumEntry]] entries + */ trait ShortEnum[A <: ShortEnumEntry] extends ValueEnum[Short, A] { /** - * Method that 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? - */ - final protected def findValues: IndexedSeq[A] = - macro ValueEnumMacros.findShortValueEntriesImpl[A] + * Method that 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? + */ + final protected def findValues: IndexedSeq[A] = macro ValueEnumMacros.findShortValueEntriesImpl[A] } object StringEnum { /** - * Materializes a StringEnum for an in-scope StringEnumEntry - */ - implicit def materialiseStringValueEnum[EntryType <: StringEnumEntry]: StringEnum[EntryType] = - macro EnumMacros.materializeEnumImpl[EntryType] + * Materializes a StringEnum for an in-scope StringEnumEntry + */ + implicit def materialiseStringValueEnum[EntryType <: StringEnumEntry]: StringEnum[EntryType] = macro EnumMacros.materializeEnumImpl[EntryType] } /** - * Value enum with [[StringEnumEntry]] entries - * - * This is similar to [[enumeratum.Enum]], but different in that values must be - * literal values. This restraint allows us to enforce uniqueness at compile time. - * - * Note that uniqueness is only guaranteed if you do not do any runtime string manipulation on values. - */ + * Value enum with [[StringEnumEntry]] entries + * + * This is similar to [[enumeratum.Enum]], but different in that values must be + * literal values. This restraint allows us to enforce uniqueness at compile time. + * + * Note that uniqueness is only guaranteed if you do not do any runtime string manipulation on values. + */ trait StringEnum[A <: StringEnumEntry] extends ValueEnum[String, A] { /** - * Method that 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? - */ - final protected def findValues: IndexedSeq[A] = - macro ValueEnumMacros.findStringValueEntriesImpl[A] + * Method that 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? + */ + final protected def findValues: IndexedSeq[A] = macro ValueEnumMacros.findStringValueEntriesImpl[A] } object ByteEnum { /** - * Materializes a ByteEnum for an in-scope ByteEnumEntry - */ - implicit def materialiseByteValueEnum[EntryType <: ByteEnumEntry]: ByteEnum[EntryType] = - macro EnumMacros.materializeEnumImpl[EntryType] + * Materializes a ByteEnum for an in-scope ByteEnumEntry + */ + implicit def materialiseByteValueEnum[EntryType <: ByteEnumEntry]: ByteEnum[EntryType] = macro EnumMacros.materializeEnumImpl[EntryType] } /** - * Value enum with [[ByteEnumEntry]] entries - * - * This is similar to [[enumeratum.Enum]], but different in that values must be - * literal values. This restraint allows us to enforce uniqueness at compile time. - * - * Note that uniqueness is only guaranteed if you do not do any runtime string manipulation on values. - */ + * Value enum with [[ByteEnumEntry]] entries + * + * This is similar to [[enumeratum.Enum]], but different in that values must be + * literal values. This restraint allows us to enforce uniqueness at compile time. + * + * Note that uniqueness is only guaranteed if you do not do any runtime string manipulation on values. + */ trait ByteEnum[A <: ByteEnumEntry] extends ValueEnum[Byte, A] { /** - * Method that 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? - */ - final protected def findValues: IndexedSeq[A] = - macro ValueEnumMacros.findByteValueEntriesImpl[A] + * Method that 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? + */ + final protected def findValues: IndexedSeq[A] = macro ValueEnumMacros.findByteValueEntriesImpl[A] } object CharEnum { /** - * Materializes a CharEnum for an in-scope CharEnumEntry - */ - implicit def materialiseCharValueEnum[EntryType <: CharEnumEntry]: CharEnum[EntryType] = - macro EnumMacros.materializeEnumImpl[EntryType] + * Materializes a CharEnum for an in-scope CharEnumEntry + */ + implicit def materialiseCharValueEnum[EntryType <: CharEnumEntry]: CharEnum[EntryType] = macro EnumMacros.materializeEnumImpl[EntryType] } /** - * Value enum with [[CharEnumEntry]] entries - * - * This is similar to [[enumeratum.Enum]], but different in that values must be - * literal values. This restraint allows us to enforce uniqueness at compile time. - * - * Note that uniqueness is only guaranteed if you do not do any runtime string manipulation on values. - */ + * Value enum with [[CharEnumEntry]] entries + * + * This is similar to [[enumeratum.Enum]], but different in that values must be + * literal values. This restraint allows us to enforce uniqueness at compile time. + * + * Note that uniqueness is only guaranteed if you do not do any runtime string manipulation on values. + */ trait CharEnum[A <: CharEnumEntry] extends ValueEnum[Char, A] { /** - * Method that 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? - */ - final protected def findValues: IndexedSeq[A] = - macro ValueEnumMacros.findCharValueEntriesImpl[A] + * Method that 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? + */ + final protected def findValues: IndexedSeq[A] = macro ValueEnumMacros.findCharValueEntriesImpl[A] } diff --git a/enumeratum-core/src/main/scala/enumeratum/values/ValueEnumEntry.scala b/enumeratum-core/src/main/scala/enumeratum/values/ValueEnumEntry.scala index f142d014..7798537b 100644 --- a/enumeratum-core/src/main/scala/enumeratum/values/ValueEnumEntry.scala +++ b/enumeratum-core/src/main/scala/enumeratum/values/ValueEnumEntry.scala @@ -1,15 +1,15 @@ package enumeratum.values /** - * Created by Lloyd on 4/11/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/11/16. + * + * Copyright 2016 + */ sealed trait ValueEnumEntry[ValueType] { /** - * Value of this entry - */ + * Value of this entry + */ def value: ValueType } @@ -19,56 +19,56 @@ object ValueEnumEntry { implicit class ValueEnumOps[A <: ValueEnumEntry[_]](val enumEntry: A) extends AnyVal { /** - * Checks if the current enum value is contained by the set of enum values in the parameter list. - * - * @param firstEnum First enum of the list. - * @param otherEnums Remaining enums. - * @return `true` if the current value is contained by the parameter list. - */ + * Checks if the current enum value is contained by the set of enum values in the parameter list. + * + * @param firstEnum First enum of the list. + * @param otherEnums Remaining enums. + * @return `true` if the current value is contained by the parameter list. + */ def in(firstEnum: A, otherEnums: A*): Boolean = in(firstEnum +: otherEnums) /** - * Checks if the current enum value is contained by the set of enum values in the parameter list. - * - * @param entries First enum of the list. - * @return `true` if the current value is contained by the parameter list. - */ + * Checks if the current enum value is contained by the set of enum values in the parameter list. + * + * @param entries First enum of the list. + * @return `true` if the current value is contained by the parameter list. + */ def in(entries: Seq[A]): Boolean = entries.contains(enumEntry) } } /** - * Value Enum Entry parent class for [[Int]] valued entries - */ + * Value Enum Entry parent class for [[Int]] valued entries + */ abstract class IntEnumEntry extends ValueEnumEntry[Int] /** - * Value Enum Entry parent class for [[Long]] valued entries - */ + * Value Enum Entry parent class for [[Long]] valued entries + */ abstract class LongEnumEntry extends ValueEnumEntry[Long] /** - * Value Enum Entry parent class for [[Short]] valued entries - */ + * Value Enum Entry parent class for [[Short]] valued entries + */ abstract class ShortEnumEntry extends ValueEnumEntry[Short] /** - * Value Enum Entry parent class for [[String]] valued entries - * - * This is similar to [[enumeratum.Enum]], but different in that values must be - * literal values. This restraint allows us to enforce uniqueness at compile time. - * - * Note that uniqueness is only guaranteed if you do not do any runtime string manipulation on values. - */ + * Value Enum Entry parent class for [[String]] valued entries + * + * This is similar to [[enumeratum.Enum]], but different in that values must be + * literal values. This restraint allows us to enforce uniqueness at compile time. + * + * Note that uniqueness is only guaranteed if you do not do any runtime string manipulation on values. + */ abstract class StringEnumEntry extends ValueEnumEntry[String] /** - * Value Enum Entry parent class for [[Byte]] valued entries - */ + * Value Enum Entry parent class for [[Byte]] valued entries + */ abstract class ByteEnumEntry extends ValueEnumEntry[Byte] /** - * Value Enum Entry parent class for [[Char]] valued entries - */ + * Value Enum Entry parent class for [[Char]] valued entries + */ abstract class CharEnumEntry extends ValueEnumEntry[Char] diff --git a/enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala b/enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala index 53ccba52..33cb8cbe 100644 --- a/enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala +++ b/enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala @@ -1,7 +1,7 @@ package enumeratum import org.scalatest.OptionValues._ -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } class EnumSpec extends FunSpec with Matchers { @@ -355,12 +355,13 @@ class EnumSpec extends FunSpec with Matchers { } it( - "should fail to compile if either enum in the parameter list is not instance of the same enum type as the checked one") { - """ + "should fail to compile if either enum in the parameter list is not instance of the same enum type as the checked one" + ) { + """ val enum: DummyEnum = DummyEnum.Hi enum.in(DummyEnum.Hello, SnakeEnum.ShoutGoodBye) """ shouldNot compile - } + } } describe("materializeEnum") { @@ -370,7 +371,7 @@ class EnumSpec extends FunSpec with Matchers { def findEnum[A <: EnumEntry: Enum](v: A) = implicitly[Enum[A]] val hello: DummyEnum = Hello - val companion = findEnum(hello) + val companion = findEnum(hello) companion shouldBe DummyEnum companion.values should contain(Hello) diff --git a/enumeratum-core/src/test/scala/enumeratum/Models.scala b/enumeratum-core/src/test/scala/enumeratum/Models.scala index b5d972e2..5affa1fe 100644 --- a/enumeratum-core/src/test/scala/enumeratum/Models.scala +++ b/enumeratum-core/src/test/scala/enumeratum/Models.scala @@ -14,9 +14,9 @@ object DummyEnum extends Enum[DummyEnum] { val values = findValues - case object Hello extends DummyEnum + case object Hello extends DummyEnum case object GoodBye extends DummyEnum - case object Hi extends DummyEnum + case object Hi extends DummyEnum } @@ -26,8 +26,8 @@ object SnakeEnum extends Enum[SnakeEnum] { val values = findValues - case object Hello extends SnakeEnum - case object GoodBye extends SnakeEnum + case object Hello extends SnakeEnum + case object GoodBye extends SnakeEnum case object ShoutGoodBye extends SnakeEnum with Uppercase } @@ -38,9 +38,9 @@ object LowerEnum extends Enum[LowerEnum] { val values = findValues - case object Hello extends LowerEnum + case object Hello extends LowerEnum case object GoodBye extends LowerEnum - case object Sike extends LowerEnum with Uppercase + case object Sike extends LowerEnum with Uppercase } @@ -52,9 +52,9 @@ object Wrapper { val values = findValues - case object Hello extends SmartEnum + case object Hello extends SmartEnum case object GoodBye extends SmartEnum - case object Hi extends SmartEnum + case object Hi extends SmartEnum } @@ -68,8 +68,8 @@ object InTheWoods { val values = findValues case object FlyAgaric extends Mushroom(true) - case object LSD extends Mushroom(false) - case object Shimeji extends Mushroom(false) + case object LSD extends Mushroom(false) + case object Shimeji extends Mushroom(false) } } diff --git a/enumeratum-core/src/test/scala/enumeratum/values/Alphabet.scala b/enumeratum-core/src/test/scala/enumeratum/values/Alphabet.scala index d33106e1..68701a29 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/Alphabet.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/Alphabet.scala @@ -1,10 +1,10 @@ package enumeratum.values /** - * Created by Lloyd on 9/24/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 9/24/16. + * + * Copyright 2016 + */ sealed abstract class Alphabet(val value: Char) extends CharEnumEntry case object Alphabet extends CharEnum[Alphabet] { diff --git a/enumeratum-core/src/test/scala/enumeratum/values/Animal.scala b/enumeratum-core/src/test/scala/enumeratum/values/Animal.scala index 6429e2d1..ffe0bc06 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/Animal.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/Animal.scala @@ -1,19 +1,19 @@ package enumeratum.values /** - * Created by Lloyd on 8/22/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 8/22/16. + * + * Copyright 2016 + */ sealed abstract class Animal(val value: Long) extends LongEnumEntry case object Animal extends LongEnum[Animal] { val values = findValues - case object Plant extends Animal(1L) + case object Plant extends Animal(1L) case object Reptile extends Animal(2L) - case object Mammal extends Animal(3L) + case object Mammal extends Animal(3L) sealed abstract class Mammalian(val value: Int) extends IntEnumEntry @@ -21,8 +21,8 @@ case object Animal extends LongEnum[Animal] { val values = findValues - case object Dog extends Mammalian(1) - case object Cat extends Mammalian(2) + case object Dog extends Mammalian(1) + case object Cat extends Mammalian(2) case object Whale extends Mammalian(3) case object Mouse extends Mammalian(4) case object Human extends Mammalian(5) diff --git a/enumeratum-core/src/test/scala/enumeratum/values/Bites.scala b/enumeratum-core/src/test/scala/enumeratum/values/Bites.scala index 7c935fd5..1a204a96 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/Bites.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/Bites.scala @@ -1,17 +1,17 @@ package enumeratum.values /** - * Created by Lloyd on 9/24/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 9/24/16. + * + * Copyright 2016 + */ sealed abstract class Bites(val value: Byte) extends ByteEnumEntry object Bites extends ByteEnum[Bites] { val values = findValues - case object OneByte extends Bites(1) - case object TwoByte extends Bites(2) + case object OneByte extends Bites(1) + case object TwoByte extends Bites(2) case object ThreeByte extends Bites(3) - case object FourByte extends Bites(4) + case object FourByte extends Bites(4) } diff --git a/enumeratum-core/src/test/scala/enumeratum/values/ContentType.scala b/enumeratum-core/src/test/scala/enumeratum/values/ContentType.scala index 136fed94..f48fced8 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/ContentType.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/ContentType.scala @@ -1,15 +1,15 @@ package enumeratum.values /** - * Created by Lloyd on 4/12/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/12/16. + * + * Copyright 2016 + */ sealed abstract class ContentType(val value: Long, name: String) extends LongEnumEntry case object ContentType extends LongEnum[ContentType] { - case object Text extends ContentType(value = 1L, name = "text") + 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") diff --git a/enumeratum-core/src/test/scala/enumeratum/values/CustomEnumPrivateConstructor.scala b/enumeratum-core/src/test/scala/enumeratum/values/CustomEnumPrivateConstructor.scala index b0c40082..ae461bec 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/CustomEnumPrivateConstructor.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/CustomEnumPrivateConstructor.scala @@ -1,8 +1,8 @@ package enumeratum.values /** - * Code from @zifeo - */ + * Code from @zifeo + */ trait CustomEnumEntry extends IntEnumEntry { val value: Int val name: String @@ -16,8 +16,8 @@ trait CustomEnumComparable[T <: CustomEnumEntry] { this: T => this.value >= that.value } sealed abstract class CustomEnumPrivateConstructor private (val value: Int, val name: String) - extends CustomEnumEntry - with CustomEnumComparable[CustomEnumPrivateConstructor] + extends CustomEnumEntry + with CustomEnumComparable[CustomEnumPrivateConstructor] object CustomEnumPrivateConstructor extends CustomEnum[CustomEnumPrivateConstructor] { val values = findValues case object A extends CustomEnumPrivateConstructor(10, "a") diff --git a/enumeratum-core/src/test/scala/enumeratum/values/Drinks.scala b/enumeratum-core/src/test/scala/enumeratum/values/Drinks.scala index 67e3eddd..97de5f40 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/Drinks.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/Drinks.scala @@ -1,18 +1,18 @@ package enumeratum.values /** - * Created by Lloyd on 4/12/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/12/16. + * + * Copyright 2016 + */ sealed abstract class Drinks(val value: Short, name: String) extends ShortEnumEntry case object Drinks extends ShortEnum[Drinks] { case object OrangeJuice extends Drinks(value = 1, name = "oj") - case object AppleJuice extends Drinks(value = 2, name = "aj") - case object Cola extends Drinks(value = 3, name = "cola") - case object Beer extends Drinks(value = 4, name = "beer") + case object AppleJuice extends Drinks(value = 2, name = "aj") + case object Cola extends Drinks(value = 3, name = "cola") + case object Beer extends Drinks(value = 4, name = "beer") val values = findValues diff --git a/enumeratum-core/src/test/scala/enumeratum/values/LibraryItem.scala b/enumeratum-core/src/test/scala/enumeratum/values/LibraryItem.scala index ca506793..04c351e9 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/LibraryItem.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/LibraryItem.scala @@ -1,10 +1,10 @@ package enumeratum.values /** - * Created by Lloyd on 4/11/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/11/16. + * + * Copyright 2016 + */ sealed abstract class LibraryItem(val value: Int, val name: String) extends IntEnumEntry case object LibraryItem extends IntEnum[LibraryItem] { @@ -13,10 +13,10 @@ case object LibraryItem extends IntEnum[LibraryItem] { - A good mix of named, unnamed, named + unordered args - Values are not in ordered consecutive order */ - case object Movie extends LibraryItem(name = "movie", value = 2) - case object Book extends LibraryItem(value = 1, name = "book") + case object Movie extends LibraryItem(name = "movie", value = 2) + case object Book extends LibraryItem(value = 1, name = "book") case object Magazine extends LibraryItem(10, "magazine") - case object CD extends LibraryItem(14, name = "cd") + case object CD extends LibraryItem(14, name = "cd") val values = findValues diff --git a/enumeratum-core/src/test/scala/enumeratum/values/MovieGenre.scala b/enumeratum-core/src/test/scala/enumeratum/values/MovieGenre.scala index cbea6524..5caf9fd3 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/MovieGenre.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/MovieGenre.scala @@ -1,10 +1,10 @@ package enumeratum.values /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ sealed abstract class MovieGenre extends IntEnumEntry case object MovieGenre extends IntEnum[MovieGenre] { diff --git a/enumeratum-core/src/test/scala/enumeratum/values/OperatingSystem.scala b/enumeratum-core/src/test/scala/enumeratum/values/OperatingSystem.scala index ae062bc8..be5f2d86 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/OperatingSystem.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/OperatingSystem.scala @@ -1,18 +1,18 @@ package enumeratum.values /** - * Created by Lloyd on 8/4/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 8/4/16. + * + * Copyright 2016 + */ sealed abstract class OperatingSystem(val value: String) extends StringEnumEntry case object OperatingSystem extends StringEnum[OperatingSystem] { val values = findValues - case object Linux extends OperatingSystem("linux") - case object OSX extends OperatingSystem("osx") + case object Linux extends OperatingSystem("linux") + case object OSX extends OperatingSystem("osx") case object Windows extends OperatingSystem("windows") case object Android extends OperatingSystem("android") diff --git a/enumeratum-core/src/test/scala/enumeratum/values/ValueEnumHelpers.scala b/enumeratum-core/src/test/scala/enumeratum/values/ValueEnumHelpers.scala index e11c6e54..e5b222d7 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/ValueEnumHelpers.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/ValueEnumHelpers.scala @@ -5,18 +5,19 @@ import java.util.NoSuchElementException import org.scalatest._ /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ trait ValueEnumHelpers { this: FunSpec with Matchers => /* * Generates tests for a given enum and groups the tests inside the given enumKind descriptor */ def testNumericEnum[EntryType <: ValueEnumEntry[ValueType], ValueType <: AnyVal: Numeric]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType]): Unit = { + enumKind: String, + enum: ValueEnum[ValueType, EntryType] + ): Unit = { val numeric = implicitly[Numeric[ValueType]] testEnum(enumKind, enum, Seq(numeric.fromInt(Int.MaxValue))) } @@ -25,9 +26,10 @@ trait ValueEnumHelpers { this: FunSpec with Matchers => * Generates tests for a given enum and groups the tests inside the given enumKind descriptor */ def testEnum[EntryType <: ValueEnumEntry[ValueType], ValueType]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - invalidValues: Seq[ValueType]): Unit = { + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + invalidValues: Seq[ValueType] + ): Unit = { describe(enumKind) { diff --git a/enumeratum-core/src/test/scala/enumeratum/values/ValueEnumSpec.scala b/enumeratum-core/src/test/scala/enumeratum/values/ValueEnumSpec.scala index 6cf94b61..0710639f 100644 --- a/enumeratum-core/src/test/scala/enumeratum/values/ValueEnumSpec.scala +++ b/enumeratum-core/src/test/scala/enumeratum/values/ValueEnumSpec.scala @@ -1,12 +1,12 @@ package enumeratum.values -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } /** - * Created by Lloyd on 4/12/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/12/16. + * + * Copyright 2016 + */ class ValueEnumSpec extends FunSpec with Matchers with ValueEnumHelpers { describe("basic sanity check") { diff --git a/enumeratum-play-json/src/main/scala/enumeratum/EnumFormats.scala b/enumeratum-play-json/src/main/scala/enumeratum/EnumFormats.scala index 22fd9262..3fae5e6d 100644 --- a/enumeratum-play-json/src/main/scala/enumeratum/EnumFormats.scala +++ b/enumeratum-play-json/src/main/scala/enumeratum/EnumFormats.scala @@ -3,16 +3,16 @@ package enumeratum import play.api.libs.json._ /** - * Holds JSON reads and writes for [[enumeratum.Enum]] - */ + * Holds JSON reads and writes for [[enumeratum.Enum]] + */ object EnumFormats { /** - * Returns an Json Reads for a given enum [[Enum]] - * - * @param enum The enum - * @param insensitive bind in a case-insensitive way, defaults to false - */ + * Returns an Json Reads for a given enum [[Enum]] + * + * @param enum The enum + * @param insensitive bind in a case-insensitive way, defaults to false + */ def reads[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Reads[A] = new Reads[A] { def reads(json: JsValue): JsResult[A] = json match { @@ -22,7 +22,7 @@ object EnumFormats { else enum.withNameOption(s) maybeBound match { case Some(obj) => JsSuccess(obj) - case None => JsError("error.expected.validenumvalue") + case None => JsError("error.expected.validenumvalue") } } case _ => JsError("error.expected.enumstring") @@ -35,7 +35,7 @@ object EnumFormats { case JsString(s) => enum.withNameLowercaseOnlyOption(s) match { case Some(obj) => JsSuccess(obj) - case None => JsError("error.expected.validenumvalue") + case None => JsError("error.expected.validenumvalue") } case _ => JsError("error.expected.enumstring") } @@ -47,59 +47,59 @@ object EnumFormats { case JsString(s) => enum.withNameUppercaseOnlyOption(s) match { case Some(obj) => JsSuccess(obj) - case None => JsError("error.expected.validenumvalue") + case None => JsError("error.expected.validenumvalue") } case _ => JsError("error.expected.enumstring") } } /** - * Returns a Json writes for a given enum [[Enum]] - */ + * Returns a Json writes for a given enum [[Enum]] + */ def writes[A <: EnumEntry](enum: Enum[A]): Writes[A] = new Writes[A] { def writes(v: A): JsValue = JsString(v.entryName) } /** - * Returns a Json writes for a given enum [[Enum]] and transforms it to lower case - */ + * Returns a Json writes for a given enum [[Enum]] and transforms it to lower case + */ def writesLowercaseOnly[A <: EnumEntry](enum: Enum[A]): Writes[A] = new Writes[A] { def writes(v: A): JsValue = JsString(v.entryName.toLowerCase) } /** - * Returns a Json writes for a given enum [[Enum]] and transforms it to upper case - */ + * Returns a Json writes for a given enum [[Enum]] and transforms it to upper case + */ def writesUppercaseOnly[A <: EnumEntry](enum: Enum[A]): Writes[A] = new Writes[A] { def writes(v: A): JsValue = JsString(v.entryName.toUpperCase) } /** - * Returns a Json format for a given enum [[Enum]] - * - * @param enum The enum - * @param insensitive bind in a case-insensitive way, defaults to false - */ + * Returns a Json format for a given enum [[Enum]] + * + * @param enum The enum + * @param insensitive bind in a case-insensitive way, defaults to false + */ def formats[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Format[A] = { Format(reads(enum, insensitive), writes(enum)) } /** - * Returns a Json format for a given enum [[Enum]] for handling lower case transformations - * - * @param enum The enum - */ + * Returns a Json format for a given enum [[Enum]] for handling lower case transformations + * + * @param enum The enum + */ def formatsLowerCaseOnly[A <: EnumEntry](enum: Enum[A]): Format[A] = { Format(readsLowercaseOnly(enum), writesLowercaseOnly(enum)) } /** - * Returns a Json format for a given enum [[Enum]] for handling upper case transformations - * - * @param enum The enum - */ + * Returns a Json format for a given enum [[Enum]] for handling upper case transformations + * + * @param enum The enum + */ def formatsUppercaseOnly[A <: EnumEntry](enum: Enum[A]): Format[A] = { Format(readsUppercaseOnly(enum), writesUppercaseOnly(enum)) } diff --git a/enumeratum-play-json/src/main/scala/enumeratum/values/EnumFormats.scala b/enumeratum-play-json/src/main/scala/enumeratum/values/EnumFormats.scala index aa1a8ab9..2fdeeeb2 100644 --- a/enumeratum-play-json/src/main/scala/enumeratum/values/EnumFormats.scala +++ b/enumeratum-play-json/src/main/scala/enumeratum/values/EnumFormats.scala @@ -3,57 +3,65 @@ package enumeratum.values import play.api.libs.json._ /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ object EnumFormats { /** - * Returns a Reads for the provided ValueEnum based on the given base Reads for the Enum's value type - */ + * Returns a Reads for the provided ValueEnum based on the given base Reads for the Enum's value type + */ def reads[ValueType, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType])( - implicit baseReads: Reads[ValueType]): Reads[EntryType] = + enum: ValueEnum[ValueType, EntryType] + )( + implicit + baseReads: Reads[ValueType] + ): Reads[EntryType] = new Reads[EntryType] { def reads(json: JsValue): JsResult[EntryType] = baseReads.reads(json).flatMap { s => val maybeBound = enum.withValueOpt(s) maybeBound match { case Some(obj) => JsSuccess(obj) - case None => JsError("error.expected.validenumvalue") + case None => JsError("error.expected.validenumvalue") } } } /** - * Returns a Writes for the provided ValueEnum based on the given base Writes for the Enum's value type - */ + * Returns a Writes for the provided ValueEnum based on the given base Writes for the Enum's value type + */ def writes[ValueType, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType])( - implicit baseWrites: Writes[ValueType]): Writes[EntryType] = + enum: ValueEnum[ValueType, EntryType] + )( + implicit + baseWrites: Writes[ValueType] + ): Writes[EntryType] = new Writes[EntryType] { def writes(o: EntryType): JsValue = baseWrites.writes(o.value) } /** - * Returns a Formats for the provided ValueEnum based on the given base Reads and Writes for the Enum's value type - */ + * Returns a Formats for the provided ValueEnum based on the given base Reads and Writes for the Enum's value type + */ def formats[ValueType, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType])(implicit baseReads: Reads[ValueType], - baseWrites: Writes[ValueType]): Format[EntryType] = { + enum: ValueEnum[ValueType, EntryType] + )(implicit + baseReads: Reads[ValueType], + baseWrites: Writes[ValueType]): Format[EntryType] = { Format(reads(enum), writes(enum)) } /** - * Format for Char - */ + * Format for Char + */ implicit val charFormat: Format[Char] = new Format[Char] { def writes(o: Char): JsValue = JsString(s"$o") def reads(json: JsValue): JsResult[Char] = json match { case JsString(s) if s.length == 1 => JsSuccess(s.charAt(0)) - case _ => JsError("error.expected.singleChar") + case _ => JsError("error.expected.singleChar") } } diff --git a/enumeratum-play-json/src/main/scala/enumeratum/values/PlayJsonValueEnum.scala b/enumeratum-play-json/src/main/scala/enumeratum/values/PlayJsonValueEnum.scala index 2a3c6b2d..559a9bcc 100644 --- a/enumeratum-play-json/src/main/scala/enumeratum/values/PlayJsonValueEnum.scala +++ b/enumeratum-play-json/src/main/scala/enumeratum/values/PlayJsonValueEnum.scala @@ -4,63 +4,63 @@ import play.api.libs.json._ import EnumFormats.charFormat /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ trait PlayJsonValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { enum: ValueEnum[ValueType, EntryType] => /** - * Implicit JSON format for the entries of this enum - */ + * Implicit JSON format for the entries of this enum + */ implicit def format: Format[EntryType] } /** - * Enum implementation for Int enum members that contains an implicit Play JSON Format - */ + * Enum implementation for Int enum members that contains an implicit Play JSON Format + */ trait IntPlayJsonValueEnum[EntryType <: IntEnumEntry] extends PlayJsonValueEnum[Int, EntryType] { this: IntEnum[EntryType] => implicit val format: Format[EntryType] = EnumFormats.formats(this) } /** - * Enum implementation for Long enum members that contains an implicit Play JSON Format - */ + * Enum implementation for Long enum members that contains an implicit Play JSON Format + */ trait LongPlayJsonValueEnum[EntryType <: LongEnumEntry] extends PlayJsonValueEnum[Long, EntryType] { this: LongEnum[EntryType] => implicit val format: Format[EntryType] = EnumFormats.formats(this) } /** - * Enum implementation for Short enum members that contains an implicit Play JSON Format - */ + * Enum implementation for Short enum members that contains an implicit Play JSON Format + */ trait ShortPlayJsonValueEnum[EntryType <: ShortEnumEntry] extends PlayJsonValueEnum[Short, EntryType] { this: ShortEnum[EntryType] => implicit val format: Format[EntryType] = EnumFormats.formats(this) } /** - * Enum implementation for String enum members that contains an implicit Play JSON Format - */ + * Enum implementation for String enum members that contains an implicit Play JSON Format + */ trait StringPlayJsonValueEnum[EntryType <: StringEnumEntry] extends PlayJsonValueEnum[String, EntryType] { this: StringEnum[EntryType] => implicit val format: Format[EntryType] = EnumFormats.formats(this) } /** - * Enum implementation for Char enum members that contains an implicit Play JSON Format - */ + * Enum implementation for Char enum members that contains an implicit Play JSON Format + */ trait CharPlayJsonValueEnum[EntryType <: CharEnumEntry] extends PlayJsonValueEnum[Char, EntryType] { this: CharEnum[EntryType] => implicit val format: Format[EntryType] = EnumFormats.formats(this) } /** - * Enum implementation for Byte enum members that contains an implicit Play JSON Format - */ + * Enum implementation for Byte enum members that contains an implicit Play JSON Format + */ trait BytePlayJsonValueEnum[EntryType <: ByteEnumEntry] extends PlayJsonValueEnum[Byte, EntryType] { this: ByteEnum[EntryType] => implicit val format: Format[EntryType] = EnumFormats.formats(this) diff --git a/enumeratum-play-json/src/test/scala/enumeratum/Dummy.scala b/enumeratum-play-json/src/test/scala/enumeratum/Dummy.scala index 3ea1266b..d20a06e6 100644 --- a/enumeratum-play-json/src/test/scala/enumeratum/Dummy.scala +++ b/enumeratum-play-json/src/test/scala/enumeratum/Dummy.scala @@ -1,8 +1,8 @@ package enumeratum /** - * Created by Lloyd on 2/4/15. - */ + * Created by Lloyd on 2/4/15. + */ sealed trait Dummy extends EnumEntry object Dummy extends Enum[Dummy] with PlayJsonEnum[Dummy] { case object A extends Dummy diff --git a/enumeratum-play-json/src/test/scala/enumeratum/EnumFormatsSpec.scala b/enumeratum-play-json/src/test/scala/enumeratum/EnumFormatsSpec.scala index 6303d319..422c5390 100644 --- a/enumeratum-play-json/src/test/scala/enumeratum/EnumFormatsSpec.scala +++ b/enumeratum-play-json/src/test/scala/enumeratum/EnumFormatsSpec.scala @@ -1,7 +1,7 @@ package enumeratum import org.scalatest.OptionValues._ -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } import play.api.libs.json._ class EnumFormatsSpec extends FunSpec with Matchers { @@ -10,7 +10,7 @@ class EnumFormatsSpec extends FunSpec with Matchers { descriptor = "normal operation", reads = EnumFormats.reads(Dummy), readSuccessExpectations = Map("A" -> Dummy.A), - readErrors = Map("C" -> Seq("error.expected.validenumvalue")), + readErrors = Map("C" -> Seq("error.expected.validenumvalue")), writes = EnumFormats.writes(Dummy), writeExpectations = Map(Dummy.A -> "A"), formats = EnumFormats.formats(Dummy) @@ -69,13 +69,13 @@ class EnumFormatsSpec extends FunSpec with Matchers { ) private def testScenario( - descriptor: String, - reads: Reads[Dummy], - readSuccessExpectations: Map[String, Dummy], - readErrors: Map[String, Seq[String]], - writes: Writes[Dummy], - writeExpectations: Map[Dummy, String], - formats: Format[Dummy] + descriptor: String, + reads: Reads[Dummy], + readSuccessExpectations: Map[String, Dummy], + readErrors: Map[String, Seq[String]], + writes: Writes[Dummy], + writeExpectations: Map[Dummy, String], + formats: Format[Dummy] ): Unit = describe(descriptor) { testReads(reads, readSuccessExpectations, readErrors) testWrites(writes, writeExpectations) @@ -83,18 +83,18 @@ class EnumFormatsSpec extends FunSpec with Matchers { } /** - * Shared scenarios for testing Reads - */ + * Shared scenarios for testing Reads + */ private def testReads( - reads: Reads[Dummy], - expectedSuccesses: Map[String, Dummy], - expectedErrors: Map[String, Seq[String]] + reads: Reads[Dummy], + expectedSuccesses: Map[String, Dummy], + expectedErrors: Map[String, Seq[String]] ): Unit = describe("Reads") { val expectedFails: Map[JsValue, Seq[String]] = { val withJsValueKeys = expectedErrors.map { case (k, v) => JsString(k) -> v } // Add standard errors withJsValueKeys ++ Map( - JsNumber(2) -> Seq("error.expected.enumstring"), + JsNumber(2) -> Seq("error.expected.enumstring"), JsString("D") -> Seq("error.expected.validenumvalue") ) } @@ -117,8 +117,8 @@ class EnumFormatsSpec extends FunSpec with Matchers { } /** - * Shared scenarios for testing Writes - */ + * Shared scenarios for testing Writes + */ private def testWrites(writer: Writes[Dummy], expectations: Map[Dummy, String]): Unit = describe("Writes") { it("should create a writes that writes enum values to JsString") { @@ -130,13 +130,13 @@ class EnumFormatsSpec extends FunSpec with Matchers { } /** - * Shared scenarios for testing Formats - */ + * Shared scenarios for testing Formats + */ private def testFormats( - formats: Format[Dummy], - expectedReadSuccesses: Map[String, Dummy], - expectedReadErrors: Map[String, Seq[String]], - expectedWrites: Map[Dummy, String] + formats: Format[Dummy], + expectedReadSuccesses: Map[String, Dummy], + expectedReadErrors: Map[String, Seq[String]], + expectedWrites: Map[Dummy, String] ): Unit = describe("Formats") { testReads(formats, expectedReadSuccesses, expectedReadErrors) testWrites(formats, expectedWrites) diff --git a/enumeratum-play-json/src/test/scala/enumeratum/PlayJsonEnumSpec.scala b/enumeratum-play-json/src/test/scala/enumeratum/PlayJsonEnumSpec.scala index cf2eb1a2..ba3675c4 100644 --- a/enumeratum-play-json/src/test/scala/enumeratum/PlayJsonEnumSpec.scala +++ b/enumeratum-play-json/src/test/scala/enumeratum/PlayJsonEnumSpec.scala @@ -1,7 +1,7 @@ package enumeratum -import org.scalatest.{Matchers, FunSpec} -import play.api.libs.json.{JsNumber, JsString, Json => PlayJson} +import org.scalatest.{ Matchers, FunSpec } +import play.api.libs.json.{ JsNumber, JsString, Json => PlayJson } import org.scalatest.OptionValues._ class PlayJsonEnumSpec extends FunSpec with Matchers { diff --git a/enumeratum-play-json/src/test/scala/enumeratum/values/EnumFormatsSpec.scala b/enumeratum-play-json/src/test/scala/enumeratum/values/EnumFormatsSpec.scala index 2b2fdc80..57e6f7d9 100644 --- a/enumeratum-play-json/src/test/scala/enumeratum/values/EnumFormatsSpec.scala +++ b/enumeratum-play-json/src/test/scala/enumeratum/values/EnumFormatsSpec.scala @@ -2,13 +2,13 @@ package enumeratum.values import org.scalatest._ import EnumFormats._ -import play.api.libs.json.{JsNumber, JsString} +import play.api.libs.json.{ JsNumber, JsString } /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ class EnumFormatsSpec extends FunSpec with Matchers with EnumJsonFormatHelpers { describe(".reads") { diff --git a/enumeratum-play-json/src/test/scala/enumeratum/values/EnumJsonFormatHelpers.scala b/enumeratum-play-json/src/test/scala/enumeratum/values/EnumJsonFormatHelpers.scala index 1e7e0aec..1f499b59 100644 --- a/enumeratum-play-json/src/test/scala/enumeratum/values/EnumJsonFormatHelpers.scala +++ b/enumeratum-play-json/src/test/scala/enumeratum/values/EnumJsonFormatHelpers.scala @@ -5,17 +5,17 @@ import play.api.libs.json._ import org.scalatest.OptionValues._ /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ trait EnumJsonFormatHelpers { this: FunSpec with Matchers => - def testNumericWrites[EntryType <: ValueEnumEntry[ValueType], - ValueType <: AnyVal: Numeric: Writes]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - providedWrites: Option[Writes[EntryType]] = None): Unit = { + def testNumericWrites[EntryType <: ValueEnumEntry[ValueType], ValueType <: AnyVal: Numeric: Writes]( + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + providedWrites: Option[Writes[EntryType]] = None + ): Unit = { val numeric = implicitly[Numeric[ValueType]] testWrites(enumKind, enum, { i: ValueType => JsNumber(numeric.toInt(i)) @@ -23,10 +23,11 @@ trait EnumJsonFormatHelpers { this: FunSpec with Matchers => } def testWrites[EntryType <: ValueEnumEntry[ValueType], ValueType: Writes]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - jsWrapper: ValueType => JsValue, - providedWrites: Option[Writes[EntryType]] = None): Unit = { + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + jsWrapper: ValueType => JsValue, + providedWrites: Option[Writes[EntryType]] = None + ): Unit = { val writes = providedWrites.getOrElse(EnumFormats.writes(enum)) describe(enumKind) { it("should write proper JsValues") { @@ -37,11 +38,11 @@ trait EnumJsonFormatHelpers { this: FunSpec with Matchers => } } - def testNumericReads[EntryType <: ValueEnumEntry[ValueType], - ValueType <: AnyVal: Numeric: Reads]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - providedReads: Option[Reads[EntryType]] = None): Unit = { + def testNumericReads[EntryType <: ValueEnumEntry[ValueType], ValueType <: AnyVal: Numeric: Reads]( + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + providedReads: Option[Reads[EntryType]] = None + ): Unit = { val numeric = implicitly[Numeric[ValueType]] testReads(enumKind, enum, { i: ValueType => JsNumber(numeric.toInt(i)) @@ -49,10 +50,11 @@ trait EnumJsonFormatHelpers { this: FunSpec with Matchers => } def testReads[EntryType <: ValueEnumEntry[ValueType], ValueType: Reads]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - jsWrapper: ValueType => JsValue, - providedReads: Option[Reads[EntryType]] = None): Unit = { + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + jsWrapper: ValueType => JsValue, + providedReads: Option[Reads[EntryType]] = None + ): Unit = { val reads = providedReads.getOrElse(EnumFormats.reads(enum)) describe(enumKind) { it("should read valid values") { @@ -67,20 +69,21 @@ trait EnumJsonFormatHelpers { this: FunSpec with Matchers => } } - def testNumericFormats[EntryType <: ValueEnumEntry[ValueType], - ValueType <: AnyVal: Numeric: Reads: Writes]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - providedFormat: Option[Format[EntryType]] = None): Unit = { + def testNumericFormats[EntryType <: ValueEnumEntry[ValueType], ValueType <: AnyVal: Numeric: Reads: Writes]( + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + providedFormat: Option[Format[EntryType]] = None + ): Unit = { testNumericReads(enumKind, enum, providedFormat) testNumericWrites(enumKind, enum, providedFormat) } def testFormats[EntryType <: ValueEnumEntry[ValueType], ValueType: Reads: Writes]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - jsWrapper: ValueType => JsValue, - providedFormat: Option[Format[EntryType]] = None): Unit = { + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + jsWrapper: ValueType => JsValue, + providedFormat: Option[Format[EntryType]] = None + ): Unit = { val format = providedFormat.getOrElse(EnumFormats.formats(enum)) testReads(enumKind, enum, jsWrapper, Some(format)) testWrites(enumKind, enum, jsWrapper, Some(format)) diff --git a/enumeratum-play-json/src/test/scala/enumeratum/values/JsonDrinks.scala b/enumeratum-play-json/src/test/scala/enumeratum/values/JsonDrinks.scala index aa0a70ee..7ae8aaf1 100644 --- a/enumeratum-play-json/src/test/scala/enumeratum/values/JsonDrinks.scala +++ b/enumeratum-play-json/src/test/scala/enumeratum/values/JsonDrinks.scala @@ -1,18 +1,18 @@ package enumeratum.values /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ sealed abstract class JsonDrinks(val value: Short, name: String) extends ShortEnumEntry case object JsonDrinks extends ShortEnum[JsonDrinks] with ShortPlayJsonValueEnum[JsonDrinks] { case object OrangeJuice extends JsonDrinks(value = 1, name = "oj") - case object AppleJuice extends JsonDrinks(value = 2, name = "aj") - case object Cola extends JsonDrinks(value = 3, name = "cola") - case object Beer extends JsonDrinks(value = 4, name = "beer") + case object AppleJuice extends JsonDrinks(value = 2, name = "aj") + case object Cola extends JsonDrinks(value = 3, name = "cola") + case object Beer extends JsonDrinks(value = 4, name = "beer") val values = findValues diff --git a/enumeratum-play/src/main/scala/enumeratum/Forms.scala b/enumeratum-play/src/main/scala/enumeratum/Forms.scala index f674c448..867a59d2 100644 --- a/enumeratum-play/src/main/scala/enumeratum/Forms.scala +++ b/enumeratum-play/src/main/scala/enumeratum/Forms.scala @@ -1,59 +1,59 @@ package enumeratum import play.api.data.format.Formatter -import play.api.data.{FormError, Forms => PlayForms, Mapping} +import play.api.data.{ FormError, Forms => PlayForms, Mapping } /** - * Created by Lloyd on 2/3/15. - */ + * Created by Lloyd on 2/3/15. + */ object Forms { /** - * Returns an [[Enum]] mapping - * - * For example: - * {{{ - * Form("status" -> maps(Status)) - * }}} - * - * @param enum The enum - * @param insensitive bind in a case-insensitive way, defaults to false - */ + * Returns an [[Enum]] mapping + * + * For example: + * {{{ + * Form("status" -> maps(Status)) + * }}} + * + * @param enum The enum + * @param insensitive bind in a case-insensitive way, defaults to false + */ def enum[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Mapping[A] = PlayForms.of(format(enum, insensitive)) /** - * Returns an [[Enum]] mapping for lower case - * - * For example: - * {{{ - * Form("status" -> maps(Status)) - * }}} - * - * @param enum The enum - */ + * Returns an [[Enum]] mapping for lower case + * + * For example: + * {{{ + * Form("status" -> maps(Status)) + * }}} + * + * @param enum The enum + */ def enumLowerCaseOnly[A <: EnumEntry](enum: Enum[A]): Mapping[A] = PlayForms.of(formatLowercaseOnly(enum)) /** - * Returns an [[Enum]] mapping for upper case - * - * For example: - * {{{ - * Form("status" -> maps(Status)) - * }}} - * - * @param enum The enum - */ + * Returns an [[Enum]] mapping for upper case + * + * For example: + * {{{ + * Form("status" -> maps(Status)) + * }}} + * + * @param enum The enum + */ def enumUppercaseOnly[A <: EnumEntry](enum: Enum[A]): Mapping[A] = PlayForms.of(formatUppercaseOnly(enum)) /** - * Returns a Formatter for [[Enum]] - * - * @param enum The enum - * @param insensitive bind in a case-insensitive way, defaults to false - */ + * Returns a Formatter for [[Enum]] + * + * @param enum The enum + * @param insensitive bind in a case-insensitive way, defaults to false + */ def format[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Formatter[A] = new Formatter[A] { def bind(key: String, data: Map[String, String]) = { @@ -63,7 +63,7 @@ object Forms { else enum.withNameOption(s) maybeBound match { case Some(obj) => Right(obj) - case None => Left(Seq(FormError(key, "error.enum", Nil))) + case None => Left(Seq(FormError(key, "error.enum", Nil))) } } } @@ -71,17 +71,17 @@ object Forms { } /** - * Returns a Formatter for [[Enum]] that transforms to lower case - * - * @param enum The enum - */ + * Returns a Formatter for [[Enum]] that transforms to lower case + * + * @param enum The enum + */ def formatLowercaseOnly[A <: EnumEntry](enum: Enum[A]): Formatter[A] = new Formatter[A] { def bind(key: String, data: Map[String, String]) = { play.api.data.format.Formats.stringFormat.bind(key, data).right.flatMap { s => enum.withNameLowercaseOnlyOption(s) match { case Some(obj) => Right(obj) - case None => Left(Seq(FormError(key, "error.enum", Nil))) + case None => Left(Seq(FormError(key, "error.enum", Nil))) } } } @@ -90,17 +90,17 @@ object Forms { } /** - * Returns a Formatter for [[Enum]] that transforms to upper case - * - * @param enum The enum - */ + * Returns a Formatter for [[Enum]] that transforms to upper case + * + * @param enum The enum + */ def formatUppercaseOnly[A <: EnumEntry](enum: Enum[A]): Formatter[A] = new Formatter[A] { def bind(key: String, data: Map[String, String]) = { play.api.data.format.Formats.stringFormat.bind(key, data).right.flatMap { s => enum.withNameUppercaseOnlyOption(s) match { case Some(obj) => Right(obj) - case None => Left(Seq(FormError(key, "error.enum", Nil))) + case None => Left(Seq(FormError(key, "error.enum", Nil))) } } } diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayEnum.scala index 7d5b75d2..0f9e2b44 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayEnum.scala @@ -1,22 +1,22 @@ package enumeratum /** - * 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)` - * - */ + * 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)` + * + */ trait PlayEnum[A <: EnumEntry] - extends Enum[A] - with PlayJsonEnum[A] - with PlayPathBindableEnum[A] - with PlayQueryBindableEnum[A] - with PlayFormFieldEnum[A] + extends Enum[A] + with PlayJsonEnum[A] + with PlayPathBindableEnum[A] + with PlayQueryBindableEnum[A] + with PlayFormFieldEnum[A] diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayFormFieldEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayFormFieldEnum.scala index a4789aef..fa6f073f 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayFormFieldEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayFormFieldEnum.scala @@ -5,7 +5,7 @@ import play.api.data.Mapping trait PlayFormFieldEnum[A <: EnumEntry] { self: Enum[A] => /** - * Form field for this enum - */ + * Form field for this enum + */ val formField: Mapping[A] = Forms.enum(self) } diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayLowercaseEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayLowercaseEnum.scala index 38fd588f..cad0174a 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayLowercaseEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayLowercaseEnum.scala @@ -1,22 +1,22 @@ package enumeratum /** - * An Enum that has a lot of the Play-related implicits built-in so you can avoid - * boilerplate. - * - * Note, the binders created here transform to lower case. - * - * 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)` - * - */ + * An Enum that has a lot of the Play-related implicits built-in so you can avoid + * boilerplate. + * + * Note, the binders created here transform to lower case. + * + * 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)` + * + */ trait PlayLowercaseEnum[A <: EnumEntry] - extends Enum[A] - with PlayLowercaseJsonEnum[A] - with PlayLowercasePathBindableEnum[A] - with PlayLowercaseQueryBindableEnum[A] - with PlayLowercaseFormFieldEnum[A] + extends Enum[A] + with PlayLowercaseJsonEnum[A] + with PlayLowercasePathBindableEnum[A] + with PlayLowercaseQueryBindableEnum[A] + with PlayLowercaseFormFieldEnum[A] diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayLowercaseFormFieldEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayLowercaseFormFieldEnum.scala index d3b72adb..807aa521 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayLowercaseFormFieldEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayLowercaseFormFieldEnum.scala @@ -5,7 +5,7 @@ import play.api.data.Mapping trait PlayLowercaseFormFieldEnum[A <: EnumEntry] { self: Enum[A] => /** - * Form field for this enum - */ + * Form field for this enum + */ val formField: Mapping[A] = Forms.enumLowerCaseOnly(self) } diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayLowercasePathBindableEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayLowercasePathBindableEnum.scala index 4d224857..f3ee114e 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayLowercasePathBindableEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayLowercasePathBindableEnum.scala @@ -6,27 +6,27 @@ import play.api.routing.sird.PathBindableExtractor trait PlayLowercasePathBindableEnum[A <: EnumEntry] { self: Enum[A] => /** - * Implicit path binder for Play's default router - */ + * Implicit path binder for Play's default router + */ implicit val pathBindable: PathBindable[A] = UrlBinders.pathBinderLowercaseOnly(self) /** - * Binder for [[play.api.routing.sird]] router - * - * Example: - * - * {{{ - * import play.api.routing.sird._ - * import play.api.routing._ - * import play.api.mvc._ - * - * Router.from { - * case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action { - * Results.Ok(s"$greeting") - * } - * } - * }}} - */ + * Binder for [[play.api.routing.sird]] router + * + * Example: + * + * {{{ + * import play.api.routing.sird._ + * import play.api.routing._ + * import play.api.mvc._ + * + * Router.from { + * case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action { + * Results.Ok(s"$greeting") + * } + * } + * }}} + */ lazy val fromPath = new PathBindableExtractor[A] } diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayPathBindableEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayPathBindableEnum.scala index 2be64943..1d3c3474 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayPathBindableEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayPathBindableEnum.scala @@ -6,26 +6,26 @@ import play.api.routing.sird.PathBindableExtractor trait PlayPathBindableEnum[A <: EnumEntry] { self: Enum[A] => /** - * Implicit path binder for Play's default router - */ + * Implicit path binder for Play's default router + */ implicit val pathBindable: PathBindable[A] = UrlBinders.pathBinder(self) /** - * Binder for [[play.api.routing.sird]] router - * - * Example: - * - * {{{ - * import play.api.routing.sird._ - * import play.api.routing._ - * import play.api.mvc._ - * - * Router.from { - * case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action { - * Results.Ok(s"$greeting") - * } - * } - * }}} - */ + * Binder for [[play.api.routing.sird]] router + * + * Example: + * + * {{{ + * import play.api.routing.sird._ + * import play.api.routing._ + * import play.api.mvc._ + * + * Router.from { + * case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action { + * Results.Ok(s"$greeting") + * } + * } + * }}} + */ lazy val fromPath = new PathBindableExtractor[A] } diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayUppercaseEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayUppercaseEnum.scala index dcfdd579..82959b09 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayUppercaseEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayUppercaseEnum.scala @@ -1,22 +1,22 @@ package enumeratum /** - * An Enum that has a lot of the Play-related implicits built-in so you can avoid - * boilerplate. - * - * Note, the binders created here transform to upper case. - * - * 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)` - * - */ + * An Enum that has a lot of the Play-related implicits built-in so you can avoid + * boilerplate. + * + * Note, the binders created here transform to upper case. + * + * 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)` + * + */ trait PlayUppercaseEnum[A <: EnumEntry] - extends Enum[A] - with PlayUppercaseJsonEnum[A] - with PlayUppercasePathBindableEnum[A] - with PlayUppercaseQueryBindableEnum[A] - with PlayUppercaseFormFieldEnum[A] + extends Enum[A] + with PlayUppercaseJsonEnum[A] + with PlayUppercasePathBindableEnum[A] + with PlayUppercaseQueryBindableEnum[A] + with PlayUppercaseFormFieldEnum[A] diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayUppercaseFormFieldEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayUppercaseFormFieldEnum.scala index ab00c76d..63e2f8e3 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayUppercaseFormFieldEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayUppercaseFormFieldEnum.scala @@ -5,7 +5,7 @@ import play.api.data.Mapping trait PlayUppercaseFormFieldEnum[A <: EnumEntry] { self: Enum[A] => /** - * Form field for this enum - */ + * Form field for this enum + */ val formField: Mapping[A] = Forms.enumUppercaseOnly(self) } diff --git a/enumeratum-play/src/main/scala/enumeratum/PlayUppercasePathBindableEnum.scala b/enumeratum-play/src/main/scala/enumeratum/PlayUppercasePathBindableEnum.scala index b982b4af..a71ed674 100644 --- a/enumeratum-play/src/main/scala/enumeratum/PlayUppercasePathBindableEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/PlayUppercasePathBindableEnum.scala @@ -6,27 +6,27 @@ import play.api.routing.sird.PathBindableExtractor trait PlayUppercasePathBindableEnum[A <: EnumEntry] { self: Enum[A] => /** - * Implicit path binder for Play's default router - */ + * Implicit path binder for Play's default router + */ implicit val pathBindable: PathBindable[A] = UrlBinders.pathBinderUppercaseOnly(self) /** - * Binder for [[play.api.routing.sird]] router - * - * Example: - * - * {{{ - * import play.api.routing.sird._ - * import play.api.routing._ - * import play.api.mvc._ - * - * Router.from { - * case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action { - * Results.Ok(s"$greeting") - * } - * } - * }}} - */ + * Binder for [[play.api.routing.sird]] router + * + * Example: + * + * {{{ + * import play.api.routing.sird._ + * import play.api.routing._ + * import play.api.mvc._ + * + * Router.from { + * case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action { + * Results.Ok(s"$greeting") + * } + * } + * }}} + */ lazy val fromPath = new PathBindableExtractor[A] } diff --git a/enumeratum-play/src/main/scala/enumeratum/UrlBinders.scala b/enumeratum-play/src/main/scala/enumeratum/UrlBinders.scala index e5ecd9ed..579e723d 100644 --- a/enumeratum-play/src/main/scala/enumeratum/UrlBinders.scala +++ b/enumeratum-play/src/main/scala/enumeratum/UrlBinders.scala @@ -4,16 +4,16 @@ import play.api.mvc.PathBindable import play.api.mvc.QueryStringBindable /** - * Created by Lloyd on 2/3/15. - */ + * Created by Lloyd on 2/3/15. + */ object UrlBinders { /** - * Builds a [[PathBindable]] A for a given Enum A - * - * @param enum The enum - * @param insensitive bind in a case-insensitive way, defaults to false - */ + * Builds a [[PathBindable]] A for a given Enum A + * + * @param enum The enum + * @param insensitive bind in a case-insensitive way, defaults to false + */ def pathBinder[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): PathBindable[A] = new PathBindable[A] { def unbind(key: String, value: A): String = value.entryName @@ -23,51 +23,53 @@ object UrlBinders { else enum.withNameOption(value) maybeBound match { case Some(v) => Right(v) - case _ => Left(s"Unknown value supplied for $enum '$value'") + case _ => Left(s"Unknown value supplied for $enum '$value'") } } } /** - * Builds a [[PathBindable]] A for a given Enum A that transforms to lower case - * - * @param enum The enum - */ + * Builds a [[PathBindable]] A for a given Enum A that transforms to lower case + * + * @param enum The enum + */ def pathBinderLowercaseOnly[A <: EnumEntry](enum: Enum[A]): PathBindable[A] = new PathBindable[A] { def unbind(key: String, value: A): String = value.entryName.toLowerCase def bind(key: String, value: String): Either[String, A] = { enum.withNameLowercaseOnlyOption(value) match { case Some(v) => Right(v) - case _ => Left(s"Unknown value supplied for $enum '$value'") + case _ => Left(s"Unknown value supplied for $enum '$value'") } } } /** - * Builds a [[PathBindable]] A for a given Enum A that transforms to upper case - * - * @param enum The enum - */ + * Builds a [[PathBindable]] A for a given Enum A that transforms to upper case + * + * @param enum The enum + */ def pathBinderUppercaseOnly[A <: EnumEntry](enum: Enum[A]): PathBindable[A] = new PathBindable[A] { def unbind(key: String, value: A): String = value.entryName.toUpperCase def bind(key: String, value: String): Either[String, A] = { enum.withNameUppercaseOnlyOption(value) match { case Some(v) => Right(v) - case _ => Left(s"Unknown value supplied for $enum '$value'") + case _ => Left(s"Unknown value supplied for $enum '$value'") } } } /** - * Builds a [[QueryStringBindable]] A for a given Enum A - * - * @param enum The enum - * @param insensitive bind in a case-insensitive way, defaults to false - */ - def queryBinder[A <: EnumEntry](enum: Enum[A], - insensitive: Boolean = false): QueryStringBindable[A] = + * Builds a [[QueryStringBindable]] A for a given Enum A + * + * @param enum The enum + * @param insensitive bind in a case-insensitive way, defaults to false + */ + def queryBinder[A <: EnumEntry]( + enum: Enum[A], + insensitive: Boolean = false + ): QueryStringBindable[A] = new QueryStringBindable[A] { def unbind(key: String, value: A): String = s"$key=${value.entryName}" @@ -79,17 +81,17 @@ object UrlBinders { else enum.withNameOption(p) maybeBound match { case Some(v) => Right(v) - case _ => Left(s"Cannot parse parameter $key as an Enum: $this") + case _ => Left(s"Cannot parse parameter $key as an Enum: $this") } } } } /** - * Builds a [[QueryStringBindable]] A for a given Enum A that transforms to lower case - * - * @param enum The enum - */ + * Builds a [[QueryStringBindable]] A for a given Enum A that transforms to lower case + * + * @param enum The enum + */ def queryBinderLowercaseOnly[A <: EnumEntry](enum: Enum[A]): QueryStringBindable[A] = new QueryStringBindable[A] { @@ -100,17 +102,17 @@ object UrlBinders { params.get(key).flatMap(_.headOption).map { p => enum.withNameLowercaseOnlyOption(p) match { case Some(v) => Right(v) - case _ => Left(s"Cannot parse parameter $key as an Enum: $this") + case _ => Left(s"Cannot parse parameter $key as an Enum: $this") } } } } /** - * Builds a [[QueryStringBindable]] A for a given Enum A that transforms to upper case - * - * @param enum The enum - */ + * Builds a [[QueryStringBindable]] A for a given Enum A that transforms to upper case + * + * @param enum The enum + */ def queryBinderUppercaseOnly[A <: EnumEntry](enum: Enum[A]): QueryStringBindable[A] = new QueryStringBindable[A] { @@ -121,7 +123,7 @@ object UrlBinders { params.get(key).flatMap(_.headOption).map { p => enum.withNameUppercaseOnlyOption(p) match { case Some(v) => Right(v) - case _ => Left(s"Cannot parse parameter $key as an Enum: $this") + case _ => Left(s"Cannot parse parameter $key as an Enum: $this") } } } diff --git a/enumeratum-play/src/main/scala/enumeratum/values/Forms.scala b/enumeratum-play/src/main/scala/enumeratum/values/Forms.scala index fdaeeec0..d4e92d91 100644 --- a/enumeratum-play/src/main/scala/enumeratum/values/Forms.scala +++ b/enumeratum-play/src/main/scala/enumeratum/values/Forms.scala @@ -1,36 +1,34 @@ package enumeratum.values import play.api.data.format.Formatter -import play.api.data.{FormError, Mapping, Forms => PlayForms} +import play.api.data.{ FormError, Mapping, Forms => PlayForms } /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ object Forms { /** - * Returns a [[ValueEnum]] mapping for Play form fields - */ - def enum[ValueType, - EntryType <: ValueEnumEntry[ValueType], - EnumType <: ValueEnum[ValueType, EntryType]](baseFormatter: Formatter[ValueType])( - enum: EnumType): Mapping[EntryType] = { + * Returns a [[ValueEnum]] mapping for Play form fields + */ + def enum[ValueType, EntryType <: ValueEnumEntry[ValueType], EnumType <: ValueEnum[ValueType, EntryType]](baseFormatter: Formatter[ValueType])( + enum: EnumType + ): Mapping[EntryType] = { PlayForms.of(formatter(baseFormatter)(enum)) } - private[this] def formatter[ValueType, - EntryType <: ValueEnumEntry[ValueType], - EnumType <: ValueEnum[ValueType, EntryType]]( - baseFormatter: Formatter[ValueType])(enum: EnumType) = { + private[this] def formatter[ValueType, EntryType <: ValueEnumEntry[ValueType], EnumType <: ValueEnum[ValueType, EntryType]]( + baseFormatter: Formatter[ValueType] + )(enum: EnumType) = { new Formatter[EntryType] { def bind(key: String, data: Map[String, String]): Either[Seq[FormError], EntryType] = baseFormatter.bind(key, data).right.flatMap { s => val maybeBound = enum.withValueOpt(s) maybeBound match { case Some(obj) => Right(obj) - case None => Left(Seq(FormError(key, "error.enum", Nil))) + case None => Left(Seq(FormError(key, "error.enum", Nil))) } } @@ -40,8 +38,8 @@ object Forms { } /** - * Taken from Play 2.4.x implementation - */ + * Taken from Play 2.4.x implementation + */ private[values] val charFormatter: Formatter[Char] = new Formatter[Char] { def bind(key: String, data: Map[String, String]) = data diff --git a/enumeratum-play/src/main/scala/enumeratum/values/PlayFormValueEnum.scala b/enumeratum-play/src/main/scala/enumeratum/values/PlayFormValueEnum.scala index 5ef3438e..fa0f2b34 100644 --- a/enumeratum-play/src/main/scala/enumeratum/values/PlayFormValueEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/values/PlayFormValueEnum.scala @@ -1,73 +1,73 @@ package enumeratum.values -import play.api.data.format.{Formats, Formatter} -import play.api.data.{FormError, Mapping} +import play.api.data.format.{ Formats, Formatter } +import play.api.data.{ FormError, Mapping } /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ sealed trait PlayFormValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { enum: ValueEnum[ValueType, EntryType] => /** - * The [[Formatter]] for binding the ValueType of this ValueEnum. - * - * Used for building the [[Formatter]] for the entries - */ + * The [[Formatter]] for binding the ValueType of this ValueEnum. + * + * Used for building the [[Formatter]] for the entries + */ protected def baseFormatter: Formatter[ValueType] /** - * Field for mapping this enum in Forms - */ + * Field for mapping this enum in Forms + */ lazy val formField: Mapping[EntryType] = Forms.enum(baseFormatter)(enum) } /** - * Form Bindable implicits for IntEnum - */ + * Form Bindable implicits for IntEnum + */ trait IntPlayFormValueEnum[EntryType <: IntEnumEntry] extends PlayFormValueEnum[Int, EntryType] { this: IntEnum[EntryType] => protected val baseFormatter: Formatter[Int] = Formats.intFormat } /** - * Form Bindable implicits for LongEnum - */ + * Form Bindable implicits for LongEnum + */ trait LongPlayFormValueEnum[EntryType <: LongEnumEntry] extends PlayFormValueEnum[Long, EntryType] { this: LongEnum[EntryType] => protected val baseFormatter: Formatter[Long] = Formats.longFormat } /** - * Form Bindable implicits for ShortEnum - */ + * Form Bindable implicits for ShortEnum + */ trait ShortPlayFormValueEnum[EntryType <: ShortEnumEntry] extends PlayFormValueEnum[Short, EntryType] { this: ShortEnum[EntryType] => protected val baseFormatter: Formatter[Short] = Formats.shortFormat } /** - * Form Bindable implicits for StringEnum - */ + * Form Bindable implicits for StringEnum + */ trait StringPlayFormValueEnum[EntryType <: StringEnumEntry] extends PlayFormValueEnum[String, EntryType] { this: StringEnum[EntryType] => protected val baseFormatter: Formatter[String] = Formats.stringFormat } /** - * Form Bindable implicits for CharEnum - */ + * Form Bindable implicits for CharEnum + */ trait CharPlayFormValueEnum[EntryType <: CharEnumEntry] extends PlayFormValueEnum[Char, EntryType] { this: CharEnum[EntryType] => protected val baseFormatter: Formatter[Char] = Forms.charFormatter } /** - * Form Bindable implicits for ByteEnum - */ + * Form Bindable implicits for ByteEnum + */ trait BytePlayFormValueEnum[EntryType <: ByteEnumEntry] extends PlayFormValueEnum[Byte, EntryType] { this: ByteEnum[EntryType] => protected val baseFormatter: Formatter[Byte] = Formats.byteFormat diff --git a/enumeratum-play/src/main/scala/enumeratum/values/PlayPathBindableValueEnum.scala b/enumeratum-play/src/main/scala/enumeratum/values/PlayPathBindableValueEnum.scala index 55afa0dc..6f30a95d 100644 --- a/enumeratum-play/src/main/scala/enumeratum/values/PlayPathBindableValueEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/values/PlayPathBindableValueEnum.scala @@ -4,41 +4,41 @@ import play.api.mvc.PathBindable import play.api.routing.sird.PathBindableExtractor /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ sealed trait PlayPathBindableValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { enum: ValueEnum[ValueType, EntryType] => /** - * Implicit path binder for Play's default router - */ + * Implicit path binder for Play's default router + */ implicit def pathBindable: PathBindable[EntryType] /** - * Binder for [[play.api.routing.sird]] router - * - * Example: - * - * {{{ - * import play.api.routing.sird._ - * import play.api.routing._ - * import play.api.mvc._ - * - * Router.from { - * case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action { - * Results.Ok(s"$greeting") - * } - * } - * }}} - */ + * Binder for [[play.api.routing.sird]] router + * + * Example: + * + * {{{ + * import play.api.routing.sird._ + * import play.api.routing._ + * import play.api.mvc._ + * + * Router.from { + * case GET(p"/hello/${Greeting.fromPath(greeting)}") => Action { + * Results.Ok(s"$greeting") + * } + * } + * }}} + */ lazy val fromPath = new PathBindableExtractor[EntryType] } /** - * Path Bindable implicits for IntEnum - */ + * Path Bindable implicits for IntEnum + */ trait IntPlayPathBindableValueEnum[EntryType <: IntEnumEntry] extends PlayPathBindableValueEnum[Int, EntryType] { this: IntEnum[EntryType] => implicit val pathBindable: PathBindable[EntryType] = @@ -46,8 +46,8 @@ trait IntPlayPathBindableValueEnum[EntryType <: IntEnumEntry] } /** - * Path Bindable implicits for LongEnum - */ + * Path Bindable implicits for LongEnum + */ trait LongPlayPathBindableValueEnum[EntryType <: LongEnumEntry] extends PlayPathBindableValueEnum[Long, EntryType] { this: LongEnum[EntryType] => implicit val pathBindable: PathBindable[EntryType] = @@ -55,8 +55,8 @@ trait LongPlayPathBindableValueEnum[EntryType <: LongEnumEntry] } /** - * Path Bindable implicits for ShortEnum - */ + * Path Bindable implicits for ShortEnum + */ trait ShortPlayPathBindableValueEnum[EntryType <: ShortEnumEntry] extends PlayPathBindableValueEnum[Short, EntryType] { this: ShortEnum[EntryType] => implicit val pathBindable: PathBindable[EntryType] = @@ -64,8 +64,8 @@ trait ShortPlayPathBindableValueEnum[EntryType <: ShortEnumEntry] } /** - * Path Bindable implicits for StringEnum - */ + * Path Bindable implicits for StringEnum + */ trait StringPlayPathBindableValueEnum[EntryType <: StringEnumEntry] extends PlayPathBindableValueEnum[String, EntryType] { this: StringEnum[EntryType] => implicit val pathBindable: PathBindable[EntryType] = @@ -73,8 +73,8 @@ trait StringPlayPathBindableValueEnum[EntryType <: StringEnumEntry] } /** - * Path Bindable implicits for CharEnum - */ + * Path Bindable implicits for CharEnum + */ trait CharPlayPathBindableValueEnum[EntryType <: CharEnumEntry] extends PlayPathBindableValueEnum[Char, EntryType] { this: CharEnum[EntryType] => implicit val pathBindable: PathBindable[EntryType] = @@ -82,8 +82,8 @@ trait CharPlayPathBindableValueEnum[EntryType <: CharEnumEntry] } /** - * Path Bindable implicits for ByteEnum - */ + * Path Bindable implicits for ByteEnum + */ trait BytePlayPathBindableValueEnum[EntryType <: ByteEnumEntry] extends PlayPathBindableValueEnum[Byte, EntryType] { this: ByteEnum[EntryType] => implicit val pathBindable: PathBindable[EntryType] = diff --git a/enumeratum-play/src/main/scala/enumeratum/values/PlayQueryBindableValueEnum.scala b/enumeratum-play/src/main/scala/enumeratum/values/PlayQueryBindableValueEnum.scala index 11184625..51e238f0 100644 --- a/enumeratum-play/src/main/scala/enumeratum/values/PlayQueryBindableValueEnum.scala +++ b/enumeratum-play/src/main/scala/enumeratum/values/PlayQueryBindableValueEnum.scala @@ -4,22 +4,22 @@ import play.api.mvc.QueryStringBindable import play.api.routing.sird.PathBindableExtractor /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ sealed trait PlayQueryBindableValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { enum: ValueEnum[ValueType, EntryType] => /** - * Implicit path binder for Play's default router - */ + * Implicit path binder for Play's default router + */ implicit def queryBindable: QueryStringBindable[EntryType] } /** - * Query Bindable implicits for IntEnum - */ + * Query Bindable implicits for IntEnum + */ trait IntPlayQueryBindableValueEnum[EntryType <: IntEnumEntry] extends PlayQueryBindableValueEnum[Int, EntryType] { this: IntEnum[EntryType] => implicit val queryBindable: QueryStringBindable[EntryType] = @@ -27,8 +27,8 @@ trait IntPlayQueryBindableValueEnum[EntryType <: IntEnumEntry] } /** - * Query Bindable implicits for LongEnum - */ + * Query Bindable implicits for LongEnum + */ trait LongPlayQueryBindableValueEnum[EntryType <: LongEnumEntry] extends PlayQueryBindableValueEnum[Long, EntryType] { this: LongEnum[EntryType] => implicit val queryBindable: QueryStringBindable[EntryType] = @@ -36,8 +36,8 @@ trait LongPlayQueryBindableValueEnum[EntryType <: LongEnumEntry] } /** - * Query Bindable implicits for ShortEnum - */ + * Query Bindable implicits for ShortEnum + */ trait ShortPlayQueryBindableValueEnum[EntryType <: ShortEnumEntry] extends PlayQueryBindableValueEnum[Short, EntryType] { this: ShortEnum[EntryType] => implicit val queryBindable: QueryStringBindable[EntryType] = @@ -45,8 +45,8 @@ trait ShortPlayQueryBindableValueEnum[EntryType <: ShortEnumEntry] } /** - * Query Bindable implicits for StringEnum - */ + * Query Bindable implicits for StringEnum + */ trait StringPlayQueryBindableValueEnum[EntryType <: StringEnumEntry] extends PlayQueryBindableValueEnum[String, EntryType] { this: StringEnum[EntryType] => implicit val queryBindable: QueryStringBindable[EntryType] = @@ -54,8 +54,8 @@ trait StringPlayQueryBindableValueEnum[EntryType <: StringEnumEntry] } /** - * Query Bindable implicits for CharEnum - */ + * Query Bindable implicits for CharEnum + */ trait CharPlayQueryBindableValueEnum[EntryType <: CharEnumEntry] extends PlayQueryBindableValueEnum[Char, EntryType] { this: CharEnum[EntryType] => implicit val queryBindable: QueryStringBindable[EntryType] = @@ -63,8 +63,8 @@ trait CharPlayQueryBindableValueEnum[EntryType <: CharEnumEntry] } /** - * Query Bindable implicits for ByteEnum - */ + * Query Bindable implicits for ByteEnum + */ trait BytePlayQueryBindableValueEnum[EntryType <: ByteEnumEntry] extends PlayQueryBindableValueEnum[Byte, EntryType] { this: ByteEnum[EntryType] => implicit val queryBindable: QueryStringBindable[EntryType] = diff --git a/enumeratum-play/src/main/scala/enumeratum/values/PlayValueEnums.scala b/enumeratum-play/src/main/scala/enumeratum/values/PlayValueEnums.scala index d2c0a56f..7e9e97a1 100644 --- a/enumeratum-play/src/main/scala/enumeratum/values/PlayValueEnums.scala +++ b/enumeratum-play/src/main/scala/enumeratum/values/PlayValueEnums.scala @@ -1,120 +1,120 @@ package enumeratum.values /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ /** - * An IntEnum that has a lot of the Play-related implicits built-in so you can avoid - * boilerplate. - * - * Things included are: - * - * - implicit PathBindable (for binding from request path) - * - implicit QueryStringBindable (for binding from query strings) - * - formField for doing things like `Form("hello" -> MyEnum.formField)` - * - implicit Json format - * - */ + * An IntEnum that has a lot of the Play-related implicits built-in so you can avoid + * boilerplate. + * + * Things included are: + * + * - implicit PathBindable (for binding from request path) + * - implicit QueryStringBindable (for binding from query strings) + * - formField for doing things like `Form("hello" -> MyEnum.formField)` + * - implicit Json format + * + */ trait IntPlayEnum[EnumEntry <: IntEnumEntry] - extends IntEnum[EnumEntry] - with IntPlayPathBindableValueEnum[EnumEntry] - with IntPlayQueryBindableValueEnum[EnumEntry] - with IntPlayFormValueEnum[EnumEntry] - with IntPlayJsonValueEnum[EnumEntry] + extends IntEnum[EnumEntry] + with IntPlayPathBindableValueEnum[EnumEntry] + with IntPlayQueryBindableValueEnum[EnumEntry] + with IntPlayFormValueEnum[EnumEntry] + with IntPlayJsonValueEnum[EnumEntry] /** - * A LongEnum that has a lot of the Play-related implicits built-in so you can avoid - * boilerplate. - * - * Things included are: - * - * - implicit PathBindable (for binding from request path) - * - implicit QueryStringBindable (for binding from query strings) - * - formField for doing things like `Form("hello" -> MyEnum.formField)` - * - implicit Json format - * - */ + * A LongEnum that has a lot of the Play-related implicits built-in so you can avoid + * boilerplate. + * + * Things included are: + * + * - implicit PathBindable (for binding from request path) + * - implicit QueryStringBindable (for binding from query strings) + * - formField for doing things like `Form("hello" -> MyEnum.formField)` + * - implicit Json format + * + */ trait LongPlayEnum[EnumEntry <: LongEnumEntry] - extends LongEnum[EnumEntry] - with LongPlayPathBindableValueEnum[EnumEntry] - with LongPlayQueryBindableValueEnum[EnumEntry] - with LongPlayFormValueEnum[EnumEntry] - with LongPlayJsonValueEnum[EnumEntry] + extends LongEnum[EnumEntry] + with LongPlayPathBindableValueEnum[EnumEntry] + with LongPlayQueryBindableValueEnum[EnumEntry] + with LongPlayFormValueEnum[EnumEntry] + with LongPlayJsonValueEnum[EnumEntry] /** - * A ShortEnum that has a lot of the Play-related implicits built-in so you can avoid - * boilerplate. - * - * Things included are: - * - * - implicit PathBindable (for binding from request path) - * - implicit QueryStringBindable (for binding from query strings) - * - formField for doing things like `Form("hello" -> MyEnum.formField)` - * - implicit Json format - * - */ + * A ShortEnum that has a lot of the Play-related implicits built-in so you can avoid + * boilerplate. + * + * Things included are: + * + * - implicit PathBindable (for binding from request path) + * - implicit QueryStringBindable (for binding from query strings) + * - formField for doing things like `Form("hello" -> MyEnum.formField)` + * - implicit Json format + * + */ trait ShortPlayEnum[EnumEntry <: ShortEnumEntry] - extends ShortEnum[EnumEntry] - with ShortPlayPathBindableValueEnum[EnumEntry] - with ShortPlayQueryBindableValueEnum[EnumEntry] - with ShortPlayFormValueEnum[EnumEntry] - with ShortPlayJsonValueEnum[EnumEntry] + extends ShortEnum[EnumEntry] + with ShortPlayPathBindableValueEnum[EnumEntry] + with ShortPlayQueryBindableValueEnum[EnumEntry] + with ShortPlayFormValueEnum[EnumEntry] + with ShortPlayJsonValueEnum[EnumEntry] /** - * A StringEnum that has a lot of the Play-related implicits built-in so you can avoid - * boilerplate. - * - * Things included are: - * - * - implicit PathBindable (for binding from request path) - * - implicit QueryStringBindable (for binding from query strings) - * - formField for doing things like `Form("hello" -> MyEnum.formField)` - * - implicit Json format - * - */ + * A StringEnum that has a lot of the Play-related implicits built-in so you can avoid + * boilerplate. + * + * Things included are: + * + * - implicit PathBindable (for binding from request path) + * - implicit QueryStringBindable (for binding from query strings) + * - formField for doing things like `Form("hello" -> MyEnum.formField)` + * - implicit Json format + * + */ trait StringPlayEnum[EnumEntry <: StringEnumEntry] - extends StringEnum[EnumEntry] - with StringPlayPathBindableValueEnum[EnumEntry] - with StringPlayQueryBindableValueEnum[EnumEntry] - with StringPlayFormValueEnum[EnumEntry] - with StringPlayJsonValueEnum[EnumEntry] + extends StringEnum[EnumEntry] + with StringPlayPathBindableValueEnum[EnumEntry] + with StringPlayQueryBindableValueEnum[EnumEntry] + with StringPlayFormValueEnum[EnumEntry] + with StringPlayJsonValueEnum[EnumEntry] /** - * A ByteEnum that has a lot of the Play-related implicits built-in so you can avoid - * boilerplate. - * - * Things included are: - * - * - implicit PathBindable (for binding from request path) - * - implicit QueryByteBindable (for binding from query strings) - * - formField for doing things like `Form("hello" -> MyEnum.formField)` - * - implicit Json format - * - */ + * A ByteEnum that has a lot of the Play-related implicits built-in so you can avoid + * boilerplate. + * + * Things included are: + * + * - implicit PathBindable (for binding from request path) + * - implicit QueryByteBindable (for binding from query strings) + * - formField for doing things like `Form("hello" -> MyEnum.formField)` + * - implicit Json format + * + */ trait BytePlayEnum[EnumEntry <: ByteEnumEntry] - extends ByteEnum[EnumEntry] - with BytePlayPathBindableValueEnum[EnumEntry] - with BytePlayQueryBindableValueEnum[EnumEntry] - with BytePlayFormValueEnum[EnumEntry] - with BytePlayJsonValueEnum[EnumEntry] + extends ByteEnum[EnumEntry] + with BytePlayPathBindableValueEnum[EnumEntry] + with BytePlayQueryBindableValueEnum[EnumEntry] + with BytePlayFormValueEnum[EnumEntry] + with BytePlayJsonValueEnum[EnumEntry] /** - * A CharEnum that has a lot of the Play-related implicits built-in so you can avoid - * boilerplate. - * - * Things included are: - * - * - implicit PathBindable (for binding from request path) - * - implicit QueryCharBindable (for binding from query strings) - * - formField for doing things like `Form("hello" -> MyEnum.formField)` - * - implicit Json format - * - */ + * A CharEnum that has a lot of the Play-related implicits built-in so you can avoid + * boilerplate. + * + * Things included are: + * + * - implicit PathBindable (for binding from request path) + * - implicit QueryCharBindable (for binding from query strings) + * - formField for doing things like `Form("hello" -> MyEnum.formField)` + * - implicit Json format + * + */ trait CharPlayEnum[EnumEntry <: CharEnumEntry] - extends CharEnum[EnumEntry] - with CharPlayPathBindableValueEnum[EnumEntry] - with CharPlayQueryBindableValueEnum[EnumEntry] - with CharPlayFormValueEnum[EnumEntry] - with CharPlayJsonValueEnum[EnumEntry] + extends CharEnum[EnumEntry] + with CharPlayPathBindableValueEnum[EnumEntry] + with CharPlayQueryBindableValueEnum[EnumEntry] + with CharPlayFormValueEnum[EnumEntry] + with CharPlayJsonValueEnum[EnumEntry] diff --git a/enumeratum-play/src/main/scala/enumeratum/values/UrlBinders.scala b/enumeratum-play/src/main/scala/enumeratum/values/UrlBinders.scala index 1e469e9f..2cda4cdc 100644 --- a/enumeratum-play/src/main/scala/enumeratum/values/UrlBinders.scala +++ b/enumeratum-play/src/main/scala/enumeratum/values/UrlBinders.scala @@ -1,20 +1,23 @@ package enumeratum.values -import play.api.mvc.{PathBindable, QueryStringBindable} +import play.api.mvc.{ PathBindable, QueryStringBindable } /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ object UrlBinders { /** - * Returns a [[PathBindable]] for the provided ValueEnum and base [[PathBindable]] - */ + * Returns a [[PathBindable]] for the provided ValueEnum and base [[PathBindable]] + */ def pathBinder[ValueType, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType])( - implicit baseBindable: PathBindable[ValueType]): PathBindable[EntryType] = + enum: ValueEnum[ValueType, EntryType] + )( + implicit + baseBindable: PathBindable[ValueType] + ): PathBindable[EntryType] = new PathBindable[EntryType] { def bind(key: String, value: String): Either[String, EntryType] = baseBindable.bind(key, value).right.flatMap { b => @@ -31,11 +34,14 @@ object UrlBinders { } /** - * Returns a [[QueryStringBindable]] for the provided ValueEnum and base [[PathBindable]] - */ + * Returns a [[QueryStringBindable]] for the provided ValueEnum and base [[PathBindable]] + */ def queryBinder[ValueType, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType])( - implicit baseBindable: QueryStringBindable[ValueType]): QueryStringBindable[EntryType] = + enum: ValueEnum[ValueType, EntryType] + )( + implicit + baseBindable: QueryStringBindable[ValueType] + ): QueryStringBindable[EntryType] = new QueryStringBindable[EntryType] { def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, EntryType]] = { baseBindable @@ -44,7 +50,7 @@ object UrlBinders { val maybeBound = enum.withValueOpt(s) maybeBound match { case Some(obj) => Right(obj) - case None => Left(s"Unknown value supplied for $enum '$s'") + case None => Left(s"Unknown value supplied for $enum '$s'") } }) } diff --git a/enumeratum-play/src/test/scala/enumeratum/Dummy.scala b/enumeratum-play/src/test/scala/enumeratum/Dummy.scala index 25ad5f40..c867da9e 100644 --- a/enumeratum-play/src/test/scala/enumeratum/Dummy.scala +++ b/enumeratum-play/src/test/scala/enumeratum/Dummy.scala @@ -1,8 +1,8 @@ package enumeratum /** - * Created by Lloyd on 2/4/15. - */ + * Created by Lloyd on 2/4/15. + */ sealed trait Dummy extends EnumEntry object Dummy extends Enum[Dummy] { case object A extends Dummy diff --git a/enumeratum-play/src/test/scala/enumeratum/FormSpec.scala b/enumeratum-play/src/test/scala/enumeratum/FormSpec.scala index d9382fdc..287828cc 100644 --- a/enumeratum-play/src/test/scala/enumeratum/FormSpec.scala +++ b/enumeratum-play/src/test/scala/enumeratum/FormSpec.scala @@ -5,8 +5,8 @@ import play.api.data.Form import org.scalatest.OptionValues._ /** - * Created by Lloyd on 2/3/15. - */ + * Created by Lloyd on 2/3/15. + */ class FormSpec extends FunSpec with Matchers { import Forms._ diff --git a/enumeratum-play/src/test/scala/enumeratum/Models.scala b/enumeratum-play/src/test/scala/enumeratum/Models.scala index d0f2b86e..6bcc15c5 100644 --- a/enumeratum-play/src/test/scala/enumeratum/Models.scala +++ b/enumeratum-play/src/test/scala/enumeratum/Models.scala @@ -1,8 +1,8 @@ package enumeratum /** - * Created by Lloyd on 2/4/15. - */ + * Created by Lloyd on 2/4/15. + */ sealed trait PlayDummyNormal extends EnumEntry object PlayDummyNormal extends PlayEnum[PlayDummyNormal] { diff --git a/enumeratum-play/src/test/scala/enumeratum/PlayEnumSpec.scala b/enumeratum-play/src/test/scala/enumeratum/PlayEnumSpec.scala index 518b5d9e..de33caca 100644 --- a/enumeratum-play/src/test/scala/enumeratum/PlayEnumSpec.scala +++ b/enumeratum-play/src/test/scala/enumeratum/PlayEnumSpec.scala @@ -2,13 +2,13 @@ package enumeratum import java.security.cert.X509Certificate -import org.scalatest.{FunSpec, Matchers} -import play.api.data.{Form, Mapping} +import org.scalatest.{ FunSpec, Matchers } +import play.api.data.{ Form, Mapping } import play.api.http.HttpVerbs -import play.api.libs.json.{Format, JsNumber, JsString, JsValue, Json => PlayJson} +import play.api.libs.json.{ Format, JsNumber, JsString, JsValue, Json => PlayJson } import org.scalatest.OptionValues._ import org.scalatest.EitherValues._ -import play.api.mvc.{Headers, PathBindable, QueryStringBindable, RequestHeader} +import play.api.mvc.{ Headers, PathBindable, QueryStringBindable, RequestHeader } import play.api.routing.sird.PathBindableExtractor class PlayEnumSpec extends FunSpec with Matchers { @@ -50,14 +50,14 @@ class PlayEnumSpec extends FunSpec with Matchers { ) private def testScenarios[A <: EnumEntry: Format]( - descriptor: String, - enum: Enum[A], - validTransforms: Map[String, A], - expectedFailures: Seq[String], - formMapping: Mapping[A], - pathBindable: PathBindable[A], - pathBindableExtractor: PathBindableExtractor[A], - queryStringBindable: QueryStringBindable[A] + descriptor: String, + enum: Enum[A], + validTransforms: Map[String, A], + expectedFailures: Seq[String], + formMapping: Mapping[A], + pathBindable: PathBindable[A], + pathBindableExtractor: PathBindableExtractor[A], + queryStringBindable: QueryStringBindable[A] ): Unit = describe(descriptor) { testJson() @@ -66,8 +66,10 @@ class PlayEnumSpec extends FunSpec with Matchers { def testJson(): Unit = { - val failures: Seq[JsValue] = expectedFailures.map(JsString) ++ Seq(JsString("AVADSGDSAFA"), - JsNumber(Int.MaxValue)) + val failures: Seq[JsValue] = expectedFailures.map(JsString) ++ Seq( + JsString("AVADSGDSAFA"), + JsNumber(Int.MaxValue) + ) describe("JSON serdes") { @@ -103,7 +105,7 @@ class PlayEnumSpec extends FunSpec with Matchers { def testFormBinding(): Unit = { - val subject = Form("hello" -> formMapping) + val subject = Form("hello" -> formMapping) val expectedErrors = expectedFailures ++ Seq(Int.MaxValue.toString, "12asdf13!") describe("Form binding") { @@ -203,7 +205,7 @@ class PlayEnumSpec extends FunSpec with Matchers { it("should not bind strings not found in the enumeration") { expectedErrors.foreach { v => - queryStringBindable.bind("hello", Map("hello" -> Seq(v))).value shouldBe 'left + queryStringBindable.bind("hello", Map("hello" -> Seq(v))).value shouldBe 'left queryStringBindable.bind("hello", Map("helloz" -> Seq(v))) shouldBe None } } diff --git a/enumeratum-play/src/test/scala/enumeratum/UrlBindersSpec.scala b/enumeratum-play/src/test/scala/enumeratum/UrlBindersSpec.scala index 45d04d00..9d1c4ba5 100644 --- a/enumeratum-play/src/test/scala/enumeratum/UrlBindersSpec.scala +++ b/enumeratum-play/src/test/scala/enumeratum/UrlBindersSpec.scala @@ -6,8 +6,8 @@ import org.scalatest.OptionValues._ import org.scalatest.EitherValues._ /** - * Created by Lloyd on 2/3/15. - */ + * Created by Lloyd on 2/3/15. + */ class UrlBindersSpec extends FunSpec with Matchers { describe(".pathBinder") { @@ -34,12 +34,13 @@ class UrlBindersSpec extends FunSpec with Matchers { val subject = pathBinder(Dummy, true) it( - "should create an enumeration binder that can bind strings corresponding to enum strings, disregarding case") { - subject.bind("hello", "A").right.value shouldBe Dummy.A - subject.bind("hello", "a").right.value shouldBe Dummy.A - subject.bind("hello", "B").right.value shouldBe Dummy.B - subject.bind("hello", "b").right.value shouldBe Dummy.B - } + "should create an enumeration binder that can bind strings corresponding to enum strings, disregarding case" + ) { + subject.bind("hello", "A").right.value shouldBe Dummy.A + subject.bind("hello", "a").right.value shouldBe Dummy.A + subject.bind("hello", "B").right.value shouldBe Dummy.B + subject.bind("hello", "b").right.value shouldBe Dummy.B + } it("should create an enumeration binder that cannot bind strings not found in the enumeration") { subject.bind("hello", "Z").isLeft shouldBe true @@ -66,9 +67,10 @@ class UrlBindersSpec extends FunSpec with Matchers { } it( - "should create an enumeration binder that cannot bind strings that aren't lower case but are mixed case") { - subject.bind("hello", "A").isLeft shouldBe true - } + "should create an enumeration binder that cannot bind strings that aren't lower case but are mixed case" + ) { + subject.bind("hello", "A").isLeft shouldBe true + } it("should create an enumeration binder that can unbind values") { subject.unbind("hello", Dummy.A) shouldBe "a" @@ -91,9 +93,10 @@ class UrlBindersSpec extends FunSpec with Matchers { } it( - "should create an enumeration binder that cannot bind strings that aren't upper case but are mixed case") { - subject.bind("hello", "a").isLeft shouldBe true - } + "should create an enumeration binder that cannot bind strings that aren't upper case but are mixed case" + ) { + subject.bind("hello", "a").isLeft shouldBe true + } it("should create an enumeration binder that can unbind values") { subject.unbind("hello", Dummy.A) shouldBe "A" @@ -107,12 +110,13 @@ class UrlBindersSpec extends FunSpec with Matchers { val subject = queryBinder(Dummy) it( - "should create an enumeration binder that can bind strings corresponding to enum strings regardless of case") { - subject.bind("hello", Map("hello" -> Seq("A"))).value.right.value should be(Dummy.A) - } + "should create an enumeration binder that can bind strings corresponding to enum strings regardless of case" + ) { + subject.bind("hello", Map("hello" -> Seq("A"))).value.right.value should be(Dummy.A) + } it("should create an enumeration binder that cannot bind strings not found in the enumeration") { - subject.bind("hello", Map("hello" -> Seq("Z"))).value should be('left) + subject.bind("hello", Map("hello" -> Seq("Z"))).value should be('left) subject.bind("hello", Map("helloz" -> Seq("A"))) shouldBe None } @@ -128,13 +132,14 @@ class UrlBindersSpec extends FunSpec with Matchers { val subject = queryBinder(Dummy, true) it( - "should create an enumeration binder that can bind strings corresponding to enum strings regardless of case, disregarding case") { - subject.bind("hello", Map("hello" -> Seq("A"))).value.right.value should be(Dummy.A) - subject.bind("hello", Map("hello" -> Seq("a"))).value.right.value should be(Dummy.A) - } + "should create an enumeration binder that can bind strings corresponding to enum strings regardless of case, disregarding case" + ) { + subject.bind("hello", Map("hello" -> Seq("A"))).value.right.value should be(Dummy.A) + subject.bind("hello", Map("hello" -> Seq("a"))).value.right.value should be(Dummy.A) + } it("should create an enumeration binder that cannot bind strings not found in the enumeration") { - subject.bind("hello", Map("hello" -> Seq("Z"))).value should be('left) + subject.bind("hello", Map("hello" -> Seq("Z"))).value should be('left) subject.bind("hello", Map("helloz" -> Seq("A"))) shouldBe None } @@ -150,19 +155,21 @@ class UrlBindersSpec extends FunSpec with Matchers { val subject = queryBinderLowercaseOnly(Dummy) it( - "should create an enumeration binder that can bind strings corresponding to enum strings regardless of case") { - subject.bind("hello", Map("hello" -> Seq("a"))).value.right.value should be(Dummy.A) - } + "should create an enumeration binder that can bind strings corresponding to enum strings regardless of case" + ) { + subject.bind("hello", Map("hello" -> Seq("a"))).value.right.value should be(Dummy.A) + } it("should create an enumeration binder that cannot bind strings not found in the enumeration") { - subject.bind("hello", Map("hello" -> Seq("Z"))).value should be('left) + subject.bind("hello", Map("hello" -> Seq("Z"))).value should be('left) subject.bind("hello", Map("helloz" -> Seq("a"))) shouldBe None } it( - "should create an enumeration binder that cannot bind strings that aren't lower case but are mixed case") { - subject.bind("hello", Map("hello" -> Seq("A"))).value should be('left) - } + "should create an enumeration binder that cannot bind strings that aren't lower case but are mixed case" + ) { + subject.bind("hello", Map("hello" -> Seq("A"))).value should be('left) + } it("should create an enumeration binder that can unbind values") { subject.unbind("hello", Dummy.A) should be("hello=a") @@ -176,19 +183,21 @@ class UrlBindersSpec extends FunSpec with Matchers { val subject = queryBinderUppercaseOnly(Dummy) it( - "should create an enumeration binder that can bind strings corresponding to enum strings regardless of case") { - subject.bind("hello", Map("hello" -> Seq("A"))).value.right.value should be(Dummy.A) - } + "should create an enumeration binder that can bind strings corresponding to enum strings regardless of case" + ) { + subject.bind("hello", Map("hello" -> Seq("A"))).value.right.value should be(Dummy.A) + } it("should create an enumeration binder that cannot bind strings not found in the enumeration") { - subject.bind("hello", Map("hello" -> Seq("Z"))).value should be('left) + subject.bind("hello", Map("hello" -> Seq("Z"))).value should be('left) subject.bind("hello", Map("helloz" -> Seq("A"))) shouldBe None } it( - "should create an enumeration binder that cannot bind strings that aren't upper case but are mixed case") { - subject.bind("hello", Map("hello" -> Seq("a"))).value should be('left) - } + "should create an enumeration binder that cannot bind strings that aren't upper case but are mixed case" + ) { + subject.bind("hello", Map("hello" -> Seq("a"))).value should be('left) + } it("should create an enumeration binder that can unbind values") { subject.unbind("hello", Dummy.A) should be("hello=A") diff --git a/enumeratum-play/src/test/scala/enumeratum/values/PlayValueEnumHelpers.scala b/enumeratum-play/src/test/scala/enumeratum/values/PlayValueEnumHelpers.scala index 2da79973..0a178c62 100644 --- a/enumeratum-play/src/test/scala/enumeratum/values/PlayValueEnumHelpers.scala +++ b/enumeratum-play/src/test/scala/enumeratum/values/PlayValueEnumHelpers.scala @@ -5,26 +5,21 @@ import java.security.cert.X509Certificate import org.scalatest._ import play.api.data.Form import play.api.http.HttpVerbs -import play.api.mvc.{Headers, RequestHeader} +import play.api.mvc.{ Headers, RequestHeader } import org.scalatest.OptionValues._ import org.scalatest.EitherValues._ -import play.api.libs.json.{Format, JsNumber, JsValue} +import play.api.libs.json.{ Format, JsNumber, JsValue } /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ trait PlayValueEnumHelpers extends EnumJsonFormatHelpers { this: FunSpec with Matchers => - def testNumericPlayEnum[EntryType <: ValueEnumEntry[ValueType], - ValueType <: AnyVal: Numeric: Format]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType] with PlayFormValueEnum[ValueType, EntryType] with PlayPathBindableValueEnum[ - ValueType, - EntryType] with PlayQueryBindableValueEnum[ValueType, EntryType] with PlayJsonValueEnum[ - ValueType, - EntryType] + def testNumericPlayEnum[EntryType <: ValueEnumEntry[ValueType], ValueType <: AnyVal: Numeric: Format]( + enumKind: String, + enum: ValueEnum[ValueType, EntryType] with PlayFormValueEnum[ValueType, EntryType] with PlayPathBindableValueEnum[ValueType, EntryType] with PlayQueryBindableValueEnum[ValueType, EntryType] with PlayJsonValueEnum[ValueType, EntryType] ) = { val numeric = implicitly[Numeric[ValueType]] testPlayEnum(enumKind, enum, { i: ValueType => @@ -34,13 +29,9 @@ trait PlayValueEnumHelpers extends EnumJsonFormatHelpers { this: FunSpec with Ma } def testPlayEnum[EntryType <: ValueEnumEntry[ValueType], ValueType: Format]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType] with PlayFormValueEnum[ValueType, EntryType] with PlayPathBindableValueEnum[ - ValueType, - EntryType] with PlayQueryBindableValueEnum[ValueType, EntryType] with PlayJsonValueEnum[ - ValueType, - EntryType], - jsWrapper: ValueType => JsValue + enumKind: String, + enum: ValueEnum[ValueType, EntryType] with PlayFormValueEnum[ValueType, EntryType] with PlayPathBindableValueEnum[ValueType, EntryType] with PlayQueryBindableValueEnum[ValueType, EntryType] with PlayJsonValueEnum[ValueType, EntryType], + jsWrapper: ValueType => JsValue ) = { describe(enumKind) { @@ -145,8 +136,8 @@ trait PlayValueEnumHelpers extends EnumJsonFormatHelpers { this: FunSpec with Ma } it("should not bind strings not found as values in the enumeration") { - subject.bind("hello", Map("hello" -> Seq("Z"))).value shouldBe 'left - subject.bind("hello", Map("hello" -> Seq(s"${Int.MaxValue}"))).value shouldBe 'left + subject.bind("hello", Map("hello" -> Seq("Z"))).value shouldBe 'left + subject.bind("hello", Map("hello" -> Seq(s"${Int.MaxValue}"))).value shouldBe 'left subject.bind("hello", Map("helloz" -> Seq("1"))) shouldBe None } diff --git a/enumeratum-play/src/test/scala/enumeratum/values/PlayValueEnumSpec.scala b/enumeratum-play/src/test/scala/enumeratum/values/PlayValueEnumSpec.scala index 430f0cd6..362366de 100644 --- a/enumeratum-play/src/test/scala/enumeratum/values/PlayValueEnumSpec.scala +++ b/enumeratum-play/src/test/scala/enumeratum/values/PlayValueEnumSpec.scala @@ -1,14 +1,14 @@ package enumeratum.values -import org.scalatest.{FunSpec, Matchers} -import play.api.libs.json.{JsNumber, JsString} +import org.scalatest.{ FunSpec, Matchers } +import play.api.libs.json.{ JsNumber, JsString } import EnumFormats.charFormat /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ class PlayValueEnumSpec extends FunSpec with Matchers with PlayValueEnumHelpers { testNumericPlayEnum("LongPlayEnum", PlayContentType) @@ -31,7 +31,7 @@ case object PlayContentType extends LongPlayEnum[PlayContentType] { val values = findValues - case object Text extends PlayContentType(value = 1L, name = "text") + case object Text extends PlayContentType(value = 1L, name = "text") case object Image extends PlayContentType(value = 2L, name = "image") case object Video extends PlayContentType(value = 3L, name = "video") case object Audio extends PlayContentType(value = 4L, name = "audio") @@ -43,9 +43,9 @@ sealed abstract class PlayDrinks(val value: Short, name: String) extends ShortEn case object PlayDrinks extends ShortPlayEnum[PlayDrinks] { case object OrangeJuice extends PlayDrinks(value = 1, name = "oj") - case object AppleJuice extends PlayDrinks(value = 2, name = "aj") - case object Cola extends PlayDrinks(value = 3, name = "cola") - case object Beer extends PlayDrinks(value = 4, name = "beer") + case object AppleJuice extends PlayDrinks(value = 2, name = "aj") + case object Cola extends PlayDrinks(value = 3, name = "cola") + case object Beer extends PlayDrinks(value = 4, name = "beer") val values = findValues @@ -56,10 +56,10 @@ sealed abstract class PlayLibraryItem(val value: Int, val name: String) extends case object PlayLibraryItem extends IntPlayEnum[PlayLibraryItem] { // A good mix of named, unnamed, named + unordered args - case object Book extends PlayLibraryItem(value = 1, name = "book") - case object Movie extends PlayLibraryItem(name = "movie", value = 2) + case object Book extends PlayLibraryItem(value = 1, name = "book") + case object Movie extends PlayLibraryItem(name = "movie", value = 2) case object Magazine extends PlayLibraryItem(3, "magazine") - case object CD extends PlayLibraryItem(4, name = "cd") + case object CD extends PlayLibraryItem(4, name = "cd") val values = findValues @@ -69,8 +69,8 @@ sealed abstract class PlayOperatingSystem(val value: String) extends StringEnumE case object PlayOperatingSystem extends StringPlayEnum[PlayOperatingSystem] { - case object Linux extends PlayOperatingSystem("linux") - case object OSX extends PlayOperatingSystem("osx") + case object Linux extends PlayOperatingSystem("linux") + case object OSX extends PlayOperatingSystem("osx") case object Windows extends PlayOperatingSystem("windows") case object Android extends PlayOperatingSystem("android") @@ -114,8 +114,8 @@ sealed abstract class PlayBites(val value: Byte) extends ByteEnumEntry object PlayBites extends ByteEnum[PlayBites] with BytePlayEnum[PlayBites] { val values = findValues - case object OneByte extends PlayBites(1) - case object TwoByte extends PlayBites(2) + case object OneByte extends PlayBites(1) + case object TwoByte extends PlayBites(2) case object ThreeByte extends PlayBites(3) - case object FourByte extends PlayBites(4) + case object FourByte extends PlayBites(4) } diff --git a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/EnumHandler.scala b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/EnumHandler.scala index b351ed43..40bc4702 100644 --- a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/EnumHandler.scala +++ b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/EnumHandler.scala @@ -2,96 +2,100 @@ package enumeratum import reactivemongo.bson._ -import scala.util.{Failure, Success} +import scala.util.{ Failure, Success } /** - * Holds BSON reader and writer for [[enumeratum.Enum]] - * - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * Holds BSON reader and writer for [[enumeratum.Enum]] + * + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ object EnumHandler { /** - * Returns a BSONReader for a given enum [[Enum]] - * - * @param enum The enum - * @param insensitive bind in a case-insensitive way, defaults to false - */ - def reader[A <: EnumEntry](enum: Enum[A], - insensitive: Boolean = false): BSONReader[BSONValue, A] = + * Returns a BSONReader for a given enum [[Enum]] + * + * @param enum The enum + * @param insensitive bind in a case-insensitive way, defaults to false + */ + def reader[A <: EnumEntry]( + enum: Enum[A], + insensitive: Boolean = false + ): BSONReader[BSONValue, A] = new BSONReader[BSONValue, A] { override def read(bson: BSONValue): A = { bson match { case BSONString(s) if insensitive => enum.withNameInsensitive(s) - case BSONString(s) => enum.withName(s) - case _ => throw new RuntimeException("String value expected") + case BSONString(s) => enum.withName(s) + case _ => throw new RuntimeException("String value expected") } } } /** - * Returns a BSONReader for a given enum [[Enum]] transformed to lower case - * - * @param enum The enum - */ + * Returns a BSONReader for a given enum [[Enum]] transformed to lower case + * + * @param enum The enum + */ def readerLowercaseOnly[A <: EnumEntry](enum: Enum[A]): BSONReader[BSONValue, A] = new BSONReader[BSONValue, A] { override def read(bson: BSONValue): A = { bson match { case BSONString(s) => enum.withNameLowercaseOnly(s) - case _ => throw new RuntimeException("String value expected") + case _ => throw new RuntimeException("String value expected") } } } /** - * Returns a BSONReader for a given enum [[Enum]] transformed to upper case - * - * @param enum The enum - */ + * Returns a BSONReader for a given enum [[Enum]] transformed to upper case + * + * @param enum The enum + */ def readerUppercaseOnly[A <: EnumEntry](enum: Enum[A]): BSONReader[BSONValue, A] = new BSONReader[BSONValue, A] { override def read(bson: BSONValue): A = { bson match { case BSONString(s) => enum.withNameUppercaseOnly(s) - case _ => throw new RuntimeException("String value expected") + case _ => throw new RuntimeException("String value expected") } } } /** - * Returns a BSONWriter for a given enum [[Enum]] - */ + * Returns a BSONWriter for a given enum [[Enum]] + */ def writer[A <: EnumEntry](enum: Enum[A]): BSONWriter[A, BSONValue] = new BSONWriter[A, BSONValue] { override def write(t: A): BSONValue = BSONString(t.entryName) } /** - * Returns a BSONWriter for a given enum [[Enum]], outputting the value as lower case - */ + * Returns a BSONWriter for a given enum [[Enum]], outputting the value as lower case + */ def writerLowercase[A <: EnumEntry](enum: Enum[A]): BSONWriter[A, BSONValue] = new BSONWriter[A, BSONValue] { override def write(t: A): BSONValue = BSONString(t.entryName.toLowerCase) } /** - * Returns a BSONWriter for a given enum [[Enum]], outputting the value as upper case - */ + * Returns a BSONWriter for a given enum [[Enum]], outputting the value as upper case + */ def writerUppercase[A <: EnumEntry](enum: Enum[A]): BSONWriter[A, BSONValue] = new BSONWriter[A, BSONValue] { override def write(t: A): BSONValue = BSONString(t.entryName.toUpperCase) } /** - * Returns a BSONHandler for a given enum [[Enum]] - * - * @param enum The enum - * @param insensitive bind in a case-insensitive way, defaults to false - */ - def handler[A <: EnumEntry](enum: Enum[A], - insensitive: Boolean = false): BSONHandler[BSONValue, A] = + * Returns a BSONHandler for a given enum [[Enum]] + * + * @param enum The enum + * @param insensitive bind in a case-insensitive way, defaults to false + */ + def handler[A <: EnumEntry]( + enum: Enum[A], + insensitive: Boolean = false + ): BSONHandler[BSONValue, A] = new BSONHandler[BSONValue, A] { val concreteReader = reader(enum, insensitive) val concreteWriter = writer(enum) @@ -102,10 +106,10 @@ object EnumHandler { } /** - * Returns a BSONHandler for a given enum [[Enum]], handling a lower case transformation - * - * @param enum The enum - */ + * Returns a BSONHandler for a given enum [[Enum]], handling a lower case transformation + * + * @param enum The enum + */ def handlerLowercaseOnly[A <: EnumEntry](enum: Enum[A]): BSONHandler[BSONValue, A] = new BSONHandler[BSONValue, A] { val concreteReader = readerLowercaseOnly(enum) @@ -117,10 +121,10 @@ object EnumHandler { } /** - * Returns a BSONHandler for a given enum [[Enum]], handling an upper case transformation - * - * @param enum The enum - */ + * Returns a BSONHandler for a given enum [[Enum]], handling an upper case transformation + * + * @param enum The enum + */ def handlerUppercaseOnly[A <: EnumEntry](enum: Enum[A]): BSONHandler[BSONValue, A] = new BSONHandler[BSONValue, A] { val concreteReader = readerUppercaseOnly(enum) diff --git a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/ReactiveMongoBsonEnum.scala b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/ReactiveMongoBsonEnum.scala index 7c7a4403..a8aeaa3a 100644 --- a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/ReactiveMongoBsonEnum.scala +++ b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/ReactiveMongoBsonEnum.scala @@ -3,9 +3,9 @@ package enumeratum import reactivemongo.bson._ /** - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ trait ReactiveMongoBsonEnum[A <: EnumEntry] { self: Enum[A] => implicit val bsonHandler: BSONHandler[BSONValue, A] = EnumHandler.handler(this) diff --git a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/BSONValueHandlers.scala b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/BSONValueHandlers.scala index cc6d29a2..7e0bd9ae 100644 --- a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/BSONValueHandlers.scala +++ b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/BSONValueHandlers.scala @@ -11,20 +11,21 @@ import reactivemongo.bson.{ } /** - * Created by Lloyd on 5/3/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 5/3/16. + * + * Copyright 2016 + */ /** - * Holds BSONValue to implicits. The ones that come with ReactiveMongo by default are for subclasses like BSONLong, - * but what we want are BSONValue and the Reader/Writer/Handler typeclasses are not covariant. - */ + * Holds BSONValue to implicits. The ones that come with ReactiveMongo by default are for subclasses like BSONLong, + * but what we want are BSONValue and the Reader/Writer/Handler typeclasses are not covariant. + */ object BSONValueHandlers extends BSONValueReads with BSONValueWrites { - implicit def anyBsonHandler[A](implicit reader: BSONReader[BSONValue, A], - writer: BSONWriter[A, BSONValue]) = + implicit def anyBsonHandler[A](implicit + reader: BSONReader[BSONValue, A], + writer: BSONWriter[A, BSONValue]) = new BSONHandler[BSONValue, A] { - def write(t: A): BSONValue = writer.write(t) + def write(t: A): BSONValue = writer.write(t) def read(bson: BSONValue): A = reader.read(bson) } @@ -43,14 +44,14 @@ trait BSONValueReads { implicit val bsonReaderInt = new BSONReader[BSONValue, Int] { def read(bson: BSONValue): Int = bson match { case BSONInteger(x) => x - case _ => throw new RuntimeException(s"Could not convert $bson to Int") + case _ => throw new RuntimeException(s"Could not convert $bson to Int") } } implicit val bsonReaderLong = new BSONReader[BSONValue, Long] { def read(bson: BSONValue): Long = bson match { case BSONLong(x) => x - case _ => throw new RuntimeException(s"Could not convert $bson to Long") + case _ => throw new RuntimeException(s"Could not convert $bson to Long") } } @@ -65,14 +66,14 @@ trait BSONValueReads { implicit val bsonReaderChar = new BSONReader[BSONValue, Char] { def read(bson: BSONValue): Char = bson match { case BSONString(x) if x.length == 1 => x.charAt(0) - case _ => throw new RuntimeException(s"Could not convert $bson to Char") + case _ => throw new RuntimeException(s"Could not convert $bson to Char") } } implicit val bsonReaderByte = new BSONReader[BSONValue, Byte] { def read(bson: BSONValue): Byte = bson match { case BSONInteger(x) => x.toByte - case _ => throw new RuntimeException(s"Could not convert $bson to Byte") + case _ => throw new RuntimeException(s"Could not convert $bson to Byte") } } diff --git a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/EnumHandler.scala b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/EnumHandler.scala index da864b40..f27c5943 100644 --- a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/EnumHandler.scala +++ b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/EnumHandler.scala @@ -2,21 +2,23 @@ package enumeratum.values import reactivemongo.bson._ -import scala.util.{Failure, Success, Try} +import scala.util.{ Failure, Success, Try } /** - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ object EnumHandler { /** - * Returns a BSONReader for the provided ValueEnum based on the given base BSONReader for the Enum's value type - */ + * Returns a BSONReader for the provided ValueEnum based on the given base BSONReader for the Enum's value type + */ def reader[ValueType, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType])( - implicit baseBsonReader: BSONReader[BSONValue, ValueType]) - : BSONReader[BSONValue, EntryType] = new BSONReader[BSONValue, EntryType] { + enum: ValueEnum[ValueType, EntryType] + )( + implicit + baseBsonReader: BSONReader[BSONValue, ValueType] + ): BSONReader[BSONValue, EntryType] = new BSONReader[BSONValue, EntryType] { def read(bson: BSONValue): EntryType = { val value = baseBsonReader.read(bson) enum.withValue(value) @@ -24,27 +26,31 @@ object EnumHandler { } /** - * Returns a BSONWriter for the provided ValueEnum based on the given base BSONWriter for the Enum's value type - */ + * Returns a BSONWriter for the provided ValueEnum based on the given base BSONWriter for the Enum's value type + */ def writer[ValueType, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType])( - implicit baseBsonWriter: BSONWriter[ValueType, BSONValue]) - : BSONWriter[EntryType, BSONValue] = new BSONWriter[EntryType, BSONValue] { + enum: ValueEnum[ValueType, EntryType] + )( + implicit + baseBsonWriter: BSONWriter[ValueType, BSONValue] + ): BSONWriter[EntryType, BSONValue] = new BSONWriter[EntryType, BSONValue] { def write(t: EntryType): BSONValue = baseBsonWriter.write(t.value) } /** - * Returns a BSONHandler for the provided ValueEnum based on the given base BSONReader and BSONWriter for the - * Enum's value type - */ + * Returns a BSONHandler for the provided ValueEnum based on the given base BSONReader and BSONWriter for the + * Enum's value type + */ def handler[ValueType, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType])( - implicit baseBsonHandler: BSONHandler[BSONValue, ValueType]) - : BSONHandler[BSONValue, EntryType] = + enum: ValueEnum[ValueType, EntryType] + )( + implicit + baseBsonHandler: BSONHandler[BSONValue, ValueType] + ): BSONHandler[BSONValue, EntryType] = new BSONHandler[BSONValue, EntryType] { - private val concreteReader = reader(enum) - private val concreteWriter = writer(enum) + private val concreteReader = reader(enum) + private val concreteWriter = writer(enum) def read(bson: BSONValue): EntryType = concreteReader.read(bson) - def write(t: EntryType): BSONValue = concreteWriter.write(t) + def write(t: EntryType): BSONValue = concreteWriter.write(t) } } diff --git a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/ReactiveMongoBsonValueEnum.scala b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/ReactiveMongoBsonValueEnum.scala index 4924a94a..86cefd04 100644 --- a/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/ReactiveMongoBsonValueEnum.scala +++ b/enumeratum-reactivemongo-bson/src/main/scala/enumeratum/values/ReactiveMongoBsonValueEnum.scala @@ -4,21 +4,21 @@ import BSONValueHandlers._ import reactivemongo.bson._ /** - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ sealed trait ReactiveMongoBsonValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { enum: ValueEnum[ValueType, EntryType] => /** - * Implicit BSON handler for the entries of this enum - */ + * Implicit BSON handler for the entries of this enum + */ implicit def bsonHandler: BSONHandler[BSONValue, EntryType] } /** - * Enum implementation for Int enum members that contains an implicit ReactiveMongo BSON Handler - */ + * Enum implementation for Int enum members that contains an implicit ReactiveMongo BSON Handler + */ trait IntReactiveMongoBsonValueEnum[EntryType <: IntEnumEntry] extends ReactiveMongoBsonValueEnum[Int, EntryType] { this: IntEnum[EntryType] => @@ -27,8 +27,8 @@ trait IntReactiveMongoBsonValueEnum[EntryType <: IntEnumEntry] } /** - * Enum implementation for Long enum members that contains an implicit ReactiveMongo BSON Handler - */ + * Enum implementation for Long enum members that contains an implicit ReactiveMongo BSON Handler + */ trait LongReactiveMongoBsonValueEnum[EntryType <: LongEnumEntry] extends ReactiveMongoBsonValueEnum[Long, EntryType] { this: LongEnum[EntryType] => @@ -37,8 +37,8 @@ trait LongReactiveMongoBsonValueEnum[EntryType <: LongEnumEntry] } /** - * Enum implementation for Short enum members that contains an implicit ReactiveMongo BSON Handler - */ + * Enum implementation for Short enum members that contains an implicit ReactiveMongo BSON Handler + */ trait ShortReactiveMongoBsonValueEnum[EntryType <: ShortEnumEntry] extends ReactiveMongoBsonValueEnum[Short, EntryType] { this: ShortEnum[EntryType] => @@ -47,8 +47,8 @@ trait ShortReactiveMongoBsonValueEnum[EntryType <: ShortEnumEntry] } /** - * Enum implementation for String enum members that contains an implicit ReactiveMongo BSON Handler - */ + * Enum implementation for String enum members that contains an implicit ReactiveMongo BSON Handler + */ trait StringReactiveMongoBsonValueEnum[EntryType <: StringEnumEntry] extends ReactiveMongoBsonValueEnum[String, EntryType] { this: StringEnum[EntryType] => @@ -57,8 +57,8 @@ trait StringReactiveMongoBsonValueEnum[EntryType <: StringEnumEntry] } /** - * Enum implementation for Char enum members that contains an implicit ReactiveMongo BSON Handler - */ + * Enum implementation for Char enum members that contains an implicit ReactiveMongo BSON Handler + */ trait CharReactiveMongoBsonValueEnum[EntryType <: CharEnumEntry] extends ReactiveMongoBsonValueEnum[Char, EntryType] { this: CharEnum[EntryType] => @@ -67,8 +67,8 @@ trait CharReactiveMongoBsonValueEnum[EntryType <: CharEnumEntry] } /** - * Enum implementation for Byte enum members that contains an implicit ReactiveMongo BSON Handler - */ + * Enum implementation for Byte enum members that contains an implicit ReactiveMongo BSON Handler + */ trait ByteReactiveMongoBsonValueEnum[EntryType <: ByteEnumEntry] extends ReactiveMongoBsonValueEnum[Byte, EntryType] { this: ByteEnum[EntryType] => diff --git a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/Dummy.scala b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/Dummy.scala index 623fe4a7..82d8a8b4 100644 --- a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/Dummy.scala +++ b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/Dummy.scala @@ -1,10 +1,10 @@ package enumeratum /** - * - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ sealed trait Dummy extends EnumEntry object Dummy extends Enum[Dummy] with ReactiveMongoBsonEnum[Dummy] { case object A extends Dummy diff --git a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/EnumBsonHandlerSpec.scala b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/EnumBsonHandlerSpec.scala index 77bc5993..c8540726 100644 --- a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/EnumBsonHandlerSpec.scala +++ b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/EnumBsonHandlerSpec.scala @@ -1,14 +1,14 @@ package enumeratum import org.scalatest.OptionValues._ -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } import reactivemongo.bson._ /** - * - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ class EnumBsonHandlerSpec extends FunSpec with Matchers { testScenario( @@ -52,13 +52,13 @@ class EnumBsonHandlerSpec extends FunSpec with Matchers { ) private def testScenario( - descriptor: String, - reader: BSONReader[BSONValue, Dummy], - expectedReadSuccesses: Map[String, Dummy], - expectedReadFails: Seq[String], - writer: BSONWriter[Dummy, BSONValue], - expectedWrites: Map[Dummy, String], - handler: BSONHandler[BSONValue, Dummy] + descriptor: String, + reader: BSONReader[BSONValue, Dummy], + expectedReadSuccesses: Map[String, Dummy], + expectedReadFails: Seq[String], + writer: BSONWriter[Dummy, BSONValue], + expectedWrites: Map[Dummy, String], + handler: BSONHandler[BSONValue, Dummy] ): Unit = describe(descriptor) { val expectedReadErrors = { diff --git a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/ReactiveMongoBsonEnumSpec.scala b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/ReactiveMongoBsonEnumSpec.scala index 8b0c3f5c..a226e471 100644 --- a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/ReactiveMongoBsonEnumSpec.scala +++ b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/ReactiveMongoBsonEnumSpec.scala @@ -1,14 +1,14 @@ package enumeratum -import org.scalatest.{FunSpec, Matchers} +import org.scalatest.{ FunSpec, Matchers } import reactivemongo.bson._ import org.scalatest.OptionValues._ /** - * - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ class ReactiveMongoBsonEnumSpec extends FunSpec with Matchers { describe("BSON serdes") { diff --git a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/BsonEnums.scala b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/BsonEnums.scala index fdc58713..b8c9436c 100644 --- a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/BsonEnums.scala +++ b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/BsonEnums.scala @@ -1,9 +1,9 @@ package enumeratum.values /** - * @author Alessandro Lacava - * @since 2016-04-23 - */ + * @author Alessandro Lacava + * @since 2016-04-23 + */ sealed abstract class BsonDrinks(val value: Short, name: String) extends ShortEnumEntry case object BsonDrinks @@ -11,9 +11,9 @@ case object BsonDrinks with ShortReactiveMongoBsonValueEnum[BsonDrinks] { case object OrangeJuice extends BsonDrinks(value = 1, name = "oj") - case object AppleJuice extends BsonDrinks(value = 2, name = "aj") - case object Cola extends BsonDrinks(value = 3, name = "cola") - case object Beer extends BsonDrinks(value = 4, name = "beer") + case object AppleJuice extends BsonDrinks(value = 2, name = "aj") + case object Cola extends BsonDrinks(value = 3, name = "cola") + case object Beer extends BsonDrinks(value = 4, name = "beer") val values = findValues @@ -27,7 +27,7 @@ case object BsonContentType val values = findValues - case object Text extends BsonContentType(value = 1L, name = "text") + case object Text extends BsonContentType(value = 1L, name = "text") case object Image extends BsonContentType(value = 2L, name = "image") case object Video extends BsonContentType(value = 3L, name = "video") case object Audio extends BsonContentType(value = 4L, name = "audio") @@ -41,10 +41,10 @@ case object BsonLibraryItem with IntReactiveMongoBsonValueEnum[BsonLibraryItem] { // A good mix of named, unnamed, named + unordered args - case object Book extends BsonLibraryItem(value = 1, name = "book") - case object Movie extends BsonLibraryItem(name = "movie", value = 2) + case object Book extends BsonLibraryItem(value = 1, name = "book") + case object Movie extends BsonLibraryItem(name = "movie", value = 2) case object Magazine extends BsonLibraryItem(3, "magazine") - case object CD extends BsonLibraryItem(4, name = "cd") + case object CD extends BsonLibraryItem(4, name = "cd") val values = findValues @@ -56,8 +56,8 @@ case object BsonOperatingSystem extends StringEnum[BsonOperatingSystem] with StringReactiveMongoBsonValueEnum[BsonOperatingSystem] { - case object Linux extends BsonOperatingSystem("linux") - case object OSX extends BsonOperatingSystem("osx") + case object Linux extends BsonOperatingSystem("linux") + case object OSX extends BsonOperatingSystem("osx") case object Windows extends BsonOperatingSystem("windows") case object Android extends BsonOperatingSystem("android") @@ -85,8 +85,8 @@ sealed abstract class BsonBites(val value: Byte) extends ByteEnumEntry object BsonBites extends ByteEnum[BsonBites] with ByteReactiveMongoBsonValueEnum[BsonBites] { val values = findValues - case object OneByte extends BsonBites(1) - case object TwoByte extends BsonBites(2) + case object OneByte extends BsonBites(1) + case object TwoByte extends BsonBites(2) case object ThreeByte extends BsonBites(3) - case object FourByte extends BsonBites(4) + case object FourByte extends BsonBites(4) } diff --git a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/EnumBsonHandlerHelpers.scala b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/EnumBsonHandlerHelpers.scala index 68955fce..7d92eae7 100644 --- a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/EnumBsonHandlerHelpers.scala +++ b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/EnumBsonHandlerHelpers.scala @@ -4,15 +4,15 @@ import org.scalatest._ import reactivemongo.bson._ /** - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ trait EnumBsonHandlerHelpers { this: FunSpec with Matchers => def testWriter[EntryType <: ValueEnumEntry[ValueType], ValueType]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - providedWriter: Option[BSONWriter[EntryType, BSONValue]] = None + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + providedWriter: Option[BSONWriter[EntryType, BSONValue]] = None )(implicit baseHandler: BSONHandler[BSONValue, ValueType]): Unit = { val writer = providedWriter.getOrElse(EnumHandler.writer(enum)) describe(enumKind) { @@ -25,9 +25,9 @@ trait EnumBsonHandlerHelpers { this: FunSpec with Matchers => } def testReader[EntryType <: ValueEnumEntry[ValueType], ValueType]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - providedReader: Option[BSONReader[BSONValue, EntryType]] = None + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + providedReader: Option[BSONReader[BSONValue, EntryType]] = None )(implicit baseHandler: BSONHandler[BSONValue, ValueType]): Unit = { val reader = providedReader.getOrElse(EnumHandler.reader(enum)) describe(enumKind) { @@ -44,9 +44,9 @@ trait EnumBsonHandlerHelpers { this: FunSpec with Matchers => } def testHandler[EntryType <: ValueEnumEntry[ValueType], ValueType]( - enumKind: String, - enum: ValueEnum[ValueType, EntryType], - providedHandler: Option[BSONHandler[BSONValue, EntryType]] = None + enumKind: String, + enum: ValueEnum[ValueType, EntryType], + providedHandler: Option[BSONHandler[BSONValue, EntryType]] = None )(implicit baseHandler: BSONHandler[BSONValue, ValueType]): Unit = { val handler = providedHandler.getOrElse(EnumHandler.handler(enum)) describe(s"$enumKind Handler") { diff --git a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/EnumBsonHandlerSpec.scala b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/EnumBsonHandlerSpec.scala index 22d5a75b..d23aeb38 100644 --- a/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/EnumBsonHandlerSpec.scala +++ b/enumeratum-reactivemongo-bson/src/test/scala/enumeratum/values/EnumBsonHandlerSpec.scala @@ -4,9 +4,9 @@ import org.scalatest._ import BSONValueHandlers._ /** - * @author Alessandro Lacava (@lambdista) - * @since 2016-04-23 - */ + * @author Alessandro Lacava (@lambdista) + * @since 2016-04-23 + */ class EnumBsonHandlerSpec extends FunSpec with Matchers with EnumBsonHandlerHelpers { describe(".reader") { @@ -38,15 +38,21 @@ class EnumBsonHandlerSpec extends FunSpec with Matchers with EnumBsonHandlerHelp testHandler("ShortEnum", Drinks) testHandler("StringEnum", OperatingSystem) testHandler("ShortReactiveMongoBsonValueEnum", BsonDrinks, Some(BsonDrinks.bsonHandler)) - testHandler("LongReactiveMongoBsonValueEnum", - BsonContentType, - Some(BsonContentType.bsonHandler)) - testHandler("IntReactiveMongoBsonValueEnum", - BsonLibraryItem, - Some(BsonLibraryItem.bsonHandler)) - testHandler("StringReactiveMongoBsonValueEnum", - BsonOperatingSystem, - Some(BsonOperatingSystem.bsonHandler)) + testHandler( + "LongReactiveMongoBsonValueEnum", + BsonContentType, + Some(BsonContentType.bsonHandler) + ) + testHandler( + "IntReactiveMongoBsonValueEnum", + BsonLibraryItem, + Some(BsonLibraryItem.bsonHandler) + ) + testHandler( + "StringReactiveMongoBsonValueEnum", + BsonOperatingSystem, + Some(BsonOperatingSystem.bsonHandler) + ) testHandler("CharReactiveMongoBsonValueEnum", BsonAlphabet, Some(BsonAlphabet.bsonHandler)) testHandler("ByteReactiveMongoBsonValueEnum", BsonBites, Some(BsonBites.bsonHandler)) diff --git a/enumeratum-upickle/src/main/scala/enumeratum/EnrichedPartialFunction.scala b/enumeratum-upickle/src/main/scala/enumeratum/EnrichedPartialFunction.scala index b5871028..8c854007 100644 --- a/enumeratum-upickle/src/main/scala/enumeratum/EnrichedPartialFunction.scala +++ b/enumeratum-upickle/src/main/scala/enumeratum/EnrichedPartialFunction.scala @@ -1,15 +1,15 @@ package enumeratum /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ object EnrichedPartialFunction { /** - * From http://stackoverflow.com/questions/23024626/compose-partial-functions - */ + * From http://stackoverflow.com/questions/23024626/compose-partial-functions + */ implicit class PartialFunctionOps[A, B](val pf: PartialFunction[A, B]) extends AnyVal { def andThenPartial[C](that: PartialFunction[B, C]): PartialFunction[A, C] = Function.unlift(pf.lift(_) flatMap that.lift) diff --git a/enumeratum-upickle/src/main/scala/enumeratum/UPickleEnum.scala b/enumeratum-upickle/src/main/scala/enumeratum/UPickleEnum.scala index 5c519abc..3cb54f9e 100644 --- a/enumeratum-upickle/src/main/scala/enumeratum/UPickleEnum.scala +++ b/enumeratum-upickle/src/main/scala/enumeratum/UPickleEnum.scala @@ -4,8 +4,8 @@ import upickle.default.Aliases.RW import upickle.default.ReadWriter /** - * Enum mix-in with default Reader and Writers defined (case sensitive) - */ + * Enum mix-in with default Reader and Writers defined (case sensitive) + */ trait UPickleEnum[A <: EnumEntry] { self: Enum[A] => import UPickler._ diff --git a/enumeratum-upickle/src/main/scala/enumeratum/UPickler.scala b/enumeratum-upickle/src/main/scala/enumeratum/UPickler.scala index 1b9b30db..083dbba0 100644 --- a/enumeratum-upickle/src/main/scala/enumeratum/UPickler.scala +++ b/enumeratum-upickle/src/main/scala/enumeratum/UPickler.scala @@ -1,16 +1,16 @@ package enumeratum -import upickle.default.{Writer, Reader} +import upickle.default.{ Writer, Reader } import EnrichedPartialFunction._ object UPickler { /** - * Returns a UPickle [[Reader]] for a given [[Enum]] - * - * @param enum the enum you wish to make a Reader for - * @param insensitive whether or not to match case-insensitively - */ + * Returns a UPickle [[Reader]] for a given [[Enum]] + * + * @param enum the enum you wish to make a Reader for + * @param insensitive whether or not to match case-insensitively + */ def reader[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Reader[A] = Reader[A] { val stringReader = implicitly[Reader[String]] @@ -24,10 +24,10 @@ object UPickler { } /** - * Returns a UPickle [[Reader]] for a given [[Enum]] - * - * @param enum the enum you wish to make a Reader for transformed to lower case - */ + * Returns a UPickle [[Reader]] for a given [[Enum]] + * + * @param enum the enum you wish to make a Reader for transformed to lower case + */ def readerLowercaseLower[A <: EnumEntry](enum: Enum[A]): Reader[A] = Reader[A] { val stringReader = implicitly[Reader[String]] @@ -40,10 +40,10 @@ object UPickler { } /** - * Returns a UPickle [[Reader]] for a given [[Enum]] - * - * @param enum the enum you wish to make a Reader for transformed to upper case - */ + * Returns a UPickle [[Reader]] for a given [[Enum]] + * + * @param enum the enum you wish to make a Reader for transformed to upper case + */ def readerUppercaseOnly[A <: EnumEntry](enum: Enum[A]): Reader[A] = Reader[A] { val stringReader = implicitly[Reader[String]] @@ -56,10 +56,10 @@ object UPickler { } /** - * Returns a [[Writer]] for a given [[Enum]] - * - * @param enum [[Enum]] to make a [[Writer]] for - */ + * Returns a [[Writer]] for a given [[Enum]] + * + * @param enum [[Enum]] to make a [[Writer]] for + */ def writer[A <: EnumEntry](enum: Enum[A]): Writer[A] = { val stringWriter = implicitly[Writer[String]] Writer[A] { @@ -68,10 +68,10 @@ object UPickler { } /** - * Returns a [[Writer]] for a given [[Enum]], outputted as lower case - * - * @param enum [[Enum]] to make a [[Writer]] for - */ + * Returns a [[Writer]] for a given [[Enum]], outputted as lower case + * + * @param enum [[Enum]] to make a [[Writer]] for + */ def writerLowercaseOnly[A <: EnumEntry](enum: Enum[A]): Writer[A] = { val stringWriter = implicitly[Writer[String]] Writer[A] { @@ -80,10 +80,10 @@ object UPickler { } /** - * Returns a [[Writer]] for a given [[Enum]], outputted as upper case - * - * @param enum [[Enum]] to make a [[Writer]] for - */ + * Returns a [[Writer]] for a given [[Enum]], outputted as upper case + * + * @param enum [[Enum]] to make a [[Writer]] for + */ def writerUppercaseOnly[A <: EnumEntry](enum: Enum[A]): Writer[A] = { val stringWriter = implicitly[Writer[String]] Writer[A] { diff --git a/enumeratum-upickle/src/main/scala/enumeratum/values/UPickleValueEnum.scala b/enumeratum-upickle/src/main/scala/enumeratum/values/UPickleValueEnum.scala index 419fa97a..08705d67 100644 --- a/enumeratum-upickle/src/main/scala/enumeratum/values/UPickleValueEnum.scala +++ b/enumeratum-upickle/src/main/scala/enumeratum/values/UPickleValueEnum.scala @@ -5,23 +5,23 @@ import upickle.default.ReadWriter import UPickler._ /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ sealed trait UPickleValueEnum[ValueType, EntryType <: ValueEnumEntry[ValueType]] { this: ValueEnum[ValueType, EntryType] => /** - * Implicit UPickle ReadWriter - */ + * Implicit UPickle ReadWriter + */ implicit def uPickleReadWriter: RW[EntryType] } /** - * Enum implementation for Int enum members that contains an implicit UPickle ReadWriter - */ + * Enum implementation for Int enum members that contains an implicit UPickle ReadWriter + */ trait IntUPickleEnum[EntryType <: IntEnumEntry] extends UPickleValueEnum[Int, EntryType] { this: ValueEnum[Int, EntryType] => implicit val uPickleReadWriter: RW[EntryType] = @@ -29,8 +29,8 @@ trait IntUPickleEnum[EntryType <: IntEnumEntry] extends UPickleValueEnum[Int, En } /** - * Enum implementation for Long enum members that contains an implicit UPickle ReadWriter - */ + * Enum implementation for Long enum members that contains an implicit UPickle ReadWriter + */ trait LongUPickleEnum[EntryType <: LongEnumEntry] extends UPickleValueEnum[Long, EntryType] { this: ValueEnum[Long, EntryType] => implicit val uPickleReadWriter: RW[EntryType] = @@ -38,8 +38,8 @@ trait LongUPickleEnum[EntryType <: LongEnumEntry] extends UPickleValueEnum[Long, } /** - * Enum implementation for Short enum members that contains an implicit UPickle ReadWriter - */ + * Enum implementation for Short enum members that contains an implicit UPickle ReadWriter + */ trait ShortUPickleEnum[EntryType <: ShortEnumEntry] extends UPickleValueEnum[Short, EntryType] { this: ValueEnum[Short, EntryType] => implicit val uPickleReadWriter: RW[EntryType] = @@ -47,8 +47,8 @@ trait ShortUPickleEnum[EntryType <: ShortEnumEntry] extends UPickleValueEnum[Sho } /** - * Enum implementation for String enum members that contains an implicit UPickle ReadWriter - */ + * Enum implementation for String enum members that contains an implicit UPickle ReadWriter + */ trait StringUPickleEnum[EntryType <: StringEnumEntry] extends UPickleValueEnum[String, EntryType] { this: ValueEnum[String, EntryType] => implicit val uPickleReadWriter: RW[EntryType] = @@ -56,8 +56,8 @@ trait StringUPickleEnum[EntryType <: StringEnumEntry] extends UPickleValueEnum[S } /** - * Enum implementation for Byte enum members that contains an implicit UPickle ReadWriter - */ + * Enum implementation for Byte enum members that contains an implicit UPickle ReadWriter + */ trait ByteUPickleEnum[EntryType <: ByteEnumEntry] extends UPickleValueEnum[Byte, EntryType] { this: ValueEnum[Byte, EntryType] => implicit val uPickleReadWriter: RW[EntryType] = @@ -65,8 +65,8 @@ trait ByteUPickleEnum[EntryType <: ByteEnumEntry] extends UPickleValueEnum[Byte, } /** - * Enum implementation for Char enum members that contains an implicit UPickle ReadWriter - */ + * Enum implementation for Char enum members that contains an implicit UPickle ReadWriter + */ trait CharUPickleEnum[EntryType <: CharEnumEntry] extends UPickleValueEnum[Char, EntryType] { this: ValueEnum[Char, EntryType] => implicit val uPickleReadWriter: RW[EntryType] = diff --git a/enumeratum-upickle/src/main/scala/enumeratum/values/UPickler.scala b/enumeratum-upickle/src/main/scala/enumeratum/values/UPickler.scala index a63c39b2..6ffc85b2 100644 --- a/enumeratum-upickle/src/main/scala/enumeratum/values/UPickler.scala +++ b/enumeratum-upickle/src/main/scala/enumeratum/values/UPickler.scala @@ -1,20 +1,21 @@ package enumeratum.values import enumeratum.EnrichedPartialFunction._ -import upickle.default.{Reader, Writer} +import upickle.default.{ Reader, Writer } /** - * Created by Lloyd on 4/13/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/13/16. + * + * Copyright 2016 + */ object UPickler { /** - * Returns a Reader for the given ValueEnum - */ + * Returns a Reader for the given ValueEnum + */ def reader[ValueType: Reader, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType]): Reader[EntryType] = { + enum: ValueEnum[ValueType, EntryType] + ): Reader[EntryType] = { val valueReader = implicitly[Reader[ValueType]] Reader[EntryType] { valueReader.read.andThenPartial { @@ -24,10 +25,11 @@ object UPickler { } /** - * Returns a Writer for the given ValueEnum - */ + * Returns a Writer for the given ValueEnum + */ def writer[ValueType: Writer, EntryType <: ValueEnumEntry[ValueType]]( - enum: ValueEnum[ValueType, EntryType]): Writer[EntryType] = { + enum: ValueEnum[ValueType, EntryType] + ): Writer[EntryType] = { val valueWriter = implicitly[Writer[ValueType]] Writer[EntryType] { case member => valueWriter.write(member.value) diff --git a/enumeratum-upickle/src/test/scala/enumeratum/Dummy.scala b/enumeratum-upickle/src/test/scala/enumeratum/Dummy.scala index c129bac3..3905304a 100644 --- a/enumeratum-upickle/src/test/scala/enumeratum/Dummy.scala +++ b/enumeratum-upickle/src/test/scala/enumeratum/Dummy.scala @@ -1,8 +1,8 @@ package enumeratum /** - * Created by Lloyd on 2/4/15. - */ + * Created by Lloyd on 2/4/15. + */ sealed trait Dummy extends EnumEntry object Dummy extends Enum[Dummy] with UPickleEnum[Dummy] { case object A extends Dummy diff --git a/enumeratum-upickle/src/test/scala/enumeratum/UPickleSpec.scala b/enumeratum-upickle/src/test/scala/enumeratum/UPickleSpec.scala index 89884f2d..b606d981 100644 --- a/enumeratum-upickle/src/test/scala/enumeratum/UPickleSpec.scala +++ b/enumeratum-upickle/src/test/scala/enumeratum/UPickleSpec.scala @@ -4,8 +4,8 @@ import org.scalatest._ import upickle.Js /** - * Created by Lloyd on 12/12/15. - */ + * Created by Lloyd on 12/12/15. + */ class UPickleSpec extends FunSpec with Matchers { import Dummy._ diff --git a/enumeratum-upickle/src/test/scala/enumeratum/values/UPicklerSpec.scala b/enumeratum-upickle/src/test/scala/enumeratum/values/UPicklerSpec.scala index 89477a10..254727a6 100644 --- a/enumeratum-upickle/src/test/scala/enumeratum/values/UPicklerSpec.scala +++ b/enumeratum-upickle/src/test/scala/enumeratum/values/UPicklerSpec.scala @@ -2,13 +2,13 @@ package enumeratum.values import org.scalatest._ import upickle.Js -import upickle.default.{readJs, writeJs, Reader, Writer} +import upickle.default.{ readJs, writeJs, Reader, Writer } /** - * Created by Lloyd on 4/14/16. - * - * Copyright 2016 - */ + * Created by Lloyd on 4/14/16. + * + * Copyright 2016 + */ class UPicklerSpec extends FunSpec with Matchers { testPickling("LongUPickleEnum", UPickleContentType) @@ -20,12 +20,12 @@ class UPicklerSpec extends FunSpec with Matchers { testPickling("IntUPickleEnum with values declared as members", UPickleMovieGenre) /** - * Given an enum, tests its JSON reading and writing behaviour, grouping the test results under the given enumKind descriptor - */ - private def testPickling[ValueType: Writer, - EntryType <: ValueEnumEntry[ValueType]: Reader: Writer]( - enumKind: String, - enum: UPickleValueEnum[ValueType, EntryType] with ValueEnum[ValueType, EntryType]) = { + * Given an enum, tests its JSON reading and writing behaviour, grouping the test results under the given enumKind descriptor + */ + private def testPickling[ValueType: Writer, EntryType <: ValueEnumEntry[ValueType]: Reader: Writer]( + enumKind: String, + enum: UPickleValueEnum[ValueType, EntryType] with ValueEnum[ValueType, EntryType] + ) = { describe(enumKind) { describe("Reader") { @@ -70,7 +70,7 @@ case object UPickleContentType val values = findValues - case object Text extends UPickleContentType(value = 1L, name = "text") + case object Text extends UPickleContentType(value = 1L, name = "text") case object Image extends UPickleContentType(value = 2L, name = "image") case object Video extends UPickleContentType(value = 3L, name = "video") case object Audio extends UPickleContentType(value = 4L, name = "audio") @@ -82,9 +82,9 @@ sealed abstract class UPickleDrinks(val value: Short, name: String) extends Shor case object UPickleDrinks extends ShortEnum[UPickleDrinks] with ShortUPickleEnum[UPickleDrinks] { case object OrangeJuice extends UPickleDrinks(value = 1, name = "oj") - case object AppleJuice extends UPickleDrinks(value = 2, name = "aj") - case object Cola extends UPickleDrinks(value = 3, name = "cola") - case object Beer extends UPickleDrinks(value = 4, name = "beer") + case object AppleJuice extends UPickleDrinks(value = 2, name = "aj") + case object Cola extends UPickleDrinks(value = 3, name = "cola") + case object Beer extends UPickleDrinks(value = 4, name = "beer") val values = findValues @@ -97,10 +97,10 @@ case object UPickleLibraryItem with IntUPickleEnum[UPickleLibraryItem] { // A good mix of named, unnamed, named + unordered args - case object Book extends UPickleLibraryItem(value = 1, name = "book") - case object Movie extends UPickleLibraryItem(name = "movie", value = 2) + case object Book extends UPickleLibraryItem(value = 1, name = "book") + case object Movie extends UPickleLibraryItem(name = "movie", value = 2) case object Magazine extends UPickleLibraryItem(3, "magazine") - case object CD extends UPickleLibraryItem(4, name = "cd") + case object CD extends UPickleLibraryItem(4, name = "cd") val values = findValues @@ -112,8 +112,8 @@ case object UPickleOperatingSystem extends StringEnum[UPickleOperatingSystem] with StringUPickleEnum[UPickleOperatingSystem] { - case object Linux extends UPickleOperatingSystem("linux") - case object OSX extends UPickleOperatingSystem("osx") + case object Linux extends UPickleOperatingSystem("linux") + case object OSX extends UPickleOperatingSystem("osx") case object Windows extends UPickleOperatingSystem("windows") case object Android extends UPickleOperatingSystem("android") @@ -161,8 +161,8 @@ sealed abstract class UPickleBites(val value: Byte) extends ByteEnumEntry object UPickleBites extends ByteEnum[UPickleBites] with ByteUPickleEnum[UPickleBites] { val values = findValues - case object OneByte extends UPickleBites(1) - case object TwoByte extends UPickleBites(2) + case object OneByte extends UPickleBites(1) + case object TwoByte extends UPickleBites(2) case object ThreeByte extends UPickleBites(3) - case object FourByte extends UPickleBites(4) + case object FourByte extends UPickleBites(4) } diff --git a/macros/compat/src/main/scala-2.10/enumeratum/ContextUtils.scala b/macros/compat/src/main/scala-2.10/enumeratum/ContextUtils.scala index 54e3bd32..774afc52 100644 --- a/macros/compat/src/main/scala-2.10/enumeratum/ContextUtils.scala +++ b/macros/compat/src/main/scala-2.10/enumeratum/ContextUtils.scala @@ -6,26 +6,27 @@ object ContextUtils { // In 2.10, the constants have Java boxed types at compile time for some reason type CTLong = java.lang.Long - type CTInt = java.lang.Integer + type CTInt = java.lang.Integer type CTChar = java.lang.Character /** - * Returns a TermName - */ + * Returns a TermName + */ def termName(c: Context)(name: String): c.universe.TermName = { c.universe.newTermName(name) } /** - * Returns a companion symbol - */ + * Returns a companion symbol + */ def companion(c: Context)(sym: c.Symbol): c.universe.Symbol = sym.companionSymbol /** - * Returns a PartialFunction for turning symbols into names - */ + * Returns a PartialFunction for turning symbols into names + */ def constructorsToParamNamesPF( - c: Context): PartialFunction[c.universe.Symbol, List[c.universe.Name]] = { + c: Context + ): PartialFunction[c.universe.Symbol, List[c.universe.Name]] = { case m if m.isMethod && m.asMethod.isConstructor => m.asMethod.paramss.flatten.map(_.asTerm.name) } diff --git a/macros/compat/src/main/scala-2.11/enumeratum/ContextUtils.scala b/macros/compat/src/main/scala-2.11/enumeratum/ContextUtils.scala index 333c1337..b0cb6532 100644 --- a/macros/compat/src/main/scala-2.11/enumeratum/ContextUtils.scala +++ b/macros/compat/src/main/scala-2.11/enumeratum/ContextUtils.scala @@ -6,26 +6,27 @@ object ContextUtils { // Constant types type CTLong = Long - type CTInt = Int + type CTInt = Int type CTChar = Char /** - * Returns a TermName - */ + * Returns a TermName + */ def termName(c: Context)(name: String): c.universe.TermName = { c.universe.TermName(name) } /** - * Returns a companion symbol - */ + * Returns a companion symbol + */ def companion(c: Context)(sym: c.Symbol): c.universe.Symbol = sym.companion /** - * Returns a PartialFunction for turning symbols into names - */ + * Returns a PartialFunction for turning symbols into names + */ def constructorsToParamNamesPF( - c: Context): PartialFunction[c.universe.Symbol, List[c.universe.Name]] = { + c: Context + ): PartialFunction[c.universe.Symbol, List[c.universe.Name]] = { case m if m.isConstructor => m.asMethod.paramLists.flatten.map(_.asTerm.name) } diff --git a/macros/src/main/scala/enumeratum/EnumMacros.scala b/macros/src/main/scala/enumeratum/EnumMacros.scala index eb80bd91..b6b37f8b 100644 --- a/macros/src/main/scala/enumeratum/EnumMacros.scala +++ b/macros/src/main/scala/enumeratum/EnumMacros.scala @@ -6,8 +6,8 @@ import scala.util.control.NonFatal object EnumMacros { /** - * Finds any [A] in the current scope and returns an expression for a list of them - */ + * Finds any [A] in the current scope and returns an expression for a list of them + */ def findValuesImpl[A: c.WeakTypeTag](c: Context): c.Expr[IndexedSeq[A]] = { import c.universe._ val typeSymbol = weakTypeOf[A].typeSymbol @@ -17,8 +17,8 @@ object EnumMacros { } /** - * Given an A, provides its companion - */ + * Given an A, provides its companion + */ def materializeEnumImpl[A: c.WeakTypeTag](c: Context) = { import c.universe._ val symbol = weakTypeOf[A].typeSymbol @@ -26,10 +26,10 @@ object EnumMacros { } /** - * Makes sure that we can work with the given type as an enum: - * - * Aborts if the type is not sealed - */ + * Makes sure that we can work with the given type as an enum: + * + * Aborts if the type is not sealed + */ private[enumeratum] def validateType(c: Context)(typeSymbol: c.universe.Symbol): Unit = { if (!typeSymbol.asClass.isSealed) c.abort( @@ -39,15 +39,16 @@ object EnumMacros { } /** - * Finds the actual trees in the current scope that implement objects of the given type - * - * aborts compilation if: - * - * - the implementations are not all objects - * - the current scope is not an object - */ + * Finds the actual trees in the current scope that implement objects of the given type + * + * aborts compilation if: + * + * - the implementations are not all objects + * - the current scope is not an object + */ private[enumeratum] def enclosedSubClassTrees(c: Context)( - typeSymbol: c.universe.Symbol): Seq[c.universe.Tree] = { + typeSymbol: c.universe.Symbol + ): Seq[c.universe.Tree] = { import c.universe._ val enclosingBodySubClassTrees: List[Tree] = try { /* @@ -71,12 +72,13 @@ object EnumMacros { enclosingModule.impl.body.filter { x => try { x.symbol.isModule && - x.symbol.asModule.moduleClass.asClass.baseClasses.contains(typeSymbol) + x.symbol.asModule.moduleClass.asClass.baseClasses.contains(typeSymbol) } catch { case NonFatal(e) => c.warning( c.enclosingPosition, - s"Got an exception, indicating a possible bug in Enumeratum. Message: ${e.getMessage}") + s"Got an exception, indicating a possible bug in Enumeratum. Message: ${e.getMessage}" + ) false } } @@ -88,18 +90,20 @@ object EnumMacros { } /** - * Returns a sequence of symbols for objects that implement the given type - */ + * Returns a sequence of symbols for objects that implement the given type + */ private[enumeratum] def enclosedSubClasses(c: Context)( - typeSymbol: c.universe.Symbol): Seq[c.universe.Symbol] = { + typeSymbol: c.universe.Symbol + ): Seq[c.universe.Symbol] = { enclosedSubClassTrees(c)(typeSymbol).map(_.symbol) } /** - * Builds and returns an expression for an IndexedSeq containing the given symbols - */ + * Builds and returns an expression for an IndexedSeq containing the given symbols + */ private[enumeratum] def buildSeqExpr[A: c.WeakTypeTag](c: Context)( - subclassSymbols: Seq[c.universe.Symbol]) = { + subclassSymbols: Seq[c.universe.Symbol] + ) = { import c.universe._ val resultType = weakTypeOf[A] if (subclassSymbols.isEmpty) { diff --git a/macros/src/main/scala/enumeratum/ValueEnumMacros.scala b/macros/src/main/scala/enumeratum/ValueEnumMacros.scala index 6685eb78..91900648 100644 --- a/macros/src/main/scala/enumeratum/ValueEnumMacros.scala +++ b/macros/src/main/scala/enumeratum/ValueEnumMacros.scala @@ -7,81 +7,86 @@ import scala.reflect.ClassTag object ValueEnumMacros { /** - * Finds ValueEntryType-typed objects in scope that have literal value:Int implementations - * - * Note, requires the ValueEntryType to have a 'value' member that has a literal value - */ + * Finds ValueEntryType-typed objects in scope that have literal value:Int implementations + * + * Note, requires the ValueEntryType to have a 'value' member that has a literal value + */ def findIntValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( - c: Context): c.Expr[IndexedSeq[ValueEntryType]] = { + c: Context + ): c.Expr[IndexedSeq[ValueEntryType]] = { findValueEntriesImpl[ValueEntryType, ContextUtils.CTInt, Int](c)(identity) } /** - * Finds ValueEntryType-typed objects in scope that have literal value:Long implementations - * - * Note, requires the ValueEntryType to have a 'value' member that has a literal value - */ + * Finds ValueEntryType-typed objects in scope that have literal value:Long implementations + * + * Note, requires the ValueEntryType to have a 'value' member that has a literal value + */ def findLongValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( - c: Context): c.Expr[IndexedSeq[ValueEntryType]] = { + c: Context + ): c.Expr[IndexedSeq[ValueEntryType]] = { findValueEntriesImpl[ValueEntryType, ContextUtils.CTLong, Long](c)(identity) } /** - * Finds ValueEntryType-typed objects in scope that have literal value:Short implementations - * - * Note - * - * - requires the ValueEntryType to have a 'value' member that has a literal value - * - the Short value should be a literal Int (do no need to cast .toShort). - */ + * Finds ValueEntryType-typed objects in scope that have literal value:Short implementations + * + * Note + * + * - requires the ValueEntryType to have a 'value' member that has a literal value + * - the Short value should be a literal Int (do no need to cast .toShort). + */ def findShortValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( - c: Context): c.Expr[IndexedSeq[ValueEntryType]] = { + c: Context + ): c.Expr[IndexedSeq[ValueEntryType]] = { findValueEntriesImpl[ValueEntryType, ContextUtils.CTInt, Short](c)(_.toShort) // do a transform because there is no such thing as Short literals } /** - * Finds ValueEntryType-typed objects in scope that have literal value:String implementations - * - * Note - * - * - requires the ValueEntryType to have a 'value' member that has a literal value - */ + * Finds ValueEntryType-typed objects in scope that have literal value:String implementations + * + * Note + * + * - requires the ValueEntryType to have a 'value' member that has a literal value + */ def findStringValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( - c: Context): c.Expr[IndexedSeq[ValueEntryType]] = { + c: Context + ): c.Expr[IndexedSeq[ValueEntryType]] = { findValueEntriesImpl[ValueEntryType, String, String](c)(identity) } /** - * Finds ValueEntryType-typed objects in scope that have literal value:Byte implementations - * - * Note - * - * - requires the ValueEntryType to have a 'value' member that has a literal value - */ + * Finds ValueEntryType-typed objects in scope that have literal value:Byte implementations + * + * Note + * + * - requires the ValueEntryType to have a 'value' member that has a literal value + */ def findByteValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( - c: Context): c.Expr[IndexedSeq[ValueEntryType]] = { + c: Context + ): c.Expr[IndexedSeq[ValueEntryType]] = { findValueEntriesImpl[ValueEntryType, ContextUtils.CTInt, Byte](c)(_.toByte) } /** - * Finds ValueEntryType-typed objects in scope that have literal value:Char implementations - * - * Note - * - * - requires the ValueEntryType to have a 'value' member that has a literal value - */ + * Finds ValueEntryType-typed objects in scope that have literal value:Char implementations + * + * Note + * + * - requires the ValueEntryType to have a 'value' member that has a literal value + */ def findCharValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( - c: Context): c.Expr[IndexedSeq[ValueEntryType]] = { + c: Context + ): c.Expr[IndexedSeq[ValueEntryType]] = { findValueEntriesImpl[ValueEntryType, ContextUtils.CTChar, Char](c)(identity) } /** - * The method that does the heavy lifting. - */ - private[this] def findValueEntriesImpl[ValueEntryType: c.WeakTypeTag, - ValueType: ClassTag, - ProcessedValue](c: Context)( - processFoundValues: ValueType => ProcessedValue): c.Expr[IndexedSeq[ValueEntryType]] = { + * The method that does the heavy lifting. + */ + private[this] def findValueEntriesImpl[ValueEntryType: c.WeakTypeTag, ValueType: ClassTag, ProcessedValue](c: Context)( + processFoundValues: ValueType => ProcessedValue + ): c.Expr[IndexedSeq[ValueEntryType]] = { import c.universe._ val typeSymbol = weakTypeOf[ValueEntryType].typeSymbol EnumMacros.validateType(c)(typeSymbol) @@ -94,7 +99,8 @@ object ValueEnumMacros { val treeWithVals = findValuesForSubclassTrees[ValueType, ProcessedValue](c)( valueEntryTypeConstructorsParams, subclassTrees, - processFoundValues) + processFoundValues + ) // Make sure the processed found value implementations are unique ensureUnique[ProcessedValue](c)(treeWithVals) // Finish by building our Sequence @@ -103,19 +109,22 @@ object ValueEnumMacros { } /** - * Returns a list of TreeWithVal (tree with value of type ProcessedValueType) for the given trees and transformation - * - * Will abort compilation if not all the trees provided have a literal value member/constructor argument - */ + * Returns a list of TreeWithVal (tree with value of type ProcessedValueType) for the given trees and transformation + * + * Will abort compilation if not all the trees provided have a literal value member/constructor argument + */ private[this] def findValuesForSubclassTrees[ValueType: ClassTag, ProcessedValueType]( - c: Context)(valueEntryCTorsParams: List[List[c.universe.Name]], - memberTrees: Seq[c.universe.Tree], - processFoundValues: ValueType => ProcessedValueType) - : Seq[TreeWithVal[c.universe.Tree, ProcessedValueType]] = { + c: Context + )( + valueEntryCTorsParams: List[List[c.universe.Name]], + memberTrees: Seq[c.universe.Tree], + processFoundValues: ValueType => ProcessedValueType + ): Seq[TreeWithVal[c.universe.Tree, ProcessedValueType]] = { val treeWithValues = toTreeWithMaybeVals[ValueType, ProcessedValueType](c)( valueEntryCTorsParams, memberTrees, - processFoundValues) + processFoundValues + ) val (hasValueMember, lacksValueMember) = treeWithValues.partition(_.maybeValue.isDefined) if (lacksValueMember.nonEmpty) { @@ -141,17 +150,17 @@ object ValueEnumMacros { } /** - * Looks through the given trees and tries to find the proper value declaration/constructor argument. - * - * Aborts compilation if the value declaration/constructor is of the wrong type, - */ + * Looks through the given trees and tries to find the proper value declaration/constructor argument. + * + * Aborts compilation if the value declaration/constructor is of the wrong type, + */ private[this] def toTreeWithMaybeVals[ValueType: ClassTag, ProcessedValueType](c: Context)( - valueEntryCTorsParams: List[List[c.universe.Name]], - memberTrees: Seq[c.universe.Tree], - processFoundValues: ValueType => ProcessedValueType) - : Seq[TreeWithMaybeVal[c.universe.Tree, ProcessedValueType]] = { + valueEntryCTorsParams: List[List[c.universe.Name]], + memberTrees: Seq[c.universe.Tree], + processFoundValues: ValueType => ProcessedValueType + ): Seq[TreeWithMaybeVal[c.universe.Tree, ProcessedValueType]] = { import c.universe._ - val classTag = implicitly[ClassTag[ValueType]] + val classTag = implicitly[ClassTag[ValueType]] val valueTerm = ContextUtils.termName(c)("value") // go through all the trees memberTrees.map { declTree => @@ -173,12 +182,13 @@ object ValueEnumMacros { case (`valueTerm`, Literal(Constant(i))) => c.abort( c.enclosingPosition, - s"${declTree.symbol} has a value with the wrong type: $i:${i.getClass}, instead of ${classTag.runtimeClass}.") + s"${declTree.symbol} has a value with the wrong type: $i:${i.getClass}, instead of ${classTag.runtimeClass}." + ) /* * found a (_, NamedArgument(argName, argument)) parameter-named pair where the argument is named "value" and the argument itself is of the right type */ case (_, - AssignOrNamedArg(Ident(`valueTerm`), Literal(Constant(i: ValueType)))) => + AssignOrNamedArg(Ident(`valueTerm`), Literal(Constant(i: ValueType)))) => i /* * found a (_, NamedArgument(argName, argument)) parameter-named pair where the argument is named "value" and the argument itself is of the wrong type @@ -186,7 +196,8 @@ object ValueEnumMacros { case (_, AssignOrNamedArg(Ident(`valueTerm`), Literal(Constant(i)))) => c.abort( c.enclosingPosition, - s"${declTree.symbol} has a value with the wrong type: $i:${i.getClass}, instead of ${classTag.runtimeClass}") + s"${declTree.symbol} has a value with the wrong type: $i:${i.getClass}, instead of ${classTag.runtimeClass}" + ) } } } @@ -202,20 +213,22 @@ object ValueEnumMacros { } /** - * Given a type, finds the constructor params lists for it - */ + * Given a type, finds the constructor params lists for it + */ private[this] def findConstructorParamsLists[ValueEntryType: c.WeakTypeTag]( - c: Context): List[List[c.universe.Name]] = { - val valueEntryTypeTpe = implicitly[c.WeakTypeTag[ValueEntryType]].tpe + c: Context + ): List[List[c.universe.Name]] = { + val valueEntryTypeTpe = implicitly[c.WeakTypeTag[ValueEntryType]].tpe val valueEntryTypeTpeMembers = valueEntryTypeTpe.members valueEntryTypeTpeMembers.collect(ContextUtils.constructorsToParamNamesPF(c)).toList } /** - * Ensures that we have unique values for trees, aborting otherwise with a message indicating which trees have the same symbol - */ + * Ensures that we have unique values for trees, aborting otherwise with a message indicating which trees have the same symbol + */ private[this] def ensureUnique[A](c: Context)( - treeWithVals: Seq[TreeWithVal[c.universe.Tree, A]]): Unit = { + treeWithVals: Seq[TreeWithVal[c.universe.Tree, A]] + ): Unit = { val membersWithValues = treeWithVals.map { treeWithVal => treeWithVal.tree.symbol -> treeWithVal.value } diff --git a/project/build.properties b/project/build.properties index 444fa917..e0cbc71d 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version = 0.13.12 +sbt.version = 0.13.13 \ No newline at end of file diff --git a/project/plugins.sbt b/project/plugins.sbt index 55b4b2a8..3d7bf039 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -2,17 +2,14 @@ resolvers ++= Seq( Classpaths.sbtPluginReleases ) -// for code formatting -addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "0.4.1") +addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.6.0") -addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.4.0") +addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.0") addSbtPlugin("org.scoverage" % "sbt-coveralls" % "1.1.0") addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5") -addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.11") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.13") addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.6") - -addSbtPlugin("com.updateimpact" % "updateimpact-sbt-plugin" % "2.1.1") diff --git a/scala_2_12/note b/scala_2_12/note new file mode 100644 index 00000000..5d4cd90f --- /dev/null +++ b/scala_2_12/note @@ -0,0 +1 @@ +Just a placeholder directory for a "scala_2_12" project definition \ No newline at end of file