Skip to content

Commit

Permalink
reuse string value for uuid encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
smiklos committed Jan 18, 2022
1 parent 35c7b12 commit 48233e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
12 changes: 2 additions & 10 deletions client/src/main/scala/caliban/client/ArgEncoder.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package caliban.client

import caliban.client.__Value.{
__BooleanValue,
__ListValue,
__NullValue,
__NumberValue,
__ObjectValue,
__StringValue,
__UUIDValue
}
import caliban.client.__Value.{ __BooleanValue, __ListValue, __NullValue, __NumberValue, __ObjectValue, __StringValue }
import io.circe.Json

import scala.annotation.implicitNotFound
Expand Down Expand Up @@ -51,7 +43,7 @@ object ArgEncoder {

implicit val unit: ArgEncoder[Unit] = (_: Unit) => __ObjectValue(Nil)

implicit val uuid: ArgEncoder[UUID] = (value: UUID) => __UUIDValue(value)
implicit val uuid: ArgEncoder[UUID] = (value: UUID) => __StringValue(value.toString())

implicit def option[A](implicit ev: ArgEncoder[A]): ArgEncoder[Option[A]] = (value: Option[A]) =>
value.fold(__NullValue: __Value)(ev.encode)
Expand Down
18 changes: 6 additions & 12 deletions client/src/main/scala/caliban/client/__Value.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package caliban.client

import io.circe.{ Decoder, Encoder, Json }
import java.util.UUID

/**
* Value that can be returned by the server or sent as an argument.
Expand All @@ -21,25 +20,21 @@ sealed trait __Value { self =>
}

object __Value {
case object __NullValue extends __Value {
case object __NullValue extends __Value {
override def toString: String = "null"
}
case class __NumberValue(value: BigDecimal) extends __Value {
case class __NumberValue(value: BigDecimal) extends __Value {
override def toString: String = s"$value"
}
case class __EnumValue(value: String) extends __Value {
case class __EnumValue(value: String) extends __Value {
override def toString: String = value
}
case class __StringValue(value: String) extends __Value {
case class __StringValue(value: String) extends __Value {
override def toString: String = Json.fromString(value).toString
}
case class __BooleanValue(value: Boolean) extends __Value {
case class __BooleanValue(value: Boolean) extends __Value {
override def toString: String = value.toString
}
case class __UUIDValue(value: UUID) extends __Value {
override def toString: String = Json.fromString(value.toString()).toString

}
case class __ListValue(values: List[__Value]) extends __Value {
override def toString: String = values.map(_.toString).mkString("[", ",", "]")
}
Expand All @@ -64,12 +59,11 @@ object __Value {
case __StringValue(value) => Json.fromString(value)
case __EnumValue(value) => Json.fromString(value)
case __BooleanValue(value) => Json.fromBoolean(value)
case __UUIDValue(value) => Json.fromString(value.toString())
case __ListValue(values) => Json.fromValues(values.map(valueToJson))
case __ObjectValue(fields) => Json.obj(fields.map { case (k, v) => k -> valueToJson(v) }: _*)
}

implicit val valueDecoder: Decoder[__Value] = Decoder.instance(hcursor => Right(jsonToValue(hcursor.value)))

implicit val valueEncoder: Encoder[__Value] = (a: __Value) => valueToJson(a)
}
}

0 comments on commit 48233e0

Please sign in to comment.