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() (#936)
blast-hardcheese authored Jun 24, 2021
1 parent c33905c commit 29a8d0d
Showing 2 changed files with 17 additions and 15 deletions.
6 changes: 4 additions & 2 deletions tools/src/main/scala/caliban/tools/ClientWriter.scala
Original file line number Diff line number Diff line change
@@ -363,10 +363,12 @@ 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] = {
26 changes: 13 additions & 13 deletions tools/src/test/scala/caliban/tools/ClientWriterSpec.scala
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)
@@ -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)

0 comments on commit 29a8d0d

Please sign in to comment.