Skip to content

Commit

Permalink
support uuid as arg encoder (ghostdogpr#1266)
Browse files Browse the repository at this point in the history
* support uuid as arg encoder

* test the uuid encoder

* reuse string value for uuid encoder
  • Loading branch information
smiklos authored and Fluxx committed Jan 27, 2022
1 parent ad0db0b commit 56e63f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client/src/main/scala/caliban/client/ArgEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import caliban.client.__Value.{ __BooleanValue, __ListValue, __NullValue, __Numb
import io.circe.Json

import scala.annotation.implicitNotFound
import java.util.UUID

/**
* Typeclass that defines how to encode an argument of type `A` into a valid [[caliban.client.__Value]].
Expand Down Expand Up @@ -42,6 +43,8 @@ object ArgEncoder {

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

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
8 changes: 8 additions & 0 deletions client/src/test/scala/caliban/client/ArgEncoderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caliban.client
import zio.test.Assertion.equalTo
import zio.test._
import zio.test.environment.TestEnvironment
import java.util.UUID

object ArgEncoderSpec extends DefaultRunnableSpec {
override def spec: ZSpec[TestEnvironment, Any] =
Expand All @@ -20,6 +21,13 @@ object ArgEncoderSpec extends DefaultRunnableSpec {
test("string with null characters") {
assert(ArgEncoder.string.encode("abcde who am i\u0000").toString)(equalTo("\"abcde who am i\\u0000\""))
}
),
suite("__UUIDValue")(
test("regular uuid") {
assert(ArgEncoder.uuid.encode(UUID.fromString("20a69d87-6d68-4779-a4da-601f4c04ebf3")).toString)(
equalTo(""""20a69d87-6d68-4779-a4da-601f4c04ebf3"""")
)
}
)
)
}

0 comments on commit 56e63f2

Please sign in to comment.