Skip to content

Commit

Permalink
Bump smithy4s to 0.18
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Oct 22, 2023
1 parent f0d8c14 commit 0f91622
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,16 @@ package org.polyvariant.smithy4scaliban
import caliban.Value.NullValue
import cats.implicits._
import smithy4s.Bijection
import smithy4s.ByteArray
import smithy4s.Document
import smithy4s.Hints
import smithy4s.Refinement
import smithy4s.ShapeId
import smithy4s.Timestamp
import smithy4s.Schema
import smithy4s.schema.Field
import smithy4s.schema.Field.Wrapped
import smithy4s.schema.Primitive
import smithy4s.schema.SchemaVisitor
import smithy4s.schema.Alt
import smithy4s.schema.SchemaAlt
import caliban.schema.ArgBuilder
import caliban.InputValue
import caliban.CalibanError
Expand All @@ -51,8 +48,9 @@ import caliban.Value.IntValue.LongNumber
import caliban.InputValue.ObjectValue
import caliban.InputValue.ListValue
import smithy.api.TimestampFormat
import smithy4s.IntEnum
import smithy4s.schema.CompilationCache
import smithy4s.schema.EnumTag
import smithy4s.Blob

private[smithy4scaliban] class ArgBuilderVisitor(val cache: CompilationCache[ArgBuilder])
extends SchemaVisitor.Cached[ArgBuilder] {
Expand All @@ -72,20 +70,11 @@ private[smithy4scaliban] class ArgBuilderVisitor(val cache: CompilationCache[Arg
override def struct[S](
shapeId: ShapeId,
hints: Hints,
fields: Vector[Field[smithy4s.Schema, S, ?]],
fields: Vector[Field[S, ?]],
make: IndexedSeq[Any] => S,
): ArgBuilder[S] = {
val fieldsCompiled = fields.map { f =>
f.label ->
f.mapK(this)
.instanceA(
new Field.ToOptional[ArgBuilder] {
override def apply[A0](
fa: ArgBuilder[A0]
): Wrapped[ArgBuilder, Option, A0] = ArgBuilder.option(fa)

}
)
f.label -> f.schema.compile(this)
}

{
Expand All @@ -98,11 +87,15 @@ private[smithy4scaliban] class ArgBuilderVisitor(val cache: CompilationCache[Arg
}
}

override def option[A](
schema: Schema[A]
): ArgBuilder[Option[A]] = ArgBuilder.option(schema.compile(this))

override def union[U](
shapeId: ShapeId,
hints: Hints,
alternatives: Vector[SchemaAlt[U, _]],
dispatch: Alt.Dispatcher[smithy4s.schema.Schema, U],
alternatives: Vector[Alt[U, _]],
dispatch: Alt.Dispatcher[U],
): ArgBuilder[U] = {
val instancesByKey = alternatives.map(alt => alt.label -> handleAlt(alt)).toMap

Expand All @@ -120,17 +113,18 @@ private[smithy4scaliban] class ArgBuilderVisitor(val cache: CompilationCache[Arg
}

private def handleAlt[U, A](
alt: Alt[Schema, U, A]
): ArgBuilder[U] = alt.instance.compile(this).map(alt.inject)
alt: Alt[U, A]
): ArgBuilder[U] = alt.schema.compile(this).map(alt.inject)

override def enumeration[E](
shapeId: ShapeId,
hints: Hints,
tag: EnumTag[E],
values: List[EnumValue[E]],
total: E => EnumValue[E],
): ArgBuilder[E] =
hints.has(IntEnum) match {
case false =>
tag match {
case EnumTag.StringEnum() =>
val valuesByString = values.map(v => v.stringValue -> v.value).toMap

ArgBuilder
Expand All @@ -141,7 +135,7 @@ private[smithy4scaliban] class ArgBuilderVisitor(val cache: CompilationCache[Arg
.toRight(CalibanError.ExecutionError("Unknown enum case: " + v))
}

case true =>
case _ =>
val valuesByInt = values.map(v => v.intValue -> v.value).toMap

ArgBuilder
Expand Down Expand Up @@ -183,8 +177,8 @@ private[smithy4scaliban] class ArgBuilderVisitor(val cache: CompilationCache[Arg
case i => Left(CalibanError.ExecutionError("Integer too large for byte: " + i))
}

implicit val byteArrayArgBuilder: ArgBuilder[ByteArray] = ArgBuilder.string.map { str =>
ByteArray(Base64.getDecoder().decode(str))
implicit val byteArrayArgBuilder: ArgBuilder[Blob] = ArgBuilder.string.map { str =>
Blob(Base64.getDecoder().decode(str))
}

implicit val documentArgBuilder: ArgBuilder[Document] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ object CalibanGraphQLInterpreter {

val querySchema: Schema[Any, service.FunctorInterpreter[F]] =
Schema.obj(name = "Queries", description = None)(implicit fa =>
queries.map(endpointToSchema[F].apply(_, abv, csv))
queries.map(endpointToSchema[F].apply(_, abv, csv)).toList
)

val mutationSchema: Schema[Any, service.FunctorInterpreter[F]] =
Schema.obj(name = "Mutations", description = None)(implicit fa =>
mutations.map(endpointToSchema[F].apply(_, abv, csv))
mutations.map(endpointToSchema[F].apply(_, abv, csv)).toList
)

caliban.graphQL(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ import caliban.schema._
import cats.implicits._
import smithy.api.TimestampFormat
import smithy4s.Bijection
import smithy4s.ByteArray
import smithy4s.Document
import smithy4s.Hints
import smithy4s.IntEnum
import smithy4s.Lazy
import smithy4s.Refinement
import smithy4s.ShapeId
import smithy4s.Timestamp
import smithy4s.schema
import smithy4s.schema.Alt
import smithy4s.schema.EnumTag
import smithy4s.schema.CollectionTag
import smithy4s.schema.Field
import smithy4s.schema.Field.Wrapped
import smithy4s.schema.Primitive
import smithy4s.schema.SchemaAlt
import smithy4s.schema.Alt
import smithy4s.schema.SchemaVisitor
import smithy4s.Blob

private class CalibanSchemaVisitor(val cache: schema.CompilationCache[Schema[Any, *]])
extends SchemaVisitor.Cached[Schema[Any, *]] {
Expand All @@ -56,6 +54,10 @@ private class CalibanSchemaVisitor(val cache: schema.CompilationCache[Schema[Any
makeResponse = f,
)

override def option[A](
schema: smithy4s.Schema[A]
): Schema[Any, Option[A]] = Schema.optionSchema(schema.compile(this))

override def primitive[P](
shapeId: ShapeId,
hints: Hints,
Expand All @@ -64,8 +66,8 @@ private class CalibanSchemaVisitor(val cache: schema.CompilationCache[Schema[Any
implicit val byteSchema: Schema[Any, Byte] = fromScalar(shapeId)(v => Value.IntValue(v.toInt))

// base-64 encoded string
implicit val blobSchema: Schema[Any, ByteArray] =
fromScalar(shapeId)(v => Value.StringValue(v.toString()))
implicit val blobSchema: Schema[Any, Blob] =
fromScalar(shapeId)(v => Value.StringValue(v.toBase64String))

// json "any" type
implicit val documentSchema: Schema[Any, Document] = fromScalar(shapeId)(documentToValue)
Expand Down Expand Up @@ -94,18 +96,11 @@ private class CalibanSchemaVisitor(val cache: schema.CompilationCache[Schema[Any
}

private def field[S, A](
f: Field[Schema[Any, *], S, A]
f: Field[S, A]
)(
implicit fa: FieldAttributes
) = {
val schema = f
.instanceA(new Field.ToOptional[Schema[Any, *]] {

override def apply[A0](
fa: Schema[Any, A0]
): Wrapped[Schema[Any, *], Option, A0] = Schema.optionSchema(fa)

})
val schema = f.schema.compile(this)

Schema.field(f.label)(f.get)(
schema,
Expand All @@ -126,12 +121,11 @@ private class CalibanSchemaVisitor(val cache: schema.CompilationCache[Schema[Any
override def struct[S](
shapeId: ShapeId,
hints: Hints,
fields: Vector[Field[smithy4s.Schema, S, ?]],
fields: Vector[Field[S, ?]],
make: IndexedSeq[Any] => S,
): Schema[Any, S] = Schema
.obj(shapeId.name, None) { implicit fa =>
fields
.map(_.mapK(this))
.map(field(_))
.toList
}
Expand Down Expand Up @@ -169,14 +163,14 @@ private class CalibanSchemaVisitor(val cache: schema.CompilationCache[Schema[Any
override def union[U](
shapeId: ShapeId,
hints: Hints,
alternatives: Vector[SchemaAlt[U, _]],
dispatch: Alt.Dispatcher[smithy4s.Schema, U],
alternatives: Vector[Alt[U, _]],
dispatch: Alt.Dispatcher[U],
): Schema[Any, U] = {
val self = this

type Resolve[A] = A => Step[Any]

val resolve0 = dispatch.compile(new Alt.Precompiler[smithy4s.Schema, Resolve] {
val resolve0 = dispatch.compile(new Alt.Precompiler[Resolve] {
override def apply[A](
label: String,
instance: smithy4s.Schema[A],
Expand Down Expand Up @@ -205,25 +199,26 @@ private class CalibanSchemaVisitor(val cache: schema.CompilationCache[Schema[Any
}
}.withName(shapeId)

private def handleAlt[U, A](parent: ShapeId, alt: SchemaAlt[U, A]) =
private def handleAlt[U, A](parent: ShapeId, alt: Alt[U, A]) =
Schema.obj(
parent.name + alt.label + "Case"
)(fa =>
List(
Schema
.field[A](alt.label)(a => a)(alt.instance.compile(this), fa)
.field[A](alt.label)(a => a)(alt.schema.compile(this), fa)
)
)

override def enumeration[E](
shapeId: ShapeId,
hints: Hints,
tag: EnumTag[E],
values: List[schema.EnumValue[E]],
total: E => schema.EnumValue[E],
): Schema[Any, E] = {
hints.has(IntEnum) match {
case false => Schema.stringSchema.contramap(total(_: E).stringValue)
case true => Schema.intSchema.contramap(total(_: E).intValue)
tag match {
case EnumTag.IntEnum() => Schema.intSchema.contramap(total(_: E).intValue)
case _ => Schema.stringSchema.contramap(total(_: E).stringValue)
}
}.withName(shapeId)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@

package org.polyvariant.smithy4scaliban

import weaver._
import smithy4s.Schema
import caliban.InputValue
import caliban.Value
import smithy4s.Bijection
import smithy.api.Length
import caliban.InputValue
import smithy.api.TimestampFormat
import smithy4s.Bijection
import smithy4s.Blob
import smithy4s.Document
import smithy4s.Schema
import smithy4s.Timestamp
import smithy4s.example.CityCoordinates
import smithy4s.example.MovieTheater
import smithy4s.example.EnumResult
import smithy4s.example.Foo
import smithy4s.example.Ingredient
import smithy4s.example.EnumResult
import smithy4s.example.MovieTheater
import smithy4s.example.Rec
import smithy4s.Document
import java.time.Instant
import smithy4s.Timestamp
import smithy.api.TimestampFormat
import smithy4s.ByteArray
import Smithy4sTestUtils._
import smithy4s.schema.CompilationCache
import weaver._

import java.time.Instant

object ArgBuilderTests extends FunSuite {
private val abv = new ArgBuilderVisitor(CompilationCache.nop)
Expand Down Expand Up @@ -281,8 +281,8 @@ object ArgBuilderTests extends FunSuite {

test("bytes") {
decodeArgSuccess(
Value.StringValue(ByteArray(Array(1, 2, 3)).toString),
ByteArray(Array(1, 2, 3)),
Value.StringValue(Blob(Array[Byte](1, 2, 3)).toBase64String),
Blob(Array[Byte](1, 2, 3)),
)(Schema.bytes)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import smithy4s.example.Rec
import Smithy4sTestUtils._
import smithy4s.Timestamp
import smithy.api.TimestampFormat
import smithy4s.ByteArray
import smithy4s.Document
import smithy4s.Blob

object CalibanSchemaTests extends SimpleIOSuite {
// Workaround for https://github.com/disneystreaming/smithy4s/issues/537
Expand Down Expand Up @@ -175,7 +175,7 @@ object CalibanSchemaTests extends SimpleIOSuite {

test("blob schema") {
testQueryResultWithSchema(
ByteArray("foo".getBytes()),
Blob("foo"),
"""query { item }""".stripMargin,
)(Schema.bytes.nested("item"))
.map(assert.eql(_, Json.obj("item" := "Zm9v")))
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.4.21")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.1")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.12")
addSbtPlugin("com.disneystreaming.smithy4s" % "smithy4s-sbt-codegen" % "0.17.7")
addSbtPlugin("com.disneystreaming.smithy4s" % "smithy4s-sbt-codegen" % "0.18.2")
addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.9.0")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.3.7")

Expand Down

0 comments on commit 0f91622

Please sign in to comment.