Skip to content

Commit

Permalink
Support Short schemas and args (#1011)
Browse files Browse the repository at this point in the history
* Support Short schemas and args

* Fix scala3 big decimal conversion
  • Loading branch information
Fluxx authored Aug 24, 2021
1 parent 60cfd37 commit d012343
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion client/src/main/scala/caliban/client/ArgEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
"""
)
Expand All @@ -23,6 +23,9 @@ trait ArgEncoder[-A] { self =>

object ArgEncoder {

// 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)

implicit val long: ArgEncoder[Long] = (value: Long) => __NumberValue(value)
Expand Down
6 changes: 5 additions & 1 deletion client/src/main/scala/caliban/client/ScalarDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
"""
)
Expand All @@ -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)))
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/caliban/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(_))
Expand Down

0 comments on commit d012343

Please sign in to comment.