Skip to content

Commit

Permalink
Merge pull request #217 from innFactory/update/smithy4s-core-0.18.3
Browse files Browse the repository at this point in the history
chore: update smithy4s-compliance-tests, ... from 0.17.19 to 0.18.3
  • Loading branch information
anderha authored Feb 7, 2024
2 parents 295c792 + db36e45 commit 1180afb
Show file tree
Hide file tree
Showing 30 changed files with 825 additions and 375 deletions.
19 changes: 10 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sbt.Compile
import sbt.Keys.cleanFiles
val releaseVersion = sys.env.getOrElse("TAG", "0.4.4-Delta")
val releaseVersion = sys.env.getOrElse("TAG", "1.0.0-Gamma")
addCommandAlias("publishSmithy4Play", "smithy4play/publish")
addCommandAlias("publishLocalSmithy4Play", "smithy4play/publishLocal")
addCommandAlias("generateCoverage", "clean; coverage; test; coverageReport")
Expand Down Expand Up @@ -29,15 +29,15 @@ val defaultProjectSettings = Seq(
version := releaseVersion
) ++ githubSettings

val sharedSettings = defaultProjectSettings

val sharedSettings = defaultProjectSettings
lazy val smithy4play = project
.in(file("smithy4play"))
.enablePlugins(Smithy4sCodegenPlugin)
.settings(
sharedSettings,
addCompilerPlugin("org.typelevel" % "kind-projector" % "0.13.2" cross CrossVersion.full),
scalaVersion := Dependencies.scalaVersion,
Compile / smithy4sAllowedNamespaces := List("smithy.smithy4play"),
Compile / smithy4sAllowedNamespaces := List("smithy.smithy4play", "aws.protocols"),
Compile / smithy4sInputDirs := Seq(
(ThisBuild / baseDirectory).value / "smithy4play" / "src" / "resources" / "META_INF" / "smithy"
),
Expand All @@ -53,14 +53,15 @@ lazy val smithy4playTest = project
.enablePlugins(Smithy4sCodegenPlugin, PlayScala)
.settings(
sharedSettings,
scalaVersion := Dependencies.scalaVersion,
name := "smithy4playTest",
scalaVersion := Dependencies.scalaVersion,
name := "smithy4playTest",
scalacOptions += "-Ymacro-annotations",
Compile / compile / wartremoverWarnings ++= Warts.unsafe,
cleanKeepFiles += (ThisBuild / baseDirectory).value / "smithy4playTest" / "app",
cleanFiles += (ThisBuild / baseDirectory).value / "smithy4playTest" / "app" / "testDefinitions" / "test",
Compile / smithy4sInputDirs := Seq((ThisBuild / baseDirectory).value / "smithy4playTest" / "testSpecs"),
Compile / smithy4sOutputDir := (ThisBuild / baseDirectory).value / "smithy4playTest" / "app",
cleanFiles += (ThisBuild / baseDirectory).value / "smithy4playTest" / "app" / "specs" / "testDefinitions" / "test",
Compile / smithy4sInputDirs := Seq((ThisBuild / baseDirectory).value / "smithy4playTest" / "testSpecs"),
Compile / smithy4sOutputDir := (ThisBuild / baseDirectory).value / "smithy4playTest" / "app" / "specs",
Compile / smithy4sAllowedNamespaces := List("aws.protocols", "testDefinitions.test"),
libraryDependencies ++= Seq(
guice,
Dependencies.cats,
Expand Down
4 changes: 3 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ object Dependencies {
val typesafePlay = "com.typesafe.play" %% "play" % playVersion

val scalaVersion = "2.13.12"
val smithy4sVersion = "0.17.19"
val smithy4sVersion = "0.18.8"
val smithyCore = "com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion
val smithyJson = "com.disneystreaming.smithy4s" %% "smithy4s-json" % smithy4sVersion
val smithyXml = "com.disneystreaming.smithy4s" %% "smithy4s-xml" % smithy4sVersion
val smithy4sCompliance = "com.disneystreaming.smithy4s" %% "smithy4s-compliance-tests" % smithy4sVersion
val alloyCore = "com.disneystreaming.alloy" % "alloy-core" % "0.2.8"
val alloyOpenapi = "com.disneystreaming.alloy" %% "alloy-openapi" % "0.2.8"
Expand All @@ -26,6 +27,7 @@ object Dependencies {
lazy val list = Seq(
smithyCore,
smithyJson,
smithyXml,
alloyCore,
alloyOpenapi,
smithy4sCompliance,
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("com.codecommit" %% "sbt-github-packages" % "0.5.3")
addSbtPlugin("org.wartremover" %% "sbt-wartremover" % "3.1.6")
addSbtPlugin("org.scalameta" %% "sbt-scalafmt" % "2.5.2")
addSbtPlugin("com.disneystreaming.smithy4s" %% "smithy4s-sbt-codegen" % "0.17.19")
addSbtPlugin("com.disneystreaming.smithy4s" %% "smithy4s-sbt-codegen" % "0.18.8")
addSbtPlugin("com.typesafe.play" %% "sbt-plugin" % "2.9.1")
addSbtPlugin("org.scoverage" %% "sbt-scoverage" % "2.0.9")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package de.innfactory.smithy4play

import play.api.http.MimeTypes
import smithy4s.capability.instances.either._
import smithy4s.codecs.Writer.CachedCompiler
import smithy4s.codecs._
import smithy4s.http.{ HttpResponse, HttpRestSchema, Metadata, MetadataError }
import smithy4s.json.Json
import smithy4s.kinds.PolyFunction
import smithy4s.schema.CachedSchemaCompiler
import smithy4s.xml.Xml
import smithy4s.{ codecs, Blob }

object CodecDecider {

private val jsonCodecs = Json.payloadCodecs
.withJsoniterCodecCompiler(
Json.jsoniter
)

private val jsonEncoder: BlobEncoder.Compiler = jsonCodecs.encoders
private val jsonDecoder: BlobDecoder.Compiler = jsonCodecs.decoders
private val metadataEncoder = Metadata.Encoder
private val metadataDecoder: CachedSchemaCompiler[Metadata.Decoder] = Metadata.Decoder

def encoder(
contentType: Seq[String]
): CachedSchemaCompiler[codecs.BlobEncoder] =
contentType match {
case Seq(MimeTypes.JSON) => jsonEncoder
case Seq(MimeTypes.XML) => Xml.encoders
case _ =>
CachedSchemaCompiler
.getOrElse(smithy4s.codecs.StringAndBlobCodecs.encoders, jsonEncoder)
}

def requestDecoder(
contentType: Seq[String]
): CachedSchemaCompiler[Decoder[Either[Throwable, *], PlayHttpRequest[Blob], *]] =
HttpRestSchema.combineDecoderCompilers[Either[Throwable, *], PlayHttpRequest[Blob]](
metadataDecoder
.mapK(
Decoder.in[Either[MetadataError, *]].composeK[Metadata, PlayHttpRequest[Blob]](_.metadata)
)
.asInstanceOf[CachedSchemaCompiler[Decoder[Either[Throwable, *], PlayHttpRequest[Blob], *]]],
decoder(contentType)
.mapK(
Decoder.in[Either[PayloadError, *]].composeK[Blob, PlayHttpRequest[Blob]](_.body)
)
.asInstanceOf[CachedSchemaCompiler[Decoder[Either[Throwable, *], PlayHttpRequest[Blob], *]]],
_ => Right(())
)(eitherZipper)

def requestEncoder(
contentType: Seq[String]
): CachedCompiler[EndpointRequest] =
HttpRestSchema.combineWriterCompilers(
metadataEncoder.mapK(
metadataPipe
),
encoder(contentType).mapK(
blobPipe
),
_ => false
)

def httpResponseDecoder(
contentType: Seq[String]
): CachedSchemaCompiler[Decoder[Either[Throwable, *], HttpResponse[Blob], *]] =
HttpRestSchema.combineDecoderCompilers[Either[Throwable, *], HttpResponse[Blob]](
metadataDecoder
.mapK(
Decoder
.in[Either[MetadataError, *]]
.composeK[Metadata, HttpResponse[Blob]](r =>
Metadata(Map.empty, Map.empty, headers = r.headers, statusCode = Some(r.statusCode))
)
)
.asInstanceOf[CachedSchemaCompiler[Decoder[Either[Throwable, *], HttpResponse[Blob], *]]],
decoder(contentType)
.mapK(
Decoder.in[Either[PayloadError, *]].composeK[Blob, HttpResponse[Blob]](_.body)
)
.asInstanceOf[CachedSchemaCompiler[Decoder[Either[Throwable, *], HttpResponse[Blob], *]]],
_ => Right(())
)(eitherZipper)

def httpMessageEncoder(
contentType: Seq[String]
): CachedCompiler[HttpResponse[Blob]] =
HttpRestSchema.combineWriterCompilers(
metadataEncoder.mapK(
httpRequestMetadataPipe
),
encoder(contentType).mapK(
httpRequestBlobPipe
),
_ => false
)

private val httpRequestBodyLift: Writer[HttpResponse[Blob], Blob] =
Writer.lift[HttpResponse[Blob], Blob]((res, blob) => res.copy(body = blob))
private val httpRequestMetadataLift: Writer[HttpResponse[Blob], Metadata] =
Writer.lift[HttpResponse[Blob], Metadata]((res, metadata) =>
res.addHeaders(metadata.headers.map { case (insensitive, value) =>
(insensitive, value)
})
)
private val httpRequestBlobPipe: PolyFunction[Encoder[Blob, *], Writer[HttpResponse[Blob], *]] =
smithy4s.codecs.Encoder.pipeToWriterK[HttpResponse[Blob], Blob](httpRequestBodyLift)
private val httpRequestMetadataPipe: PolyFunction[Encoder[Metadata, *], Writer[HttpResponse[Blob], *]] =
smithy4s.codecs.Encoder.pipeToWriterK(httpRequestMetadataLift)

private val blobLift: Writer[EndpointRequest, Blob] =
Writer.lift[EndpointRequest, Blob]((res, blob) => res.copy(body = blob))
private val metadataLift: Writer[EndpointRequest, Metadata] =
Writer.lift[EndpointRequest, Metadata]((res, metadata) =>
res.addHeaders(metadata.headers.map { case (insensitive, value) =>
(insensitive, value)
})
)
private val blobPipe: PolyFunction[Encoder[Blob, *], Writer[EndpointRequest, *]] =
smithy4s.codecs.Encoder.pipeToWriterK[EndpointRequest, Blob](blobLift)
private val metadataPipe: PolyFunction[Encoder[Metadata, *], Writer[EndpointRequest, *]] =
smithy4s.codecs.Encoder.pipeToWriterK(metadataLift)

def decoder(
contentType: Seq[String]
): CachedSchemaCompiler[BlobDecoder] =
contentType match {
case Seq(MimeTypes.JSON) => jsonDecoder
case Seq(MimeTypes.XML) => Xml.decoders
case _ =>
CachedSchemaCompiler
.getOrElse(smithy4s.codecs.StringAndBlobCodecs.decoders, jsonDecoder)
}

}

This file was deleted.

Loading

0 comments on commit 1180afb

Please sign in to comment.