Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Created May 5, 2012 09:09
Show Gist options
  • Save rirakkumya/2601128 to your computer and use it in GitHub Desktop.
Save rirakkumya/2601128 to your computer and use it in GitHub Desktop.
play2でAction内がどんどん入れ子になっていく問題を解決する方法。Action compositionにしてみた
package controllers
import play.api._
import play.api.mvc._
import play.api.cache._
import play.api.Play.current
object EA {
case class Bind[a1,a2](x:Either[a1,a2]) {
def #>>[b](f:a2 => Either[a1,b]):Either[a1,b] = x.right flatMap f
}
implicit def either2Bind[a](s:Either[Result,a]) = Bind(s)
implicit def any2Bind[a](x:a) = Bind[Result,a](Right(x))
def EitherAction(
f:Request[AnyContent] => Either[Result,Result]
):Action[AnyContent] = Action{f(_) merge}
}
object Application extends Controller {
import EA._
def index(id:String) = EitherAction {req =>
id #>> {
Cache.getAs[String](_) match {
case Some(r) if r.startsWith("x") => Right("foo")
case Some(r) => Right(r)
case None => Left(BadRequest)
}
} #>> {
Cache.getAs[String](_) match {
case Some(r) if r == "badcode" => Left(BadRequest)
case Some(r) => Right(r)
case None => Left(NotFound)
}
} #>> {x => Right(Ok(x))}
}
}
GET /:id controllers.Application.index(id)
GET /assets/*file controllers.Assets.at(path="/public", file)
@rirakkumya
Copy link
Author

コンパイルエラー解決しました!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment