-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'series/0.18' into series-017-into-018-again
- Loading branch information
Showing
45 changed files
with
866 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
modules/aws-http4s/src/smithy4s/aws/internals/AwsXmlCodecs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* Copyright 2021-2022 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s | ||
package aws | ||
package internals | ||
|
||
import cats.effect.Concurrent | ||
import cats.syntax.all._ | ||
import cats.Applicative | ||
import fs2.compression.Compression | ||
import smithy4s.http4s.kernel._ | ||
import smithy4s.Endpoint | ||
import smithy4s.schema.CachedSchemaCompiler | ||
import org.http4s.EntityDecoder | ||
import smithy4s.xml.XmlDocument | ||
import org.http4s.MediaRange | ||
import fs2.data.xml._ | ||
import fs2.data.xml.dom._ | ||
import cats.data.EitherT | ||
import smithy4s.kinds.PolyFunction | ||
import org.http4s.EntityEncoder | ||
import smithy4s.capability.Covariant | ||
|
||
import org.http4s.MediaType | ||
import smithy4s.http.Metadata | ||
|
||
private[aws] object AwsXmlCodecs { | ||
|
||
def make[F[_]: Concurrent: Compression](): UnaryClientCodecs.Make[F] = | ||
new UnaryClientCodecs.Make[F] { | ||
def apply[I, E, O, SI, SO]( | ||
endpoint: Endpoint.Base[I, E, O, SI, SO] | ||
): UnaryClientCodecs[F, I, E, O] = { | ||
|
||
val stringAndBlobsEntityEncoders = | ||
smithy4s.http.StringAndBlobCodecs.WriterCompiler | ||
.mapK( | ||
Covariant.liftPolyFunction[Option]( | ||
EntityEncoders.fromHttpMediaWriterK[F] | ||
) | ||
) | ||
|
||
val stringAndBlobsEntityDecoders = | ||
smithy4s.http.StringAndBlobCodecs.ReaderCompiler | ||
.mapK( | ||
Covariant.liftPolyFunction[Option]( | ||
EntityDecoders.fromHttpMediaReaderK[F] | ||
) | ||
) | ||
|
||
val xmlEntityEncoders: CachedSchemaCompiler[EntityEncoder[F, *]] = | ||
XmlDocument.Encoder.mapK( | ||
new PolyFunction[XmlDocument.Encoder, EntityEncoder[F, *]] { | ||
def apply[A](fa: XmlDocument.Encoder[A]): EntityEncoder[F, A] = | ||
xmlEntityEncoder[F].contramap(fa.encode) | ||
} | ||
) | ||
|
||
val encoders = CachedSchemaCompiler.getOrElse( | ||
stringAndBlobsEntityEncoders, | ||
xmlEntityEncoders | ||
) | ||
|
||
val xmlEntityDecoders: CachedSchemaCompiler[EntityDecoder[F, *]] = | ||
XmlDocument.Decoder.mapK( | ||
new PolyFunction[XmlDocument.Decoder, EntityDecoder[F, *]] { | ||
def apply[A]( | ||
fa: XmlDocument.Decoder[A] | ||
): EntityDecoder[F, A] = | ||
xmlEntityDecoder[F].flatMapR(xmlDocument => | ||
EitherT.liftF { | ||
fa.decode(xmlDocument) | ||
.leftMap(fromXmlToHttpError) | ||
.liftTo[F] | ||
} | ||
) | ||
} | ||
) | ||
|
||
val decoders = CachedSchemaCompiler.getOrElse( | ||
stringAndBlobsEntityDecoders, | ||
xmlEntityDecoders | ||
) | ||
|
||
val restEncoders = | ||
RequestEncoder.restSchemaCompiler[F](Metadata.AwsEncoder, encoders) | ||
|
||
val restDecoders = | ||
ResponseDecoder.restSchemaCompiler(Metadata.AwsDecoder, decoders) | ||
|
||
val errorDecoders = restDecoders.contramapSchema( | ||
smithy4s.schema.Schema.transformHintsLocallyK( | ||
_ ++ smithy4s.Hints( | ||
smithy4s.xml.internals.XmlStartingPath( | ||
List("ErrorResponse", "Error") | ||
) | ||
) | ||
) | ||
) | ||
|
||
val discriminator = AwsErrorTypeDecoder.fromResponse(errorDecoders) | ||
|
||
val transformEncoders = applyCompression[F](endpoint.hints) | ||
val finalEncoders = transformEncoders(restEncoders) | ||
|
||
val make = | ||
UnaryClientCodecs | ||
.Make[F](finalEncoders, restDecoders, errorDecoders, discriminator) | ||
make.apply(endpoint) | ||
} | ||
} | ||
|
||
private def xmlEntityDecoder[F[_]: Concurrent] | ||
: EntityDecoder[F, XmlDocument] = | ||
EntityDecoder.decodeBy( | ||
MediaRange.parse("application/xml").getOrElse(sys.error("TODO")) | ||
) { media => | ||
EitherT.liftF( | ||
media.body | ||
.through(fs2.text.utf8.decode[F]) | ||
.through(events[F, String]()) | ||
.through(referenceResolver()) | ||
.through(normalize) | ||
.through(documents[F, XmlDocument]) | ||
.head | ||
.compile | ||
.last | ||
.map( | ||
_.getOrElse( | ||
// TODO: This isn't right | ||
XmlDocument( | ||
XmlDocument.XmlElem( | ||
XmlDocument.XmlQName(None, "Unit"), | ||
List.empty, | ||
List.empty | ||
) | ||
) | ||
) | ||
) | ||
) | ||
} | ||
|
||
private def xmlEntityEncoder[F[_]: Applicative] | ||
: EntityEncoder[F, XmlDocument] = | ||
EntityEncoder.encodeBy( | ||
org.http4s.headers.`Content-Type`.apply(MediaType.application.xml) | ||
) { xmlDocument => | ||
val body: fs2.Chunk[Byte] = XmlDocument.documentEventifier | ||
.eventify(xmlDocument) | ||
.through(render(collapseEmpty = false)) | ||
.through(fs2.text.utf8.encode[fs2.Pure]) | ||
.compile | ||
.foldChunks(fs2.Chunk.empty[Byte])(_ ++ _) | ||
|
||
org.http4s.Entity.apply( | ||
fs2.Stream.chunk(body), | ||
Some(body.size.toLong) | ||
) | ||
} | ||
|
||
private def fromXmlToHttpError( | ||
xmlDecodeError: smithy4s.xml.XmlDecodeError | ||
): smithy4s.http.HttpContractError = { | ||
smithy4s.http.HttpPayloadError( | ||
xmlDecodeError.path.toPayloadPath, | ||
"", | ||
xmlDecodeError.message | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
modules/bootstrapped/src/generated/smithy4s/example/_package/MyPackageString.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package smithy4s.example._package | ||
|
||
import smithy4s.Hints | ||
import smithy4s.Newtype | ||
import smithy4s.Schema | ||
import smithy4s.ShapeId | ||
import smithy4s.schema.Schema.bijection | ||
import smithy4s.schema.Schema.string | ||
|
||
object MyPackageString extends Newtype[String] { | ||
val id: ShapeId = ShapeId("smithy4s.example.package", "MyPackageString") | ||
val hints: Hints = Hints.empty | ||
val underlyingSchema: Schema[String] = string.withId(id).addHints(hints) | ||
implicit val schema: Schema[MyPackageString] = bijection(underlyingSchema, asBijection) | ||
} |
7 changes: 7 additions & 0 deletions
7
modules/bootstrapped/src/generated/smithy4s/example/_package/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package smithy4s.example | ||
|
||
package object _package { | ||
|
||
type MyPackageString = smithy4s.example._package.MyPackageString.Type | ||
|
||
} |
Oops, something went wrong.