From 8a2797ca5c69c7bc82c0386b3a32ce476578622d Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Mon, 28 Feb 2022 21:20:15 +0530 Subject: [PATCH 01/11] enhancement: added combine operator --- .../example/PlainTextBenchmarkServer.scala | 30 ++++++++++++++----- zio-http/src/main/scala/zhttp/http/Http.scala | 26 +++++++++++++++- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/example/src/main/scala/example/PlainTextBenchmarkServer.scala b/example/src/main/scala/example/PlainTextBenchmarkServer.scala index 17952e0e5d..74c4770ad1 100644 --- a/example/src/main/scala/example/PlainTextBenchmarkServer.scala +++ b/example/src/main/scala/example/PlainTextBenchmarkServer.scala @@ -12,9 +12,19 @@ import zio.{App, ExitCode, UIO, URIO} object Main extends App { private val message: String = "Hello, World!" + private val json: String = """{"greetings": "Hello World!"}""" + + val path1 = "/plaintext" + val path2 = "/json" private val STATIC_SERVER_NAME = AsciiString.cached("zio-http") + private val frozenJsonResponse = Response + .json(json) + .withServerTime + .withServer(STATIC_SERVER_NAME) + .freeze + private val frozenResponse = Response .text(message) .withServerTime @@ -22,16 +32,20 @@ object Main extends App { .freeze override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = { - frozenResponse - .flatMap(server(_).make.useForever) - .provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(8)) - .exitCode + val s = for { + res1 <- frozenResponse + res2 <- frozenJsonResponse + } yield server(res1, res2) + s.flatMap(_.make.useForever.provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(8))).exitCode } - private val path = "/plaintext" - private def app(response: Response) = Http.fromHExit(HExit.succeed(response)).whenPathEq(path) - private def server(response: Response) = - Server.app(app(response)) ++ + private def app(response: Response, json: Response) = + Http.fromHExit(HExit.succeed(response)).whenPathEq(path1) ++ Http + .fromHExit(HExit.succeed(json)) + .whenPathEq(path2) + + private def server(response: Response, json: Response) = + Server.app(app(response, json)) ++ Server.port(8080) ++ Server.error(_ => UIO.unit) ++ Server.disableLeakDetection ++ diff --git a/zio-http/src/main/scala/zhttp/http/Http.scala b/zio-http/src/main/scala/zhttp/http/Http.scala index 032e8b85cd..a8329e1a5a 100644 --- a/zio-http/src/main/scala/zhttp/http/Http.scala +++ b/zio-http/src/main/scala/zhttp/http/Http.scala @@ -4,6 +4,7 @@ import io.netty.buffer.{ByteBuf, ByteBufUtil} import io.netty.channel.ChannelHandler import io.netty.handler.codec.http.HttpHeaderNames import zhttp.html._ +import zhttp.http.HExit.Effect import zhttp.http.headers.HeaderModifier import zhttp.service.server.ServerTime import zhttp.service.{Handler, HttpRuntime, Server} @@ -223,7 +224,7 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => * Named alias for `++` */ final def defaultWith[R1 <: R, E1 >: E, A1 <: A, B1 >: B](other: Http[R1, E1, A1, B1]): Http[R1, E1, A1, B1] = - self.foldHttp(Http.fail, Http.die, Http.succeed, other) + Http.Combine(self, other) /** * Delays production of output B for the specified duration of time @@ -596,6 +597,24 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => } catch { case e: Throwable => HExit.die(e) } + + case Combine(self, other) => { + self.execute(a) match { + case HExit.Effect(zio) => { + Effect( + zio.foldM( + { + case Some(error) => ZIO.fail(Option(error.asInstanceOf[E])) + case None => other.execute(a).toZIO + }, + b => ZIO.succeed(b.asInstanceOf[B]), + ), + ) + } + case HExit.Empty => other.execute(a) + case u => u.asInstanceOf[HExit[R, E, B]] + } + } } } @@ -1053,6 +1072,11 @@ object Http { private case class Attempt[A](a: () => A) extends Http[Any, Nothing, Any, A] + private final case class Combine[R, E, EE, A, B, BB]( + self: Http[R, E, A, B], + other: Http[R, EE, A, BB], + ) extends Http[R, EE, A, BB] + private final case class FromHExit[R, E, B](h: HExit[R, E, B]) extends Http[R, E, Any, B] private final case class When[R, E, A, B](f: A => Boolean, other: Http[R, E, A, B]) extends Http[R, E, A, B] From 9d54e076c3edf08e35d3c379e3cb3a00ecb7a9db Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Tue, 1 Mar 2022 11:55:48 +0530 Subject: [PATCH 02/11] added test cases with three apps --- .../src/test/scala/zhttp/http/HttpSpec.scala | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala index 887b95a428..7fb404c4fb 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala @@ -164,11 +164,40 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { val actual = (a ++ b).execute(2) assert(actual)(isSuccess(equalTo("B"))) } + + test("should resolve third") { + val a = Http.collect[Int] { case 1 => "A" } + val b = Http.collect[Int] { case 2 => "B" } + val c = Http.collect[Int] { case 3 => "C" } + val actual = (a ++ b ++ c).execute(3) + assert(actual)(isSuccess(equalTo("C"))) + } + + test("should resolve third") { + val a = Http.collectZIO[Int] { case 1 => UIO("A") } + val b = Http.collectZIO[Int] { case 2 => UIO("B") } + val c = Http.collectZIO[Int] { case 3 => UIO("C") } + val actual = (a ++ b ++ c).execute(3) + assert(actual)(isEffect) + } + test("should not resolve") { val a = Http.collect[Int] { case 1 => "A" } val b = Http.collect[Int] { case 2 => "B" } - val actual = (a ++ b).execute(3) + val c = Http.collect[Int] { case 3 => "C" } + val actual = (a ++ b ++ c).execute(4) assert(actual)(isEmpty) + } + + test("should not resolve") { + val a = Http.collectZIO[Int] { case 1 => UIO("A") } + val b = Http.collectZIO[Int] { case 2 => UIO("B") } + val c = Http.collectZIO[Int] { case 3 => UIO("C") } + val actual = (a ++ b ++ c).execute(4) + assert(actual)(isEmpty) + } + + test("should fail with second") { + val a = Http.collect[Int] { case 1 => "A" } + val b = Http.collectHttp[Int] { case 2 => Http.fail(100) } + val c = Http.collect[Int] { case 3 => "C" } + val actual = (a ++ b ++ c).execute(2) + assert(actual)(isFailure(equalTo(100))) }, ) + suite("asEffect")( @@ -204,6 +233,13 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { val b = Http.succeed("B") val actual = (a ++ b).execute(2) assert(actual)(isSuccess(equalTo("B"))) + } + + test("should resolve third") { + val a = Http.empty.flatten + val b = Http.empty.flatten + val c = Http.succeed("C") + val actual = (a ++ b ++ c).execute(3) + assert(actual)(isSuccess(equalTo("C"))) }, ) + suite("route")( From 5f522b1bbfbd9adeef9513afd1bbb7f642d2e63d Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Tue, 1 Mar 2022 12:33:44 +0530 Subject: [PATCH 03/11] refactor: plaintextBenchmarkServer --- .../example/PlainTextBenchmarkServer.scala | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/example/src/main/scala/example/PlainTextBenchmarkServer.scala b/example/src/main/scala/example/PlainTextBenchmarkServer.scala index 74c4770ad1..e68ca77ca7 100644 --- a/example/src/main/scala/example/PlainTextBenchmarkServer.scala +++ b/example/src/main/scala/example/PlainTextBenchmarkServer.scala @@ -1,7 +1,7 @@ package example import io.netty.util.AsciiString -import zhttp.http._ +import zhttp.http.{Http, _} import zhttp.service.server.ServerChannelFactory import zhttp.service.{EventLoopGroup, Server} import zio.{App, ExitCode, UIO, URIO} @@ -11,41 +11,43 @@ import zio.{App, ExitCode, UIO, URIO} */ object Main extends App { - private val message: String = "Hello, World!" - private val json: String = """{"greetings": "Hello World!"}""" + private val plainTextMessage: String = "Hello, World!" + private val jsonMessage: String = """{"greetings": "Hello World!"}""" - val path1 = "/plaintext" - val path2 = "/json" + private val plaintextPath = "/plaintext" + private val jsonPath = "/json" private val STATIC_SERVER_NAME = AsciiString.cached("zio-http") private val frozenJsonResponse = Response - .json(json) + .json(jsonMessage) .withServerTime .withServer(STATIC_SERVER_NAME) .freeze - private val frozenResponse = Response - .text(message) + private val frozenPlainTextResponse = Response + .text(plainTextMessage) .withServerTime .withServer(STATIC_SERVER_NAME) .freeze - override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = { - val s = for { - res1 <- frozenResponse - res2 <- frozenJsonResponse - } yield server(res1, res2) - s.flatMap(_.make.useForever.provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(8))).exitCode - } - - private def app(response: Response, json: Response) = - Http.fromHExit(HExit.succeed(response)).whenPathEq(path1) ++ Http - .fromHExit(HExit.succeed(json)) - .whenPathEq(path2) - - private def server(response: Response, json: Response) = - Server.app(app(response, json)) ++ + private def plainTextApp(response: Response) = Http.fromHExit(HExit.succeed(response)).whenPathEq(plaintextPath) + + private def jsonApp(json: Response) = Http.fromHExit(HExit.succeed(json)).whenPathEq(jsonPath) + + private def app = for { + plainTextResponse <- frozenPlainTextResponse + jsonResponse <- frozenJsonResponse + } yield plainTextApp(plainTextResponse) ++ jsonApp(jsonResponse) + + override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = + app + .flatMap(server(_).make.useForever) + .provideCustomLayer(ServerChannelFactory.auto ++ EventLoopGroup.auto(8)) + .exitCode + + private def server(app: HttpApp[Any, Nothing]) = + Server.app(app) ++ Server.port(8080) ++ Server.error(_ => UIO.unit) ++ Server.disableLeakDetection ++ From 646a43c0daafab53ab03df48eda4d4b8b6972bc7 Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Tue, 1 Mar 2022 15:20:20 +0530 Subject: [PATCH 04/11] test case updated --- zio-http/src/test/scala/zhttp/http/HttpSpec.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala index 7fb404c4fb..aa6c40fa71 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala @@ -172,8 +172,8 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { assert(actual)(isSuccess(equalTo("C"))) } + test("should resolve third") { - val a = Http.collectZIO[Int] { case 1 => UIO("A") } - val b = Http.collectZIO[Int] { case 2 => UIO("B") } + val a = Http.collect[Int] { case 1 => "A" } + val b = Http.collect[Int] { case 2 => "B" } val c = Http.collectZIO[Int] { case 3 => UIO("C") } val actual = (a ++ b ++ c).execute(3) assert(actual)(isEffect) From 82a5d49b1f2eb06c8ffb877eb37c562955c48a4e Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Tue, 1 Mar 2022 18:24:56 +0530 Subject: [PATCH 05/11] removed flatten from test --- zio-http/src/test/scala/zhttp/http/HttpSpec.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala index aa6c40fa71..2836111a4e 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala @@ -235,8 +235,8 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { assert(actual)(isSuccess(equalTo("B"))) } + test("should resolve third") { - val a = Http.empty.flatten - val b = Http.empty.flatten + val a = Http.empty + val b = Http.empty val c = Http.succeed("C") val actual = (a ++ b ++ c).execute(3) assert(actual)(isSuccess(equalTo("C"))) From 34392616461e3edf5ae0b559b92543737f2217fc Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Wed, 2 Mar 2022 13:19:20 +0530 Subject: [PATCH 06/11] test case updated --- zio-http/src/test/scala/zhttp/http/HttpSpec.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala index 2836111a4e..d3bb01986e 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala @@ -194,9 +194,9 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { } + test("should fail with second") { val a = Http.collect[Int] { case 1 => "A" } - val b = Http.collectHttp[Int] { case 2 => Http.fail(100) } + val b = Http.fail(100) val c = Http.collect[Int] { case 3 => "C" } - val actual = (a ++ b ++ c).execute(2) + val actual = (a ++ b ++ c).execute(3) assert(actual)(isFailure(equalTo(100))) }, ) + From 74ac818eaa034184119855a7754463c6cde9690b Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Thu, 3 Mar 2022 12:03:04 +0530 Subject: [PATCH 07/11] added test for collectHttp --- zio-http/src/test/scala/zhttp/http/HttpSpec.scala | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala index d3bb01986e..bf7b6537b7 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala @@ -198,6 +198,13 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { val c = Http.collect[Int] { case 3 => "C" } val actual = (a ++ b ++ c).execute(3) assert(actual)(isFailure(equalTo(100))) + } + + test("should fail with second") { + val a = Http.collectHttp[Int] { case 1 => Http.succeed("A") } + val b = Http.collectHttp[Int] { case 2 => Http.fail(100) } + val c = Http.collectHttp[Int] { case 3 => Http.succeed("C") } + val actual = (a ++ b ++ c).execute(2) + assert(actual)(isFailure(equalTo(100))) }, ) + suite("asEffect")( From a90397cf4d25c4846de3ef44c5138ebb6dfbe64b Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Mon, 7 Mar 2022 17:38:37 +0530 Subject: [PATCH 08/11] simplified tests --- .../src/test/scala/zhttp/http/HttpSpec.scala | 50 ++++++------------- 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala index bf7b6537b7..8ea77f1e7e 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala @@ -164,47 +164,32 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { val actual = (a ++ b).execute(2) assert(actual)(isSuccess(equalTo("B"))) } + - test("should resolve third") { - val a = Http.collect[Int] { case 1 => "A" } - val b = Http.collect[Int] { case 2 => "B" } - val c = Http.collect[Int] { case 3 => "C" } - val actual = (a ++ b ++ c).execute(3) - assert(actual)(isSuccess(equalTo("C"))) - } + - test("should resolve third") { - val a = Http.collect[Int] { case 1 => "A" } - val b = Http.collect[Int] { case 2 => "B" } - val c = Http.collectZIO[Int] { case 3 => UIO("C") } - val actual = (a ++ b ++ c).execute(3) - assert(actual)(isEffect) - } + test("should not resolve") { val a = Http.collect[Int] { case 1 => "A" } val b = Http.collect[Int] { case 2 => "B" } - val c = Http.collect[Int] { case 3 => "C" } - val actual = (a ++ b ++ c).execute(4) + val actual = (a ++ b).execute(3) assert(actual)(isEmpty) } + test("should not resolve") { - val a = Http.collectZIO[Int] { case 1 => UIO("A") } - val b = Http.collectZIO[Int] { case 2 => UIO("B") } - val c = Http.collectZIO[Int] { case 3 => UIO("C") } - val actual = (a ++ b ++ c).execute(4) + val a = Http.empty + val b = Http.empty + val c = Http.empty + val actual = (a ++ b ++ c).execute(()) assert(actual)(isEmpty) } + test("should fail with second") { - val a = Http.collect[Int] { case 1 => "A" } + val a = Http.empty val b = Http.fail(100) - val c = Http.collect[Int] { case 3 => "C" } - val actual = (a ++ b ++ c).execute(3) + val c = Http.succeed("A") + val actual = (a ++ b ++ c).execute(()) assert(actual)(isFailure(equalTo(100))) } + - test("should fail with second") { - val a = Http.collectHttp[Int] { case 1 => Http.succeed("A") } - val b = Http.collectHttp[Int] { case 2 => Http.fail(100) } - val c = Http.collectHttp[Int] { case 3 => Http.succeed("C") } - val actual = (a ++ b ++ c).execute(2) - assert(actual)(isFailure(equalTo(100))) + test("should resolve third") { + val a = Http.empty + val b = Http.empty + val c = Http.succeed("C") + val actual = (a ++ b ++ c).execute(()) + assert(actual)(isSuccess(equalTo("C"))) }, ) + suite("asEffect")( @@ -240,13 +225,6 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { val b = Http.succeed("B") val actual = (a ++ b).execute(2) assert(actual)(isSuccess(equalTo("B"))) - } + - test("should resolve third") { - val a = Http.empty - val b = Http.empty - val c = Http.succeed("C") - val actual = (a ++ b ++ c).execute(3) - assert(actual)(isSuccess(equalTo("C"))) }, ) + suite("route")( From 02c72ff11f51dbe94439710de0416c453ff48a7e Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Wed, 9 Mar 2022 16:24:25 +0530 Subject: [PATCH 09/11] added failing test --- zio-http/src/main/scala/zhttp/http/Http.scala | 16 ++-------------- .../src/test/scala/zhttp/http/HttpSpec.scala | 6 ++++++ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/http/Http.scala b/zio-http/src/main/scala/zhttp/http/Http.scala index a8329e1a5a..6fc5554760 100644 --- a/zio-http/src/main/scala/zhttp/http/Http.scala +++ b/zio-http/src/main/scala/zhttp/http/Http.scala @@ -4,7 +4,6 @@ import io.netty.buffer.{ByteBuf, ByteBufUtil} import io.netty.channel.ChannelHandler import io.netty.handler.codec.http.HttpHeaderNames import zhttp.html._ -import zhttp.http.HExit.Effect import zhttp.http.headers.HeaderModifier import zhttp.service.server.ServerTime import zhttp.service.{Handler, HttpRuntime, Server} @@ -600,19 +599,8 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => case Combine(self, other) => { self.execute(a) match { - case HExit.Effect(zio) => { - Effect( - zio.foldM( - { - case Some(error) => ZIO.fail(Option(error.asInstanceOf[E])) - case None => other.execute(a).toZIO - }, - b => ZIO.succeed(b.asInstanceOf[B]), - ), - ) - } - case HExit.Empty => other.execute(a) - case u => u.asInstanceOf[HExit[R, E, B]] + case HExit.Empty => other.execute(a) + case u => u.asInstanceOf[HExit[R, E, B]] } } } diff --git a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala index 8ea77f1e7e..3330a5caa7 100644 --- a/zio-http/src/test/scala/zhttp/http/HttpSpec.scala +++ b/zio-http/src/test/scala/zhttp/http/HttpSpec.scala @@ -190,6 +190,12 @@ object HttpSpec extends DefaultRunnableSpec with HExitAssertion { val c = Http.succeed("C") val actual = (a ++ b ++ c).execute(()) assert(actual)(isSuccess(equalTo("C"))) + } + + testM("should resolve second") { + val a = Http.fromHExit(HExit.Effect(ZIO.fail(None))) + val b = Http.succeed(2) + val actual = (a ++ b).execute(()).toZIO.either + assertM(actual)(isRight) }, ) + suite("asEffect")( From 676925165971f8eeac8f91bebece825b7530fbcb Mon Sep 17 00:00:00 2001 From: shrutiverma97 Date: Wed, 9 Mar 2022 16:26:03 +0530 Subject: [PATCH 10/11] fixed test --- zio-http/src/main/scala/zhttp/http/Http.scala | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/http/Http.scala b/zio-http/src/main/scala/zhttp/http/Http.scala index 6fc5554760..a8329e1a5a 100644 --- a/zio-http/src/main/scala/zhttp/http/Http.scala +++ b/zio-http/src/main/scala/zhttp/http/Http.scala @@ -4,6 +4,7 @@ import io.netty.buffer.{ByteBuf, ByteBufUtil} import io.netty.channel.ChannelHandler import io.netty.handler.codec.http.HttpHeaderNames import zhttp.html._ +import zhttp.http.HExit.Effect import zhttp.http.headers.HeaderModifier import zhttp.service.server.ServerTime import zhttp.service.{Handler, HttpRuntime, Server} @@ -599,8 +600,19 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => case Combine(self, other) => { self.execute(a) match { - case HExit.Empty => other.execute(a) - case u => u.asInstanceOf[HExit[R, E, B]] + case HExit.Effect(zio) => { + Effect( + zio.foldM( + { + case Some(error) => ZIO.fail(Option(error.asInstanceOf[E])) + case None => other.execute(a).toZIO + }, + b => ZIO.succeed(b.asInstanceOf[B]), + ), + ) + } + case HExit.Empty => other.execute(a) + case u => u.asInstanceOf[HExit[R, E, B]] } } } From 3f643fc6bb52bdc7b41ecf725f857b41447689fd Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Thu, 10 Mar 2022 19:56:43 +0530 Subject: [PATCH 11/11] refactor: cleaning up the execute method in Http --- .../zhttp.benchmarks/HttpCombineEval.scala | 4 ++-- zio-http/src/main/scala/zhttp/http/Http.scala | 19 +++++-------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/zio-http-benchmarks/src/main/scala/zhttp.benchmarks/HttpCombineEval.scala b/zio-http-benchmarks/src/main/scala/zhttp.benchmarks/HttpCombineEval.scala index eca25652d3..8894c6540a 100644 --- a/zio-http-benchmarks/src/main/scala/zhttp.benchmarks/HttpCombineEval.scala +++ b/zio-http-benchmarks/src/main/scala/zhttp.benchmarks/HttpCombineEval.scala @@ -14,13 +14,13 @@ class HttpCombineEval { private val spec = (0 to MAX).foldLeft(app)((a, _) => a ++ app) @Benchmark - def benchmarkNotFound(): Unit = { + def empty(): Unit = { spec.execute(-1) () } @Benchmark - def benchmarkOk(): Unit = { + def ok(): Unit = { spec.execute(0) () } diff --git a/zio-http/src/main/scala/zhttp/http/Http.scala b/zio-http/src/main/scala/zhttp/http/Http.scala index a8329e1a5a..1602d3ab37 100644 --- a/zio-http/src/main/scala/zhttp/http/Http.scala +++ b/zio-http/src/main/scala/zhttp/http/Http.scala @@ -4,7 +4,6 @@ import io.netty.buffer.{ByteBuf, ByteBufUtil} import io.netty.channel.ChannelHandler import io.netty.handler.codec.http.HttpHeaderNames import zhttp.html._ -import zhttp.http.HExit.Effect import zhttp.http.headers.HeaderModifier import zhttp.service.server.ServerTime import zhttp.service.{Handler, HttpRuntime, Server} @@ -600,19 +599,11 @@ sealed trait Http[-R, +E, -A, +B] extends (A => ZIO[R, Option[E], B]) { self => case Combine(self, other) => { self.execute(a) match { - case HExit.Effect(zio) => { - Effect( - zio.foldM( - { - case Some(error) => ZIO.fail(Option(error.asInstanceOf[E])) - case None => other.execute(a).toZIO - }, - b => ZIO.succeed(b.asInstanceOf[B]), - ), - ) - } - case HExit.Empty => other.execute(a) - case u => u.asInstanceOf[HExit[R, E, B]] + case HExit.Empty => other.execute(a) + case exit: HExit.Success[_] => exit.asInstanceOf[HExit[R, E, B]] + case exit: HExit.Failure[_] => exit.asInstanceOf[HExit[R, E, B]] + case exit: HExit.Die => exit + case exit @ HExit.Effect(_) => exit.defaultWith(other.execute(a)).asInstanceOf[HExit[R, E, B]] } } }