Play Framework: Chain ActionsBuilder and ActionRefiner

我的梦境 提交于 2019-12-23 02:51:40

问题


I am desperate. I tried to do ActionComposition like in the very last paragraph of the official docs: https://playframework.com/documentation/2.3.x/ScalaActionsComposition

My code:

object ActionBuilder1 extends ActionRefiner[Request, Request] {
  override protected def refine[A](request: Request[A]): Future[Either[Result, Request[A]]] = Future {Right(request)}
}


object ActionBuilder2 extends ActionBuilder[Request] {
  def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) : Future[Result] = {
    block(request)
  }
}

In my controller:

def yolo = ActionBuilder2 andThen ActionBuilder1 {
  Ok("ASd")
}

But the compiler says:

actions.ActionBuilder1.type does not take parameters
def yolo = ActionBuilder2 andThen ActionBuilder1 {
                                               ^

I really do not know why...


回答1:


I think Scala can't work out what you mean by:

ActionBuilder2 andThen ActionBuilder1 { // Some block }

so the easiest way seems to be declaring that chain as a thing in its own right, then applying the block to it:

val actionChain = ActionBuilder2 andThen ActionBuilder1

def yolo = actionChain { 
  Ok("yolo")
}

Verification that it's working in the desired order (2 then 1), via logging:

object ActionBuilder1 extends ActionRefiner[Request, Request] {
  override protected def refine[A](request: Request[A]): Future[Either[Result, Request[A]]] = Future {
    Logger.info("ActionBuilder1")
    Right(request)
  } 
} 


object ActionBuilder2 extends ActionBuilder[Request] {
  def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) : Future[Result] = {
    Logger.info("ActionBuilder2")
    block(request)
  }
}

In the console upon requesting the endpoint:

[info] application - ActionBuilder2
[info] application - ActionBuilder1


来源:https://stackoverflow.com/questions/29978399/play-framework-chain-actionsbuilder-and-actionrefiner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!