From 35fb87b817b4901c21d5f885b3cf3da78ba18a91 Mon Sep 17 00:00:00 2001 From: Pierre Ricadat Date: Sat, 29 Feb 2020 13:20:00 +0900 Subject: [PATCH] Fix input values encoding --- client/src/main/scala/caliban/client/Value.scala | 6 +++++- codegen/src/main/scala/caliban/codegen/ClientWriter.scala | 2 +- .../src/test/scala/caliban/codegen/ClientWriterSpec.scala | 6 +++--- core/src/main/scala/caliban/schema/ArgBuilder.scala | 5 ++--- examples/src/main/scala/caliban/client/Client.scala | 6 +++--- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/client/src/main/scala/caliban/client/Value.scala b/client/src/main/scala/caliban/client/Value.scala index e001bf1dd..7911dff72 100644 --- a/client/src/main/scala/caliban/client/Value.scala +++ b/client/src/main/scala/caliban/client/Value.scala @@ -14,6 +14,9 @@ object Value { case class NumberValue(value: BigDecimal) extends Value { override def toString: String = s"$value" } + case class EnumValue(value: String) extends Value { + override def toString: String = value + } case class StringValue(value: String) extends Value { override def toString: String = s""""${value.replace("\"", "\\\"")}"""" } @@ -25,7 +28,7 @@ object Value { } case class ObjectValue(fields: List[(String, Value)]) extends Value { override def toString: String = - fields.map { case (name, value) => s""""$name":${value.toString}""" }.mkString("{", ",", "}") + fields.map { case (name, value) => s"""$name:${value.toString}""" }.mkString("{", ",", "}") } private def jsonToValue(json: Json): Value = @@ -42,6 +45,7 @@ object Value { case NullValue => Json.Null case NumberValue(value) => Json.fromBigDecimal(value) case StringValue(value) => Json.fromString(value) + case EnumValue(value) => Json.fromString(value) case BooleanValue(value) => Json.fromBoolean(value) case ListValue(values) => Json.fromValues(values.map(valueToJson)) case ObjectValue(fields) => Json.obj(fields.map { case (k, v) => k -> valueToJson(v) }: _*) diff --git a/codegen/src/main/scala/caliban/codegen/ClientWriter.scala b/codegen/src/main/scala/caliban/codegen/ClientWriter.scala index e639639bc..2edc069f5 100644 --- a/codegen/src/main/scala/caliban/codegen/ClientWriter.scala +++ b/codegen/src/main/scala/caliban/codegen/ClientWriter.scala @@ -150,7 +150,7 @@ object ClientWriter { implicit val encoder: ArgEncoder[${typedef.name}] = new ArgEncoder[${typedef.name}] { override def encode(value: ${typedef.name}): Value = value match { ${typedef.enumValuesDefinition - .map(v => s"""case ${typedef.name}.${safeName(v.enumValue)} => StringValue("${v.enumValue}")""") + .map(v => s"""case ${typedef.name}.${safeName(v.enumValue)} => EnumValue("${v.enumValue}")""") .mkString("\n")} } override def typeName: String = "${typedef.name}" diff --git a/codegen/src/test/scala/caliban/codegen/ClientWriterSpec.scala b/codegen/src/test/scala/caliban/codegen/ClientWriterSpec.scala index ccb70c56e..84dcbf005 100644 --- a/codegen/src/test/scala/caliban/codegen/ClientWriterSpec.scala +++ b/codegen/src/test/scala/caliban/codegen/ClientWriterSpec.scala @@ -225,9 +225,9 @@ object Client { } implicit val encoder: ArgEncoder[Origin] = new ArgEncoder[Origin] { override def encode(value: Origin): Value = value match { - case Origin.EARTH => StringValue("EARTH") - case Origin.MARS => StringValue("MARS") - case Origin.BELT => StringValue("BELT") + case Origin.EARTH => EnumValue("EARTH") + case Origin.MARS => EnumValue("MARS") + case Origin.BELT => EnumValue("BELT") } override def typeName: String = "Origin" } diff --git a/core/src/main/scala/caliban/schema/ArgBuilder.scala b/core/src/main/scala/caliban/schema/ArgBuilder.scala index ee367a021..d0f2c9e55 100644 --- a/core/src/main/scala/caliban/schema/ArgBuilder.scala +++ b/core/src/main/scala/caliban/schema/ArgBuilder.scala @@ -134,9 +134,8 @@ object ArgBuilder { def dispatch[T](ctx: SealedTrait[ArgBuilder, T]): ArgBuilder[T] = input => { (input match { - case StringValue(value) => Some(value) - case EnumValue(value) => Some(value) - case _ => None + case EnumValue(value) => Some(value) + case _ => None }) match { case Some(value) => ctx.subtypes diff --git a/examples/src/main/scala/caliban/client/Client.scala b/examples/src/main/scala/caliban/client/Client.scala index 74ed6e97b..7a08d3a98 100644 --- a/examples/src/main/scala/caliban/client/Client.scala +++ b/examples/src/main/scala/caliban/client/Client.scala @@ -26,9 +26,9 @@ object Client { } implicit val encoder: ArgEncoder[Origin] = new ArgEncoder[Origin] { override def encode(value: Origin): Value = value match { - case BELT => StringValue("BELT") - case EARTH => StringValue("EARTH") - case MARS => StringValue("MARS") + case BELT => EnumValue("BELT") + case EARTH => EnumValue("EARTH") + case MARS => EnumValue("MARS") } override def typeName: String = "Origin" }