Skip to content

Commit ae4a078

Browse files
committed
Merge pull request #11 from lloydmeta/feature/use-name
Feature/use name
2 parents 8e7bcc6 + 6a7bfe7 commit ae4a078

File tree

18 files changed

+60
-43
lines changed

18 files changed

+60
-43
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ jdk:
77
- oraclejdk8
88
scala:
99
- 2.11.6
10-
- 2.10.5
1110
script: "sbt clean coverage test"
1211
after_success: "sbt coveralls"

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ Compatible with Scala 2.10.x and 2.11.x
2020
For basic enumeratum (with no Play support):
2121
```scala
2222
libraryDependencies ++= Seq(
23-
"com.beachape" %% "enumeratum" % "1.1.0"
23+
"com.beachape" %% "enumeratum" % "1.2.0"
2424
)
2525
```
2626

2727
For enumeratum with Play JSON:
2828
```scala
2929
libraryDependencies ++= Seq(
30-
"com.beachape" %% "enumeratum" % "1.1.0",
31-
"com.beachape" %% "enumeratum-play-json" % "1.1.0"
30+
"com.beachape" %% "enumeratum" % "1.2.0",
31+
"com.beachape" %% "enumeratum-play-json" % "1.2.0"
3232
)
3333
```
3434

3535
For enumeratum with full Play support:
3636
```scala
3737
libraryDependencies ++= Seq(
38-
"com.beachape" %% "enumeratum" % "1.1.0",
39-
"com.beachape" %% "enumeratum-play" % "1.1.0"
38+
"com.beachape" %% "enumeratum" % "1.2.0",
39+
"com.beachape" %% "enumeratum-play" % "1.2.0"
4040
)
4141
```
4242

@@ -51,9 +51,9 @@ the `values` method. If you don't care about ordinality, just pass `findValues`
5151

5252
```scala
5353

54-
import enumeratum.Enum
54+
import enumeratum._
5555

56-
sealed trait Greeting
56+
sealed trait Greeting extends EnumEntry
5757

5858
object Greeting extends Enum[Greeting] {
5959

@@ -107,9 +107,9 @@ For example:
107107
```scala
108108
package enums._
109109

110-
import enumeratum.PlayEnum
110+
import enumeratum._
111111

112-
sealed trait Greeting
112+
sealed trait Greeting extends EnumEntry
113113

