Skip to content

Commit

Permalink
Change Unmarshal#to to use a default context #78 (#947)
Browse files Browse the repository at this point in the history
* Change `Unmarshal#to` to use a default context #78

* Update Unmarshal.scala
  • Loading branch information
athieriot authored and ktoso committed Mar 17, 2017
1 parent ad7fabf commit 34c0837
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ object Unmarshal {
class Unmarshal[A](val value: A) {
/**
* Unmarshals the value to the given Type using the in-scope Unmarshaller.
* Uses the default materializer [[ExecutionContext]] if no implicit execution context is provided.
* If you expect the marshalling to be heavy, it is suggested to provide a specialized context for those operations.
*/
def to[B](implicit um: Unmarshaller[A, B], ec: ExecutionContext, mat: Materializer): Future[B] = um(value)
def to[B](implicit um: Unmarshaller[A, B], ec: ExecutionContext = null, mat: Materializer): Future[B] = {
val context: ExecutionContext = if (ec == null) mat.executionContext else ec

um(value)(context, mat)
}
}
17 changes: 16 additions & 1 deletion docs/src/test/scala/docs/http/scaladsl/UnmarshalSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UnmarshalSpec extends AkkaSpec {
"use unmarshal" in {
//#use-unmarshal
import akka.http.scaladsl.unmarshalling.Unmarshal
import system.dispatcher // ExecutionContext
import system.dispatcher // Optional ExecutionContext (default from Materializer)
implicit val materializer: Materializer = ActorMaterializer()

import scala.concurrent.Await
Expand All @@ -28,4 +28,19 @@ class UnmarshalSpec extends AkkaSpec {
//#use-unmarshal
}

"use unmarshal without execution context" in {
import akka.http.scaladsl.unmarshalling.Unmarshal
implicit val materializer: Materializer = ActorMaterializer()

import scala.concurrent.Await
import scala.concurrent.duration._

val intFuture = Unmarshal("42").to[Int]
val int = Await.result(intFuture, 1.second) // don't block in non-test code!
int shouldEqual 42

val boolFuture = Unmarshal("off").to[Boolean]
val bool = Await.result(boolFuture, 1.second) // don't block in non-test code!
bool shouldBe false
}
}

0 comments on commit 34c0837

Please sign in to comment.