Skip to content

Commit

Permalink
feature: Add collectManaged to Http (#909)
Browse files Browse the repository at this point in the history
* Add collectManaged

* comment typo
  • Loading branch information
beem812 authored Jan 27, 2022
1 parent b54e935 commit 8e73e9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit 8e73e9e

Please sign in to comment.