Skip to content

Commit

Permalink
Exposing a distinct "value" parameter for enumeration members, to avo…
Browse files Browse the repository at this point in the history
…id relying on default toString()
  • Loading branch information
blast-hardcheese committed Jun 23, 2021
1 parent 218a036 commit 1f9c946
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions tools/src/main/scala/caliban/tools/ClientWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,10 @@ object ClientWriter {
def safeEnumValue(enumValue: String): String =
safeName(mappingClashedEnumValues.getOrElse(enumValue, enumValue))

s"""sealed trait $enumName extends scala.Product with scala.Serializable
s"""sealed trait $enumName extends scala.Product with scala.Serializable { def value: String }
object $enumName {
${typedef.enumValuesDefinition
.map(v => s"case object ${safeEnumValue(v.enumValue)} extends $enumName")
.map(v => s"case object ${safeEnumValue(v.enumValue)} extends $enumName { val value: String = ${"\"" + safeEnumValue(v.enumValue) + "\""} }")
.mkString("\n")}

implicit val decoder: ScalarDecoder[$enumName] = {
Expand Down
26 changes: 13 additions & 13 deletions tools/src/test/scala/caliban/tools/ClientWriterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ import caliban.client.__Value._
object Client {
sealed trait Origin extends scala.Product with scala.Serializable
sealed trait Origin extends scala.Product with scala.Serializable { def value: String }
object Origin {
case object EARTH extends Origin
case object MARS extends Origin
case object BELT extends Origin
case object EARTH extends Origin { val value: String = "EARTH" }
case object MARS extends Origin { val value: String = "MARS" }
case object BELT extends Origin { val value: String = "BELT" }
implicit val decoder: ScalarDecoder[Origin] = {
case __StringValue("EARTH") => Right(Origin.EARTH)
Expand Down Expand Up @@ -272,11 +272,11 @@ import caliban.client.__Value._
object Client {
sealed trait Origin extends scala.Product with scala.Serializable
sealed trait Origin extends scala.Product with scala.Serializable { def value: String }
object Origin {
case object EARTH extends Origin
case object MARS extends Origin
case object BELT extends Origin
case object EARTH extends Origin { val value: String = "EARTH" }
case object MARS extends Origin { val value: String = "MARS" }
case object BELT extends Origin { val value: String = "BELT" }
implicit val decoder: ScalarDecoder[Origin] = {
case __StringValue("EARTH") => Right(Origin.EARTH)
Expand Down Expand Up @@ -579,12 +579,12 @@ import caliban.client.__Value._
object Client {
sealed trait Episode extends scala.Product with scala.Serializable
sealed trait Episode extends scala.Product with scala.Serializable { def value: String }
object Episode {
case object NEWHOPE extends Episode
case object EMPIRE extends Episode
case object JEDI extends Episode
case object jedi_1 extends Episode
case object NEWHOPE extends Episode { val value: String = "NEWHOPE" }
case object EMPIRE extends Episode { val value: String = "EMPIRE" }
case object JEDI extends Episode { val value: String = "JEDI" }
case object jedi_1 extends Episode { val value: String = "jedi_1" }
implicit val decoder: ScalarDecoder[Episode] = {
case __StringValue("NEWHOPE") => Right(Episode.NEWHOPE)
Expand Down

0 comments on commit 1f9c946

Please sign in to comment.