Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Await methods for Future and FutureValidation; renamed FutureValidati…

…on.or to FutureValidation.orElse
  • Loading branch information...
commit 17f1eccdd47401f5aef73031f09442039634b141 1 parent 080142a
Dave Gurnell davegurnell authored
32 core/src/main/scala/bigtop/concurrent/FutureImplicits.scala
View
@@ -1,25 +1,36 @@
package bigtop
package concurrent
-import akka.dispatch.{Future, Promise}
+import akka.dispatch.{Await, Future, Promise}
+import akka.util.Duration
import blueeyes.bkka.AkkaDefaults
import scalaz.Validation
import scalaz.syntax.validation._
trait FutureImplicits extends AkkaDefaults {
+ class FutureW[A](inner: Future[A]) {
+ implicit val defaultDuration = Duration("3s")
- case class WithFutureFlatMap[F, S](val inner: FutureValidation[F, S]) {
+ def await: A = await()
- def flatMap[T](fn: (S) => Future[T]): FutureValidation[F, T] =
- FutureValidation(inner.inner flatMap { validation => delayFailure(validation.map(fn)) })
+ def await(duration: Duration = defaultDuration): A =
+ Await.result(inner, duration)
- }
+ def awaitOption: Option[A] = awaitOption()
- case class WithValidationFlatMap[F, S](val inner: FutureValidation[F, S]) {
+ def awaitOption(duration: Duration = defaultDuration): Option[A] =
+ try {
+ Some(await(duration))
+ } catch {
+ case exn: Exception => None
+ }
+ }
- def flatMap[G >: F, T](fn: (S) => Validation[G, T]): FutureValidation[G, T] =
- FutureValidation(inner.inner map { validation => validation.flatMap[T, G](fn) })
+ class FutureValidationW[E, S](val fv: FutureValidation[E,S]) extends FutureW(fv.inner) {
+ def awaitSuccess: S = awaitSuccess()
+ def awaitSuccess(duration: Duration = defaultDuration): S =
+ await(duration).toOption.get
}
def delayFailure[F, S](in: Validation[F, Future[S]]): Future[Validation[F, S]] =
@@ -40,6 +51,11 @@ trait FutureImplicits extends AkkaDefaults {
implicit def validationToFutureValidation[F, S](in: Validation[F, S]): FutureValidation[F, S] =
FutureValidation(Promise.successful(in))
+ implicit def futureValidationToFutureValidationW[E,S](fv: FutureValidation[E,S]) =
+ new FutureValidationW(fv)
+
+ implicit def futureToFutureW[A](f: Future[A]) =
+ new FutureW(f)
}
object FutureImplicits extends FutureImplicits
22 core/src/main/scala/bigtop/concurrent/FutureValidation.scala
View
@@ -1,8 +1,7 @@
package bigtop
package concurrent
-import akka.dispatch.{Await, Future, Promise}
-import akka.util.Duration
+import akka.dispatch.{Future, Promise}
import blueeyes.bkka.AkkaDefaults
import scalaz.Validation
import scalaz.syntax.validation._
@@ -39,7 +38,7 @@ case class FutureValidation[F, S](val inner: Future[Validation[F, S]])
()
}
- def or[G](f: F => FutureValidation[G, S]) =
+ def orElse[G](f: F => FutureValidation[G, S]) =
inner flatMap {
(v: Validation[F, S]) => v.fold(
success = s => Promise.successful(s.success[G]),
@@ -47,14 +46,6 @@ case class FutureValidation[F, S](val inner: Future[Validation[F, S]])
)
} fv
- /** Modifier to allow use of Future's flatMap: `foo.byFuture.flatMap( ... )` */
- def byFuture: WithFutureFlatMap[F, S] =
- WithFutureFlatMap(this)
-
- /** Modifier to allow use of Validation's flatMap: `foo.byFuture.flatMap( ... )` */
- def byValidation: WithValidationFlatMap[F, S] =
- WithValidationFlatMap(this)
-
/** And the success shall be failures and the failures shall be successes. This is how you do logical negation */
def invert: FutureValidation[S, F] =
FutureValidation(
@@ -63,14 +54,9 @@ case class FutureValidation[F, S](val inner: Future[Validation[F, S]])
failure = f => f.success))
)
- /** Only use for testing */
- def await =
- Await.result(inner, Duration("3s"))
-
- def lift: FutureValidation[F, S] =
- this
+ // def lift: FutureValidation[F, S] =
+ // this
def fv: FutureValidation[F, S] =
this
-
}
19 core/src/main/scala/bigtop/http/JsonServiceSpec.scala
View
@@ -37,25 +37,6 @@ trait JsonServiceSpec extends Specification
implicit val queryDuration = Duration("3s")
implicit val queryTimeout = Timeout(queryDuration)
- case class FutureW[A](val f: Future[A]) {
- def await: A =
- Await.result(f, Duration("3s"))
- }
-
- implicit def futureToFutureW[A](fv: Future[A]) =
- FutureW(fv)
-
- case class FutureValidationW[E,S](val fv: FutureValidation[E,S]) {
- def await: Validation[E,S] =
- Await.result(fv.inner, Duration("3s"))
-
- def awaitSuccess: S =
- await.toOption.get
- }
-
- implicit def futureValidationToFutureValidationW[E,S](fv: FutureValidation[E,S]) =
- FutureValidationW(fv)
-
// def convertResponse(in: HttpResponse[ByteChunk])(implicit timeout: Timeout): HttpResponse[JValue] =
// in.copy(content = in.content.map(chunk => chunkToFutureJValue(timeout)(chunk).await))
11 core/src/main/scala/bigtop/problem/Problem.scala
View
@@ -39,7 +39,10 @@ sealed trait Problem extends ProblemFormat {
}
object Problem extends ProblemImplicits {
- case class Message(val messageType: String, val args: Seq[(String, String)] = Seq())
+ case class Message(val messageType: String, val args: Seq[(String, String)] = Seq()) {
+ override def toString =
+ "Message(" + (messageType +: args.map(pair => pair._1 + "=" + pair._2)).mkString(", ") + ")"
+ }
}
final case class ServerProblem(val messages: Seq[Problem.Message], val logMessages: Seq[String]) extends Problem {
@@ -53,6 +56,9 @@ final case class ServerProblem(val messages: Seq[Problem.Message], val logMessag
def log(msg: String): Problem =
this.copy(logMessages = msg +: this.logMessages)
+
+ override def toString =
+ "ServerProblem(messages=%s, logMessages=%s)".format(messages, logMessages)
}
object ServerProblem extends ProblemImplicits {
@@ -79,6 +85,9 @@ final case class ClientProblem(val messages: Seq[Problem.Message], val logMessag
def log(msg: String): Problem =
this.copy(logMessages = msg +: this.logMessages)
+
+ override def toString =
+ "ClientProblem(messages=%s, logMessages=%s)".format(messages.toList, logMessages.toList)
}
object ClientProblem extends ProblemImplicits {
2  user/src/main/scala/bigtop/user/SecurityCheck.scala
View
@@ -12,7 +12,7 @@ case class SecurityCheck[A,U <: User](val predicate: SecurityPredicate[A,U]) ext
predicate(request, user)
def or(other: SecurityCheck[A,U]): SecurityCheck[A,U] =
- SecurityCheck((request, user) => predicate(request, user).or(_ => other(request, user)))
+ SecurityCheck((request, user) => predicate(request, user).orElse(_ => other(request, user)))
}
Please sign in to comment.
Something went wrong with that request. Please try again.