-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from ovotech/add-fallthrough-to
Add fallthroughTo so that HttpRoutes and HttpApp can be composed
- Loading branch information
Showing
6 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
natchez-extras-http4s/src/main/scala/com/ovoenergy/natchez/extras/http4s/server/syntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ovoenergy.natchez.extras.http4s.server | ||
|
||
import cats.Monad | ||
import cats.data.{Kleisli, OptionT} | ||
|
||
object syntax { | ||
/** | ||
* Given an `A => F[Option[B]]` and an `A => F[B]` run the first function | ||
* and if it returns F[None] then fall through to the second function. | ||
* This is useful for composing HttpRoutes[F] with HttpApp[F] | ||
*/ | ||
implicit class KleisliSyntax[F[_]: Monad, A, B](a: Kleisli[OptionT[F, *], A, B]) { | ||
def fallthroughTo(b: Kleisli[F, A, B]): Kleisli[F, A, B] = Kleisli(r => a.run(r).getOrElseF(b.run(r))) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...-extras-http4s/src/test/scala/com/ovoenergy/natchez/extras/http4s/server/SyntaxTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.ovoenergy.natchez.extras.http4s.server | ||
|
||
import cats.effect.IO | ||
import com.ovoenergy.natchez.extras.http4s.server.syntax._ | ||
import org.http4s.Status.{InsufficientStorage, Ok} | ||
import org.http4s.{HttpApp, HttpRoutes, Request, Response} | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class SyntaxTest extends AnyWordSpec with Matchers { | ||
|
||
"fallThroughTo" should { | ||
|
||
"Call the second Kleisli if the first returns None" in { | ||
val routes: HttpRoutes[IO] = HttpRoutes.empty | ||
val app: HttpApp[IO] = HttpApp.pure(Response(status = Ok)) | ||
val result = routes.fallthroughTo(app).run(Request()).unsafeRunSync() | ||
result.status shouldBe Ok | ||
} | ||
|
||
"Not call the second Kleisli if the first returns a result" in { | ||
val routes: HttpRoutes[IO] = HttpRoutes.pure(Response(status = Ok)) | ||
val app: HttpApp[IO] = HttpApp.pure(Response(status = InsufficientStorage)) | ||
val result = routes.fallthroughTo(app).run(Request()).unsafeRunSync() | ||
result.status shouldBe Ok | ||
} | ||
} | ||
} |