From 057d30bf5946350552272d72075bd9d22f064d12 Mon Sep 17 00:00:00 2001 From: "liviu.ungureanu" Date: Tue, 11 Feb 2020 12:23:09 +0000 Subject: [PATCH] fixed test name --- .../scala/com/spingo/op_rabbit/Directives.scala | 14 ++++++++------ .../scala/com/spingo/op_rabbit/EnhancedTry.scala | 10 ++++++++++ .../com/spingo/op_rabbit/DirectivesSpec.scala | 12 ++++++------ 3 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 core/src/main/scala/com/spingo/op_rabbit/EnhancedTry.scala diff --git a/core/src/main/scala/com/spingo/op_rabbit/Directives.scala b/core/src/main/scala/com/spingo/op_rabbit/Directives.scala index 7a8ede4..c52e4a2 100644 --- a/core/src/main/scala/com/spingo/op_rabbit/Directives.scala +++ b/core/src/main/scala/com/spingo/op_rabbit/Directives.scala @@ -7,6 +7,7 @@ import scala.language.implicitConversions import shapeless._ import com.spingo.op_rabbit.Binding._ import com.spingo.op_rabbit.Exchange.ExchangeType +import EnhancedTry._ import scala.util.{Failure, Success, Try} @@ -180,22 +181,23 @@ trait Directives { } /** - Extract the message body. Uses a [[com.spingo.op_rabbit.RabbitUnmarshaller RabbitUnmarshaller]] to deserialize. - In case the body cannot be unmarshalled, it will issue a None otherwise Some unmarshalled body. + Extract the message body as a Either. Uses a [[com.spingo.op_rabbit.RabbitUnmarshaller RabbitUnmarshaller]] to deserialize. + In case the body cannot be unmarshalled, the exception is present in the Left of the Either. + In this way the client code can have the unmarshalling error reason. Example: {{{ - bodyOpt(as[JobDescription]) { jobDescriptionOpt => ... + bodyOpt(as[JobDescription]) { jobDescriptionEither => ... } }}} */ - def bodyOpt[T](um: RabbitUnmarshaller[T]): Directive1[Option[T]] = new Directive1[Option[T]] { - def happly(fn: ::[Option[T], HNil] => Handler): Handler = { (promise, delivery) => + def bodyEither[T](um: RabbitUnmarshaller[T]): Directive1[Either[Throwable, T]] = new Directive1[Either[Throwable, T]] { + def happly(fn: ::[Either[Throwable, T], HNil] => Handler): Handler = { (promise, delivery) => val dataTry = Try { um.unmarshall(delivery.body, Option(delivery.properties.getContentType), Option(delivery.properties.getContentEncoding)) } - fn(dataTry.toOption :: HNil)(promise, delivery) + fn(dataTry.toEither :: HNil)(promise, delivery) } } diff --git a/core/src/main/scala/com/spingo/op_rabbit/EnhancedTry.scala b/core/src/main/scala/com/spingo/op_rabbit/EnhancedTry.scala new file mode 100644 index 0000000..03aa2f1 --- /dev/null +++ b/core/src/main/scala/com/spingo/op_rabbit/EnhancedTry.scala @@ -0,0 +1,10 @@ +package com.spingo.op_rabbit + +import scala.util.{Success, Try} + +object EnhancedTry { + implicit class EnhancedTryImpl[T](t: Try[T]) { + def toEither: Either[Throwable, T] = + t.transform(success => Success(Right(success)), exception => Success(Left(exception))).get + } +} diff --git a/core/src/test/scala/com/spingo/op_rabbit/DirectivesSpec.scala b/core/src/test/scala/com/spingo/op_rabbit/DirectivesSpec.scala index 8023a63..0892078 100644 --- a/core/src/test/scala/com/spingo/op_rabbit/DirectivesSpec.scala +++ b/core/src/test/scala/com/spingo/op_rabbit/DirectivesSpec.scala @@ -108,24 +108,24 @@ class DirectivesSpec extends FunSpec with Matchers with Inside { } should be (acked) } - it("yields the value for both directives when one is bodyOpt") { + it("yields the value for both directives when one is bodyEither") { val delivery = testDelivery(body = "hi".getBytes, properties = Seq(ReplyTo("place"))) resultFor(delivery) { - (bodyOpt(as[String]) & property(ReplyTo)) { (bodyOpt, replyTo) => + (bodyEither(as[String]) & property(ReplyTo)) { (bodyEither, replyTo) => replyTo should be ("place") - bodyOpt should contain ("hi") + bodyEither.right.get should be ("hi") ack } } should be (acked) } } - describe("bodyOpt") { + describe("bodyEither") { it("yields the value as an option") { val delivery = testDelivery(body = "hi".getBytes) resultFor(delivery) { - (bodyOpt(as[String])) { (bodyOpt) => - bodyOpt should contain ("hi") + (bodyEither(as[String])) { (bodyEither) => + bodyEither.right.get should be ("hi") ack } } should be (acked)