From 29a8d0d89605206839c00d24731761b6d8831f2b Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Wed, 23 Jun 2021 17:33:18 -0700 Subject: [PATCH] Exposing a distinct "value" parameter for enumeration members, to avoid relying on default toString() (#936) --- .../scala/caliban/tools/ClientWriter.scala | 6 +++-- .../caliban/tools/ClientWriterSpec.scala | 26 +++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/tools/src/main/scala/caliban/tools/ClientWriter.scala b/tools/src/main/scala/caliban/tools/ClientWriter.scala index a811a6373..b2af2d9be 100644 --- a/tools/src/main/scala/caliban/tools/ClientWriter.scala +++ b/tools/src/main/scala/caliban/tools/ClientWriter.scala @@ -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] = { diff --git a/tools/src/test/scala/caliban/tools/ClientWriterSpec.scala b/tools/src/test/scala/caliban/tools/ClientWriterSpec.scala index 4add61daa..f275e3e65 100644 --- a/tools/src/test/scala/caliban/tools/ClientWriterSpec.scala +++ b/tools/src/test/scala/caliban/tools/ClientWriterSpec.scala @@ -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)