From 52dfe4bd355adbd4317435a782ee0b0d15a1bc99 Mon Sep 17 00:00:00 2001 From: Jeff Pollard Date: Mon, 23 Aug 2021 16:36:28 -0700 Subject: [PATCH 1/2] Support Short schemas and args --- client/src/main/scala/caliban/client/ArgEncoder.scala | 4 +++- client/src/main/scala/caliban/client/ScalarDecoder.scala | 6 +++++- core/src/main/scala/caliban/schema/Schema.scala | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/client/src/main/scala/caliban/client/ArgEncoder.scala b/client/src/main/scala/caliban/client/ArgEncoder.scala index 09819060f..81b240494 100644 --- a/client/src/main/scala/caliban/client/ArgEncoder.scala +++ b/client/src/main/scala/caliban/client/ArgEncoder.scala @@ -11,7 +11,7 @@ import scala.annotation.implicitNotFound */ @implicitNotFound( """Cannot find an ArgEncoder for type ${A}. - + Caliban needs it to know how to encode arguments of type ${A}. """ ) @@ -23,6 +23,8 @@ trait ArgEncoder[-A] { self => object ArgEncoder { + implicit val short: ArgEncoder[Short] = (value: Short) => __NumberValue(value) + implicit val int: ArgEncoder[Int] = (value: Int) => __NumberValue(value) implicit val long: ArgEncoder[Long] = (value: Long) => __NumberValue(value) diff --git a/client/src/main/scala/caliban/client/ScalarDecoder.scala b/client/src/main/scala/caliban/client/ScalarDecoder.scala index 77971422f..7d2c4895d 100644 --- a/client/src/main/scala/caliban/client/ScalarDecoder.scala +++ b/client/src/main/scala/caliban/client/ScalarDecoder.scala @@ -14,7 +14,7 @@ import scala.annotation.implicitNotFound */ @implicitNotFound( """Cannot find a ScalarDecoder for type ${A}. - + Caliban needs it to know how to decode a scalar of type ${A}. """ ) @@ -23,6 +23,10 @@ trait ScalarDecoder[+A] { } object ScalarDecoder { + implicit val short: ScalarDecoder[Short] = { + case __NumberValue(value) => Right(value.toShort) + case other => Left(DecodingError(s"Can't build a Short from input $other")) + } implicit val int: ScalarDecoder[Int] = { case __NumberValue(value) => Try(value.toIntExact).toEither.left.map(ex => DecodingError(s"Can't build an Int from input $value", Some(ex))) diff --git a/core/src/main/scala/caliban/schema/Schema.scala b/core/src/main/scala/caliban/schema/Schema.scala index 8aa9f0f46..74c661063 100644 --- a/core/src/main/scala/caliban/schema/Schema.scala +++ b/core/src/main/scala/caliban/schema/Schema.scala @@ -243,6 +243,7 @@ trait GenericSchema[R] extends SchemaDerivation[R] with TemporalSchema { implicit val booleanSchema: Schema[Any, Boolean] = scalarSchema("Boolean", None, BooleanValue.apply) implicit val stringSchema: Schema[Any, String] = scalarSchema("String", None, StringValue.apply) implicit val uuidSchema: Schema[Any, UUID] = scalarSchema("ID", None, uuid => StringValue(uuid.toString)) + implicit val shortSchema: Schema[Any, Short] = scalarSchema("Short", None, IntValue(_)) implicit val intSchema: Schema[Any, Int] = scalarSchema("Int", None, IntValue(_)) implicit val longSchema: Schema[Any, Long] = scalarSchema("Long", None, IntValue(_)) implicit val bigIntSchema: Schema[Any, BigInt] = scalarSchema("BigInt", None, IntValue(_)) From eb5ab794fb8c463969aceee1a3e6aae830745ae0 Mon Sep 17 00:00:00 2001 From: Jeff Pollard Date: Mon, 23 Aug 2021 20:39:58 -0700 Subject: [PATCH 2/2] Fix scala3 big decimal conversion --- client/src/main/scala/caliban/client/ArgEncoder.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/src/main/scala/caliban/client/ArgEncoder.scala b/client/src/main/scala/caliban/client/ArgEncoder.scala index 81b240494..6cf0a25d8 100644 --- a/client/src/main/scala/caliban/client/ArgEncoder.scala +++ b/client/src/main/scala/caliban/client/ArgEncoder.scala @@ -23,7 +23,8 @@ trait ArgEncoder[-A] { self => object ArgEncoder { - implicit val short: ArgEncoder[Short] = (value: Short) => __NumberValue(value) + // In Scala3 there does not appear to be a short2bigDecimal implicit conversion. + implicit val short: ArgEncoder[Short] = (value: Short) => __NumberValue(value.toInt) implicit val int: ArgEncoder[Int] = (value: Int) => __NumberValue(value)