Giter Club home page Giter Club logo

Comments (4)

lloydmeta avatar lloydmeta commented on July 18, 2024

Does this help https://github.com/lloydmeta/enumeratum?tab=readme-ov-file#manual-override-of-name ?

An example of that is here (this shows multi-name usage but you don't have to do that)

sealed class MultiEnum(override val entryName: String, val alternateNames: String*)
extends EnumEntry
case object MultiEnum extends Enum[MultiEnum] {
val values = findValues
override lazy val namesToValuesMap: Map[String, MultiEnum] =
values.flatMap { n =>
(n.entryName -> n) +: n.alternateNames.map(_ -> n)
}.toMap
case object One extends MultiEnum("one", "1", "eins")
case object Two extends MultiEnum("two", "2", "zwei")
}

from enumeratum.

jpanda109 avatar jpanda109 commented on July 18, 2024

Not quite - I was hoping for something where I can only rename a single case, and have the others use the default of whatever mixing i've already set.

So in the case above, it'd be something like:

import enumeratum._
import enumeratum.EnumEntry._

sealed trait Greeting extends EnumEntry with Snakecase

object Greeting extends Enum[Greeting] {

  val values = findValues

  case object Hello        extends Greeting
  case object GoodBye      extends Greeting
  // not valid scala
  case object ShoutGoodBye extends Greeting with EntryName("foo")
}

Greeting.withName("hello") == Hello
Greeting.withName("good_bye") == GoodBye
Greeting.withName("foo") == ShoutGoodBye

from enumeratum.

lloydmeta avatar lloydmeta commented on July 18, 2024

The issues in this repo are not meant to walk people through exact solutions to Scala code problems. The key in the section I linked to in my reply was

manually override the def entryName: String method.

Not an exact copy pasta of the code 😅 . Anyways, you should consider something like the following

import enumeratum._
import enumeratum.EnumEntry._

sealed trait Greeting extends EnumEntry with Snakecase

object Greeting extends Enum[Greeting] {

  val values = findValues

  case object Hello        extends Greeting
  case object GoodBye      extends Greeting
  case object ShoutGoodBye extends Greeting {
    override val entryName: String = "foo"
  }

}

If you want, you can declare an intermediate trait/abstract class to make the entryName overriding more convenient for the Greetings that you want to have non-default names.

from enumeratum.

jpanda109 avatar jpanda109 commented on July 18, 2024

Ahh sorry, I misread, and sorry for clogging the issues - I didn’t see a link to any other discussion forums in the readme. Out of curiosity, is there a better place for questions?

I’ll go ahead and close this out - thank for the help.

from enumeratum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.