Skip to content

Commit

Permalink
avoid generating internal frames (#589)
Browse files Browse the repository at this point in the history
This PR makes the `Frame` macro fail to compile in case the frame is in
the `kyo` package and fixes the methods in the codebase that were
missing the implicit.
  • Loading branch information
fwbrasil authored Aug 22, 2024
1 parent 8099aff commit c8b801b
Show file tree
Hide file tree
Showing 35 changed files with 210 additions and 145 deletions.
8 changes: 4 additions & 4 deletions kyo-cache/shared/src/main/scala/kyo/Cache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import scala.util.Success
class Cache(private[kyo] val store: Store):
def memo[A, B: Flat, S](
f: A => B < S
): A => B < (Async & S) =
)(using Frame): A => B < (Async & S) =
(v: A) =>
Promise.init[Throwable, B].map { p =>
val key = (this, v)
Expand Down Expand Up @@ -44,21 +44,21 @@ class Cache(private[kyo] val store: Store):

def memo2[T1, T2, S, B: Flat](
f: (T1, T2) => B < S
): (T1, T2) => B < (Async & S) =
)(using Frame): (T1, T2) => B < (Async & S) =
val m = memo[(T1, T2), B, S](f.tupled)
(t1, t2) => m((t1, t2))
end memo2

def memo3[T1, T2, T3, S, B: Flat](
f: (T1, T2, T3) => B < S
): (T1, T2, T3) => B < (Async & S) =
)(using Frame): (T1, T2, T3) => B < (Async & S) =
val m = memo[(T1, T2, T3), B, S](f.tupled)
(t1, t2, t3) => m((t1, t2, t3))
end memo3

def memo4[T1, T2, T3, T4, S, B: Flat](
f: (T1, T2, T3, T4) => B < S
): (T1, T2, T3, T4) => B < (Async & S) =
)(using Frame): (T1, T2, T3, T4) => B < (Async & S) =
val m = memo[(T1, T2, T3, T4), B, S](f.tupled)
(t1, t2, t3, t4) => m((t1, t2, t3, t4))
end memo4
Expand Down
27 changes: 17 additions & 10 deletions kyo-caliban/src/main/scala/kyo/Resolvers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,34 @@ object Resolvers:
private given StreamConstructor[Nothing] =
(_: ZStream[Any, Throwable, Byte]) => throw new Throwable("Streaming is not supported")

def run[A, S](v: HttpInterpreter[Any, CalibanError] < (Resolvers & S)): NettyKyoServerBinding < (ZIOs & Abort[CalibanError] & S) =
def run[A, S](v: HttpInterpreter[Any, CalibanError] < (Resolvers & S))(
using Frame
): NettyKyoServerBinding < (ZIOs & Abort[CalibanError] & S) =
run[A, S](NettyKyoServer())(v)

def run[A, S](server: NettyKyoServer)(v: HttpInterpreter[Any, CalibanError] < (Resolvers & S))
: NettyKyoServerBinding < (ZIOs & Abort[CalibanError] & S) =
def run[A, S](server: NettyKyoServer)(v: HttpInterpreter[Any, CalibanError] < (Resolvers & S))(
using Frame
): NettyKyoServerBinding < (ZIOs & Abort[CalibanError] & S) =
ZIOs.get(ZIO.runtime[Any]).map(runtime => run(server, runtime)(v))

def run[R, A, S](runner: Runner[R])(v: HttpInterpreter[Runner[R], CalibanError] < (Resolvers & S))(using
tag: Tag[Runner[R]]
def run[R, A, S](runner: Runner[R])(v: HttpInterpreter[Runner[R], CalibanError] < (Resolvers & S))(
using
tag: Tag[Runner[R]],
frame: Frame
): NettyKyoServerBinding < (ZIOs & Abort[CalibanError] & S) =
run[R, A, S](NettyKyoServer(), runner)(v)

def run[R, A, S](server: NettyKyoServer, runner: Runner[R])(v: HttpInterpreter[Runner[R], CalibanError] < (Resolvers & S))(using
tag: Tag[Runner[R]]
def run[R, A, S](server: NettyKyoServer, runner: Runner[R])(v: HttpInterpreter[Runner[R], CalibanError] < (Resolvers & S))(
using
tag: Tag[Runner[R]],
frame: Frame
): NettyKyoServerBinding < (ZIOs & Abort[CalibanError] & S) =
ZIOs.get(ZIO.runtime[Any]).map(runtime => run(server, runtime.withEnvironment(ZEnvironment(runner)))(v))

def run[R, A, S](
server: NettyKyoServer,
runtime: Runtime[R]
)(v: HttpInterpreter[R, CalibanError] < (Resolvers & S)): NettyKyoServerBinding < (ZIOs & Abort[CalibanError] & S) =
)(v: HttpInterpreter[R, CalibanError] < (Resolvers & S))(using Frame): NettyKyoServerBinding < (ZIOs & Abort[CalibanError] & S) =
for
interpreter <- v
endpoints = interpreter.serverEndpoints[R, NoStreams](NoStreams).map(convertEndpoint(_, runtime))
Expand All @@ -56,15 +63,15 @@ object Resolvers:
def get[R](api: GraphQL[R])(using
requestCodec: JsonCodec[GraphQLRequest],
responseValueCodec: JsonCodec[ResponseValue]
): HttpInterpreter[R, CalibanError] < Resolvers =
)(using Frame): HttpInterpreter[R, CalibanError] < Resolvers =
ZIOs.get(api.interpreter.map(HttpInterpreter(_)))

private val rightUnit: Right[Nothing, Unit] = Right(())

private def convertEndpoint[R, I](
endpoint: ServerEndpoint.Full[Unit, Unit, I, TapirResponse, CalibanResponse[NoStreams.BinaryStream], NoStreams, [x] =>> RIO[R, x]],
runtime: Runtime[R]
): ServerEndpoint[Any, KyoSttpMonad.M] =
)(using Frame): ServerEndpoint[Any, KyoSttpMonad.M] =
ServerEndpoint[Unit, Unit, I, TapirResponse, CalibanResponse[NoStreams.BinaryStream], Any, KyoSttpMonad.M](
endpoint.endpoint.asInstanceOf[Endpoint[Unit, I, TapirResponse, CalibanResponse[NoStreams.BinaryStream], Any]],
_ => _ => rightUnit,
Expand Down
2 changes: 2 additions & 0 deletions kyo-caliban/src/main/scala/kyo/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ given zioSchema[R, A: Flat, S](using ev: Schema[R, A], ev2: (A < S) <:< (A < (Ab
ev.toType_(isInput, isSubscription)

override def resolve(value: A < S): Step[R] =
given Frame = Frame.internal
QueryStep(ZQuery.fromZIONow(ZIOs.run(ev2(value).map(ev.resolve))))

end zioSchema
Expand All @@ -33,6 +34,7 @@ given runnerSchema[R, A: Flat, S](using ev: Schema[R, A], tag: zio.Tag[Runner[S]
ev.toType_(isInput, isSubscription)

override def resolve(value: A < S): Step[R & Runner[S]] =
given Frame = Frame.internal
QueryStep(ZQuery.fromZIONow(ZIO.serviceWithZIO[Runner[S]](_(value.map(ev.resolve)))))

end runnerSchema
Loading

0 comments on commit c8b801b

Please sign in to comment.