Skip to content

wip: caches v2 #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: smithy4s-integration
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ object CirceJsonCodec {
* This enables encoding values of type `A` to JSON and decoding JSON back into `A`, using the structure defined by
* the Smithy schema.
*/
def fromSchema[A](implicit schema: Schema[A]): Codec[A] = Codec.from(
def fromSchema[A](
schema: Schema[A],
documentDecoderCache: Document.Decoder.Cache,
documentEncoderCache: Document.Encoder.Cache
): Codec[A] = Codec.from(
c => {
c.as[Json]
.map(fromJson)
.flatMap { d =>
Document
.decode[A](d)
Document.Decoder
.fromSchema(schema, documentDecoderCache)
.decode(d)
.left
.map(e =>
DecodingFailure(DecodingFailure.Reason.CustomReason(e.getMessage), c.history ++ toCursorOps(e.path))
)
}
},
a => documentToJson(Document.encode(a))
a => documentToJson(Document.Encoder.fromSchema(schema, documentEncoderCache).encode(a))
)

private def toCursorOps(path: PayloadPath): List[CursorOp] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import jsonrpclib.Channel
import jsonrpclib.Monadic
import smithy4s.~>
import smithy4s.schema._
import smithy4s.Document
import smithy4s.Service
import smithy4s.ShapeId

Expand All @@ -30,12 +31,14 @@ object ClientStub {
private class ClientStub[Alg[_[_, _, _, _, _]], F[_]: Monadic](val service: Service[Alg], channel: Channel[F]) {

def compile: service.Impl[F] = {
val documentEncoderCache = Document.Encoder.createCache()
val documentDecoderCache = Document.Decoder.createCache()
val interpreter = new service.FunctorEndpointCompiler[F] {
def apply[I, E, O, SI, SO](e: service.Endpoint[I, E, O, SI, SO]): I => F[O] = {
val shapeId = e.id
val spec = EndpointSpec.fromHints(e.hints).toRight(NotJsonRPCEndpoint(shapeId)).toTry.get

jsonRPCStub(e, spec)
jsonRPCStub(e, spec, documentEncoderCache, documentDecoderCache)
}
}

Expand All @@ -44,11 +47,15 @@ private class ClientStub[Alg[_[_, _, _, _, _]], F[_]: Monadic](val service: Serv

def jsonRPCStub[I, E, O, SI, SO](
smithy4sEndpoint: service.Endpoint[I, E, O, SI, SO],
endpointSpec: EndpointSpec
endpointSpec: EndpointSpec,
documentEncoderCache: Document.Encoder.Cache,
documentDecoderCache: Document.Decoder.Cache
): I => F[O] = {

implicit val inputCodec: Codec[I] = CirceJsonCodec.fromSchema(smithy4sEndpoint.input)
implicit val outputCodec: Codec[O] = CirceJsonCodec.fromSchema(smithy4sEndpoint.output)
implicit val inputCodec: Codec[I] =
CirceJsonCodec.fromSchema(smithy4sEndpoint.input, documentDecoderCache, documentEncoderCache)
implicit val outputCodec: Codec[O] =
CirceJsonCodec.fromSchema(smithy4sEndpoint.output, documentDecoderCache, documentEncoderCache)

endpointSpec match {
case EndpointSpec.Notification(methodName) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import jsonrpclib.Payload
import smithy4s.kinds.FunctorAlgebra
import smithy4s.kinds.FunctorInterpreter
import smithy4s.schema.ErrorSchema
import smithy4s.Document
import smithy4s.Service

import _root_.smithy4s.{Endpoint => Smithy4sEndpoint}
Expand All @@ -34,11 +35,15 @@ object ServerEndpoints {
)(implicit service: Service[Alg], F: Monadic[F]): List[Endpoint[F]] = {
val transformedService = JsonRpcTransformations.apply(service)
val interpreter: transformedService.FunctorInterpreter[F] = transformedService.toPolyFunction(impl)

val documentDecoderCache = Document.Decoder.createCache()
val documentEncoderCache = Document.Encoder.createCache()

transformedService.endpoints.toList.flatMap { smithy4sEndpoint =>
EndpointSpec
.fromHints(smithy4sEndpoint.hints)
.map { endpointSpec =>
jsonRPCEndpoint(smithy4sEndpoint, endpointSpec, interpreter)
jsonRPCEndpoint(smithy4sEndpoint, endpointSpec, interpreter, documentDecoderCache, documentEncoderCache)
}
.toList
}
Expand All @@ -61,11 +66,15 @@ object ServerEndpoints {
private def jsonRPCEndpoint[F[_]: Monadic, Op[_, _, _, _, _], I, E, O, SI, SO](
smithy4sEndpoint: Smithy4sEndpoint[Op, I, E, O, SI, SO],
endpointSpec: EndpointSpec,
impl: FunctorInterpreter[Op, F]
impl: FunctorInterpreter[Op, F],
documentDecoderCache: Document.Decoder.Cache,
documentEncoderCache: Document.Encoder.Cache
): Endpoint[F] = {

implicit val inputCodec: Codec[I] = CirceJsonCodec.fromSchema(smithy4sEndpoint.input)
implicit val outputCodec: Codec[O] = CirceJsonCodec.fromSchema(smithy4sEndpoint.output)
implicit val inputCodec: Codec[I] =
CirceJsonCodec.fromSchema(smithy4sEndpoint.input, documentDecoderCache, documentEncoderCache)
implicit val outputCodec: Codec[O] =
CirceJsonCodec.fromSchema(smithy4sEndpoint.output, documentDecoderCache, documentEncoderCache)

def errorResponse(throwable: Throwable): F[E] = throwable match {
case smithy4sEndpoint.Error((_, e)) => e.pure
Expand All @@ -86,7 +95,8 @@ object ServerEndpoints {
impl(op)
}
case Some(errorSchema) =>
implicit val errorCodec: ErrorEncoder[E] = errorCodecFromSchema(errorSchema)
implicit val errorCodec: ErrorEncoder[E] =
errorCodecFromSchema(errorSchema, documentEncoderCache, documentDecoderCache)
Endpoint[F](methodName).apply[I, E, O] { (input: I) =>
val op = smithy4sEndpoint.wrap(input)
impl(op).attempt.flatMap {
Expand All @@ -98,8 +108,12 @@ object ServerEndpoints {
}
}

private def errorCodecFromSchema[A](s: ErrorSchema[A]): ErrorEncoder[A] = {
val circeCodec = CirceJsonCodec.fromSchema(s.schema)
private def errorCodecFromSchema[A](
s: ErrorSchema[A],
documentEncoderCache: Document.Encoder.Cache,
documentDecoderCache: Document.Decoder.Cache
): ErrorEncoder[A] = {
val circeCodec = CirceJsonCodec.fromSchema(s.schema, documentDecoderCache, documentEncoderCache)
(a: A) =>
ErrorPayload(
0,
Expand Down
Loading