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

feature: Add collectManaged to Http #909

Merged
merged 2 commits into from
Jan 27, 2022
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
15 changes: 15 additions & 0 deletions zio-http/src/main/scala/zhttp/http/Http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self =>
): Http[R1, E1, A1, C] =
self >>> Http.collectZIO(pf)

final def collectManaged[R1 <: R, E1 >: E, A1 <: A, B1 >: B, C](
pf: PartialFunction[B1, ZManaged[R1, E1, C]],
): Http[R1, E1, A1, C] =
self >>> Http.collectManaged(pf)

/**
* Named alias for `<<<`
*/
Expand Down Expand Up @@ -465,6 +470,11 @@ object Http {
*/
def collectZIO[A]: Http.PartialCollectZIO[A] = Http.PartialCollectZIO(())

/**
* Creates an Http app which accepts a request and produces response from a managed resource
*/
def collectManaged[A]: Http.PartialCollectManaged[A] = Http.PartialCollectManaged(())

/**
* Combines multiple Http apps into one
*/
Expand Down Expand Up @@ -619,6 +629,11 @@ object Http {
Http.collect[A] { case a if pf.isDefinedAt(a) => Http.fromZIO(pf(a)) }.flatten
}

final case class PartialCollectManaged[A](unit: Unit) extends AnyVal {
def apply[R, E, B](pf: PartialFunction[A, ZManaged[R, E, B]]): Http[R, E, A, B] =
Http.collect[A] { case a if pf.isDefinedAt(a) => Http.fromZIO(pf(a).useNow) }.flatten
}

final case class PartialCollect[A](unit: Unit) extends AnyVal {
def apply[B](pf: PartialFunction[A, B]): Http[Any, Nothing, A, B] = Collect(pf)
}
Expand Down
5 changes: 5 additions & 0 deletions zio-http/src/test/scala/zhttp/http/HttpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion {
val actual = a.execute(1)
assert(actual)(isEffect)
} +
test("should resolve managed") {
val a = Http.collectManaged[Int] { case 1 => ZManaged.succeed("A") }
val actual = a.execute(1)
assert(actual)(isEffect)
} +
test("should resolve second effect") {
val a = Http.empty.flatten
val b = Http.succeed("B")
Expand Down