Skip to content
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

Implement ZQuery#provideLayer #270

Merged
merged 1 commit into from
Mar 7, 2020
Merged
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
2 changes: 1 addition & 1 deletion core/src/main/scala/caliban/execution/Executor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ object Executor {
wrap(
wrapper
.f(query, fieldInfo)
.foldM(error => ZQuery.fromEffect(errors.update(error :: _)).map(_ => NullValue), ZQuery.succeed)
.foldM(error => ZQuery.fromEffect(errors.update(error :: _)).map(_ => NullValue), ZQuery.succeed(_))
)(tail, fieldInfo)

}
Expand Down
43 changes: 40 additions & 3 deletions core/src/main/scala/zquery/ZQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ sealed trait ZQuery[-R, +E, +A] { self =>
final def provide(r: Described[R])(implicit ev: NeedsEnv[R]): ZQuery[Any, E, A] =
provideSome(Described(_ => r.value, s"_ => ${r.description}"))

/**
* Provides the part of the environment that is not part of the `ZEnv`,
* leaving a query that only depends on the `ZEnv`.
*/
final def provideCustomLayer[E1 >: E, R1 <: Has[_]](
layer: Described[ZLayer[ZEnv, E1, R1]]
)(implicit ev: ZEnv with R1 <:< R, tagged: Tagged[R1]): ZQuery[ZEnv, E1, A] =
provideSomeLayer[ZEnv](layer)

/**
* Provides a layer to this query, which translates it to another level.
*/
final def provideLayer[E1 >: E, R0, R1 <: Has[_]](
layer: Described[ZLayer[R0, E1, R1]]
)(implicit ev1: R1 <:< R, ev2: NeedsEnv[R]): ZQuery[R0, E1, A] =
new ZQuery[R0, E1, A] {
def step(cache: Cache): ZIO[R0, Nothing, Result[R0, E1, A]] =
layer.value.build.run.use {
case Exit.Failure(e) => ZIO.succeed(Result.fail(e))
case Exit.Success(r) => self.step(cache).provide(r).map(_.provide(Described(r, layer.description)))
}
}

/**
* Provides this query with part of its required environment.
*/
Expand All @@ -171,6 +194,13 @@ sealed trait ZQuery[-R, +E, +A] { self =>
self.step(cache).provideSome(f.value).map(_.provideSome(f))
}

/**
* Splits the environment into two parts, providing one part using the
* specified layer and leaving the remainder `R0`.
*/
final def provideSomeLayer[R0 <: Has[_]]: ZQuery.ProvideSomeLayer[R0, R, E, A] =
new ZQuery.ProvideSomeLayer[R0, R, E, A](self)

/**
* Returns an effect that models executing this query.
*/
Expand Down Expand Up @@ -309,7 +339,7 @@ object ZQuery {
/**
* Constructs a query that dies with the specified error.
*/
def die(t: Throwable): ZQuery[Any, Nothing, Nothing] =
def die(t: => Throwable): ZQuery[Any, Nothing, Nothing] =
ZQuery(ZIO.die(t))

/**
Expand All @@ -320,7 +350,7 @@ object ZQuery {
/**
* Constructs a query that fails with the specified error.
*/
def fail[E](error: E): ZQuery[Any, E, Nothing] =
def fail[E](error: => E): ZQuery[Any, E, Nothing] =
ZQuery(ZIO.succeed(Result.fail(Cause.fail(error))))

/**
Expand Down Expand Up @@ -416,9 +446,16 @@ object ZQuery {
/**
* Constructs a query that succeeds with the specified value.
*/
def succeed[A](value: A): ZQuery[Any, Nothing, A] =
def succeed[A](value: => A): ZQuery[Any, Nothing, A] =
ZQuery(ZIO.succeed(Result.done(value)))

final class ProvideSomeLayer[R0 <: Has[_], -R, +E, +A](private val self: ZQuery[R, E, A]) extends AnyVal {
def apply[E1 >: E, R1 <: Has[_]](
layer: Described[ZLayer[R0, E1, R1]]
)(implicit ev1: R0 with R1 <:< R, ev2: NeedsEnv[R], tagged: Tagged[R1]): ZQuery[R0, E1, A] =
self.provideLayer[E1, R0, R0 with R1](Described(ZLayer.identity[R0] ++ layer.value, layer.description))
}

/**
* Constructs a query from an effect that returns a result.
*/
Expand Down