-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Many to one #175
Many to one #175
Changes from 5 commits
62b9a67
fe050ae
6bd23b5
8b46940
e48eb12
d76f40d
9441907
67984ff
21dd9b1
cf7cad8
79d8ad6
7871c68
308716d
5e27c6b
2c604d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package enumeratum | |
import scala.language.experimental.macros | ||
|
||
import scala.collection.immutable._ | ||
import scala.collection.breakOut | ||
|
||
/** | ||
* All the cool kids have their own Enumeration implementation, most of which try to | ||
|
@@ -38,20 +39,20 @@ trait Enum[A <: EnumEntry] { | |
/** | ||
* Map of [[A]] object names to [[A]]s | ||
*/ | ||
lazy final val namesToValuesMap: Map[String, A] = | ||
values.map(v => v.entryName -> v).toMap | ||
lazy val namesToValuesMap: Map[String, A] = | ||
values.map(v => v.entryName -> v)(breakOut) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of using
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tl;dr Removed
Also, |
||
|
||
/** | ||
* 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 | ||
values.map(v => v.entryName.toLowerCase -> v)(breakOut) | ||
|
||
/** | ||
* 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 | ||
values.map(v => v.entryName.toUpperCase -> v)(breakOut) | ||
|
||
/** | ||
* Map of [[A]] to their index in the values sequence. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,8 @@ object EnumEntry { | |
* | ||
* http://stackoverflow.com/a/19832063/1814775 | ||
*/ | ||
private val regexp1: Pattern = Pattern.compile("([A-Z]+)([A-Z][a-z])") | ||
private val regexp2: Pattern = Pattern.compile("([a-z\\d])([A-Z])") | ||
private val regexp1: Pattern = Pattern.compile("([A-Z]+)([A-Z][a-z])") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we don't need the class you added, please revert these formatting changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
private val regexp2: Pattern = Pattern.compile("([a-z\\d])([A-Z])") | ||
private val replacement: String = "$1_$2" | ||
|
||
// Adapted from Lift's StringHelpers#snakify https://github.com/lift/framework/blob/a3075e0676d60861425281427aa5f57c02c3b0bc/core/util/src/main/scala/net/liftweb/util/StringHelpers.scala#L91 | ||
|
@@ -62,63 +62,71 @@ object EnumEntry { | |
* Stackable trait to convert the entryName to Capital_Snake_Case . | ||
*/ | ||
trait CapitalSnakecase extends EnumEntry { | ||
override def entryName: String = stableEntryName | ||
override def entryName: String = stableEntryName | ||
|
||
private[this] lazy val stableEntryName: String = camel2WordArray(super.entryName).mkString("_") | ||
} | ||
|
||
/** | ||
* Stackable trait to convert the entryName to Capital-Hyphen-Case. | ||
*/ | ||
trait CapitalHyphencase extends EnumEntry { | ||
override def entryName: String = stableEntryName | ||
override def entryName: String = stableEntryName | ||
|
||
private[this] lazy val stableEntryName: String = camel2WordArray(super.entryName).mkString("-") | ||
} | ||
|
||
/** | ||
* Stackable trait to convert the entryName to Capital.Dot.Case. | ||
*/ | ||
trait CapitalDotcase extends EnumEntry { | ||
override def entryName: String = stableEntryName | ||
override def entryName: String = stableEntryName | ||
|
||
private[this] lazy val stableEntryName: String = camel2WordArray(super.entryName).mkString(".") | ||
} | ||
|
||
/** | ||
* Stackable trait to convert the entryName to Capital Words. | ||
*/ | ||
trait CapitalWords extends EnumEntry { | ||
override def entryName: String = stableEntryName | ||
override def entryName: String = stableEntryName | ||
|
||
private[this] lazy val stableEntryName: String = camel2WordArray(super.entryName).mkString(" ") | ||
} | ||
|
||
/** | ||
* Stackable trait to convert the entryName to CamelCase. | ||
*/ | ||
trait Camelcase extends EnumEntry { | ||
override def entryName: String = stableEntryName | ||
override def entryName: String = stableEntryName | ||
|
||
private[this] lazy val stableEntryName: String = super.entryName.split("_+").map(s => capitalise(s.toLowerCase)).mkString | ||
} | ||
|
||
/** | ||
* Stackable trait to convert the entryName to UPPERCASE. | ||
*/ | ||
trait Uppercase extends EnumEntry { | ||
override def entryName: String = stableEntryName | ||
override def entryName: String = stableEntryName | ||
|
||
private[this] lazy val stableEntryName: String = super.entryName.toUpperCase | ||
} | ||
|
||
/** | ||
* Stackable trait to convert the entryName to lowercase. | ||
*/ | ||
trait Lowercase extends EnumEntry { | ||
override def entryName: String = stableEntryName | ||
override def entryName: String = stableEntryName | ||
|
||
private[this] lazy val stableEntryName: String = super.entryName.toLowerCase | ||
} | ||
|
||
/** | ||
* Stackable trait to uncapitalise the first letter of the entryName. | ||
*/ | ||
trait Uncapitalised extends EnumEntry { | ||
override def entryName: String = stableEntryName | ||
override def entryName: String = stableEntryName | ||
|
||
private[this] lazy val stableEntryName: String = uncapitalise(super.entryName) | ||
} | ||
|
||
|
@@ -167,6 +175,9 @@ object EnumEntry { | |
*/ | ||
trait LowerCamelcase extends EnumEntry with Camelcase with Uncapitalised | ||
|
||
abstract class MultiEnum(override val entryName: String, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is needed (at least not here); looks like something you want in your tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
val alternateNames: String*) extends EnumEntry | ||
|
||
/** | ||
* Helper implicit class that holds enrichment methods | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,19 @@ object UncapitalisedEnum extends Enum[UncapitalisedEnum] { | |
|
||
} | ||
|
||
case object MultiEnum extends Enum[MultiEnum] { | ||
import scala.collection.breakOut | ||
val values = findValues | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't do this because the type parameter passed to Compilation error https://travis-ci.org/lloydmeta/enumeratum/jobs/343456716#L844 [error] /home/travis/build/lloydmeta/enumeratum/enumeratum-core/src/test/scala/enumeratum/Models.scala:232:16: You can only use findValues on sealed traits or classes
[error] val values = findValues
[error] ^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
override val namesToValuesMap: Map[String, MultiEnum] = | ||
values.flatMap { n => | ||
(n.entryName -> n) +: n.alternateNames.map(name => name -> n) | ||
}(breakOut) | ||
|
||
case object One extends MultiEnum("one", "1", "eins") | ||
case object Two extends MultiEnum("two", "2", "zwei") | ||
} | ||
|
||
object Wrapper { | ||
|
||
sealed trait SmartEnum extends EnumEntry | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
I think we should probably modify these
Map
s to be in terms ofnamesToValuesMap
as well (I'd like to keep them final for now)enumeratum/enumeratum-core/src/main/scala/enumeratum/Enum.scala
Lines 47 to 54 in c895c32
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify what you mean here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, they're built in terms of
values
directly, whereas I think they should be built in terms ofnamesToValuesMap
so that they can have the same values (in case the user overridesnamesToValuesMap
and wants to use functions that calllowerCaseNamesToValuesMap
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I got it.