-
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
Provide withName* variants returning an Either[String, EnumEntry] #258
Conversation
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.
Wow, good idea. Would be nice if you could add this to the ValueEnum
variants as well.
Indeed, I'll add right away ! |
Is is okay for you ? |
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.
This LGTM, but I'm away at the moment so will hold off on merging.
Wondering if String
is a good type to return in the left or we should create a newtype wrapper around it (e.g. final case class NoSuchEntry(message: String) extends AnyVal
) to make it more type safe...
Was just about implement this very same feature :) Let me know if I can help |
I didn't get time recently to work on it, hopefully this week end :) |
Sorry for the delay, finally got time to update this PR, with the change discussed above:
|
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.
Looking good, left a couple of suggestions for improvement.
@@ -85,6 +85,13 @@ trait Enum[A <: EnumEntry] { | |||
*/ | |||
def withNameOption(name: String): Option[A] = namesToValuesMap.get(name) | |||
|
|||
/** | |||
* Returns an [[Right[A]]] for a given name, or a [[Left[String]]] if the name does not match any of the values' |
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.
* Returns an [[Right[A]]] for a given name, or a [[Left[String]]] if the name does not match any of the values' | |
* Returns an [[Right[A]]] for a given name, or a [[Left[NoSuchMember]]] if the name does not match any of the values' |
* Returns an [[Right[A]]] for a given name, or a [[Left[String]]] if the name does not match any of the values' | ||
* .entryName values, disregarding case. | ||
*/ | ||
def withNameUppercaseOnlyEither(name: String): Either[String, A] = |
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.
Same here.
* Returns an [[Right[A]]] for a given name, or a [[Left[String]]] if the name does not match any of the values' | ||
* .entryName values, disregarding case. | ||
*/ | ||
def withNameLowercaseOnlyEither(name: String): Either[String, A] = |
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.
Same here
enumeratum-core/src/main/scala/enumeratum/values/ValueEnum.scala
Outdated
Show resolved
Hide resolved
All fixed, completely missed the documentation and case variants methods 😅 |
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.
LGTM. Will merge and release in the next couple days :)
Hey, thanks for this @pdalpra . Just released 1.5.15 based on this. |
Similar to the Option variants, this provide safe functions to get an EnumEntry by name, returning a Left is the entry cannot be found.
This reuses the helper function to builds the error message for Exception.
NB: this can eventually be reused in some integrations (Circe, Play) that uses Either in their APIs, which currently relies on the withName* Option variants and pattern matches on the result to convert it to Either.