114114
object Greeting extends PlayEnum[Greeting] {
115115

enumeratum-core/src/main/scala/enumeratum/Enum.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import scala.language.postfixOps
1010
* This is yet another one.
1111
*
1212
* Oh yeah, [[Enum]] is BYOO (bring your own ordinality). Take care of that when
13-
* you implement the value method.
13+
* you implement the values method.
1414
*
1515
* How to use:
1616
*
@@ -34,7 +34,7 @@ import scala.language.postfixOps
3434
* }}}
3535
* @tparam A The sealed trait
3636
*/
37-
trait Enum[A] {
37+
trait Enum[A <: EnumEntry] {
3838

3939
/**
4040
* The sequence of values for your [[Enum]]. You will typically want
@@ -57,12 +57,12 @@ trait Enum[A] {
5757
/**
5858
* Map of [[A]] object names to [[A]]s
5959
*/
60-
lazy final val namesToValuesMap: Map[String, A] = values map (v => v.toString -> v) toMap
60+
lazy final val namesToValuesMap: Map[String, A] = values map (v => v.entryName -> v) toMap
6161

6262
/**
6363
* Map of [[A]] object names in lower case to [[A]]s for case-insensitive comparison
6464
*/
65-
lazy final val lowerCaseNamesToValuesMap: Map[String, A] = values map (v => v.toString.toLowerCase -> v) toMap
65+
lazy final val lowerCaseNamesToValuesMap: Map[String, A] = values map (v => v.entryName.toLowerCase -> v) toMap
6666

6767
/**
6868
* Optionally returns an [[A]] for a given name.
@@ -75,11 +75,11 @@ trait Enum[A] {
7575
def withNameInsensitiveOption(name: String): Option[A] = lowerCaseNamesToValuesMap get name.toLowerCase
7676

7777
/**
78-
* Tries to get an [[A]] by the supplied name. The name corresponds to the .toString
78+
* Tries to get an [[A]] by the supplied name. The name corresponds to the .name
7979
* of the case objects implementing [[A]]
8080
*
8181
* Like [[Enumeration]]'s `withName`, this method will throw if the name does not match any of the values'
82-
* .toString names.
82+
* .entryName values.
8383
*/
8484
def withName(name: String): A =
8585
withNameOption(name) getOrElse (throw new NoSuchElementException(s"$name is not a member of Enum $this"))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package enumeratum
2+
3+
/**
4+
* Base type for an enum entry for [[Enum]]
5+
*
6+
* By default, the entryName method used for serialising and deseralising Enum values uses
7+
* toString, but feel free to override to fit your needs
8+
*/
9+
trait EnumEntry {
10+
11+
/**
12+
* String representation of this Enum Entry.
13+
*
14+
* Override in your implementation if needed
15+
*/
16+
def entryName: String = toString
17+
18+
}

enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class EnumSpec extends FunSpec with Matchers {
132132

133133
it("should fail to compile for unsealed traits") {
134134
"""
135-
trait NotSealed
135+
trait NotSealed extends EnumEntry
136136
137137
object NotSealed extends Enum[NotSealed] {
138138
val values = findValues
@@ -142,7 +142,7 @@ class EnumSpec extends FunSpec with Matchers {
142142

143143
it("should fail to compile for unsealed abstract classes") {
144144
"""
145-
abstract class Abstract
145+
abstract class Abstract extends EnumEntry
146146
147147
object Abstract extends Enum[Abstract] {
148148
val values = findValues
@@ -152,7 +152,7 @@ class EnumSpec extends FunSpec with Matchers {
152152

153153
it("should fail to compile for classes") {
154154
"""
155-
class Class
155+
class Class extends EnumEntry
156156
157157
object Class extends Enum[Class] {
158158
val values = findValues
@@ -162,7 +162,7 @@ class EnumSpec extends FunSpec with Matchers {
162162

163163
it("should fail to compile if the enum is not an object") {
164164
"""
165-
sealed trait Foo
165+
sealed trait Foo extends EnumEntry
166166
167167
class Class extends Enum[Foo] {
168168
val values = findValues

enumeratum-core/src/test/scala/enumeratum/Models.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package enumeratum
22

3-
sealed trait DummyEnum
3+
sealed trait DummyEnum extends EnumEntry
44

55
object DummyEnum extends Enum[DummyEnum] {
66

@@ -14,7 +14,7 @@ object DummyEnum extends Enum[DummyEnum] {
1414

1515
object Wrapper {
1616

17-
sealed trait SmartEnum
17+
sealed trait SmartEnum extends EnumEntry
1818

1919
object SmartEnum extends Enum[SmartEnum] {
2020

@@ -29,7 +29,7 @@ object Wrapper {
2929
}
3030

3131
object InTheWoods {
32-
sealed abstract class Mushroom(val toxic: Boolean)
32+
sealed abstract class Mushroom(val toxic: Boolean) extends EnumEntry
3333

3434
object Mushroom extends Enum[Mushroom] {
3535

enumeratum-play-json/src/main/scala/enumeratum/Json.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Json {
1313
* @param enum The enum
1414
* @param insensitive bind in a case-insensitive way, defaults to false
1515
*/
16-
def reads[A](enum: Enum[A], insensitive: Boolean = false): Reads[A] = new Reads[A] {
16+
def reads[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Reads[A] = new Reads[A] {
1717
def reads(json: JsValue): JsResult[A] = json match {
1818
case JsString(s) => {
1919
val maybeBound = if (insensitive) enum.withNameInsensitiveOption(s) else enum.withNameOption(s)
@@ -29,8 +29,8 @@ object Json {
2929
/**
3030
* Returns a Json writes for a given enum [[Enum]]
3131
*/
32-
def writes[A](enum: Enum[A]): Writes[A] = new Writes[A] {
33-
def writes(v: A): JsValue = JsString(v.toString)
32+
def writes[A <: EnumEntry](enum: Enum[A]): Writes[A] = new Writes[A] {
33+
def writes(v: A): JsValue = JsString(v.entryName)
3434
}
3535

3636
/**
@@ -39,7 +39,7 @@ object Json {
3939
* @param enum The enum
4040
* @param insensitive bind in a case-insensitive way, defaults to false
4141
*/
42-
def formats[A](enum: Enum[A], insensitive: Boolean = false): Format[A] = {
42+
def formats[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Format[A] = {
4343
Format(reads(enum, insensitive), writes(enum))
4444
}
4545

enumeratum-play-json/src/main/scala/enumeratum/PlayJsonEnum.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package enumeratum
22

33
import play.api.libs.json.Format
44

5-
trait PlayJsonEnum[A] { self: Enum[A] =>
5+
trait PlayJsonEnum[A <: EnumEntry] { self: Enum[A] =>
66
implicit val jsonFormat: Format[A] = Json.formats(this)
77
}

enumeratum-play-json/src/test/scala/enumeratum/Dummy.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package enumeratum
33
/**
44
* Created by Lloyd on 2/4/15.
55
*/
6-
sealed trait Dummy
6+
sealed trait Dummy extends EnumEntry
77
object Dummy extends Enum[Dummy] with PlayJsonEnum[Dummy] {
88
case object A extends Dummy
99
case object B extends Dummy

enumeratum-play/src/main/scala/enumeratum/Forms.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ object Forms {
1919
* @param enum The enum
2020
* @param insensitive bind in a case-insensitive way, defaults to false
2121
*/
22-
def enum[A](enum: Enum[A], insensitive: Boolean = false): Mapping[A] = PlayForms.of(format(enum, insensitive))
22+
def enum[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Mapping[A] = PlayForms.of(format(enum, insensitive))
2323

2424
/**
2525
* Returns a Formatter for [[Enum]]
2626
*
2727
* @param enum The enum
2828
* @param insensitive bind in a case-insensitive way, defaults to false
2929
*/
30-
private[enumeratum] def format[A](enum: Enum[A], insensitive: Boolean = false): Formatter[A] = new Formatter[A] {
30+
private[enumeratum] def format[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Formatter[A] = new Formatter[A] {
3131
def bind(key: String, data: Map[String, String]) = {
3232
play.api.data.format.Formats.stringFormat.bind(key, data).right.flatMap { s =>
3333
val maybeBound = if (insensitive) enum.withNameInsensitiveOption(s) else enum.withNameOption(s)
@@ -37,7 +37,7 @@ object Forms {
3737
}
3838
}
3939
}
40-
def unbind(key: String, value: A) = Map(key -> value.toString)
40+
def unbind(key: String, value: A) = Map(key -> value.entryName)
4141
}
4242

4343
}

0 commit comments

Comments
 (0)