Giter VIP home page Giter VIP logo

action-zipper's Introduction

ActionZipper Build Status

Play2 ActionBuilder Composition Support

package controllers

import play.api.mvc._
import jp.t2v.lab.play2.actzip._
import scala.concurrent.{ExecutionContext, Future}

class Application (cc: ControllerComponents) extends AbstractController(cc) {

  private implicit val ec: ExecutionContext = cc.executionContext

  val MyAction = AuthAction zip DBTxAction

  def index = MyAction.async(parse.json) { case (authRequest, dbRequest) =>
    println(authRequest.user)
    println(dbRequest.dbSession)
    Future.successful(Ok(views.html.index("Your new application is ready.")))
  }

}

Motivation

ActionBuilder is Play2 standard action composition system.

It can compose other ActionFunctions that have same Request type.

However, ActionBuilders that have different request type can not compose each other. (for example, AuthenticationAction and DBAction)

Action-Zipper provides the way that make any ActionBuilders enable to compose.

Installation

Add dependency declarations into your Build.scala or build.sbt file:

libraryDependencies += "jp.t2v" %% "action-zipper" % "0.2.0"

Target

  • Scala 2.11.x & Scala 2.12.x
  • Play 2.6.x

Alias

any and anyAsync

Since ActionBuilder#apply and ActionBuilder#async are overloaded, we can not use Pattern Matching Anonymous Functions.

// compile error!!
def index = MyAction { case (authRequest, dbRequest) =>

So ZippedActionN has any and anyAsync method that can use instead of apply and async

def index = MyAction.any { case (authRequest, dbRequest) =>
def index = MyAction.anyAsync { case (authRequest, dbRequest) =>

More Example

package controllers

import play.api.mvc._
import jp.t2v.lab.play2.actzip._
import scala.concurrent.{ExecutionContext, Future}

class Application (cc: ControllerComponents) extends AbstractController(cc) {

  private implicit val ec: ExecutionContext = cc.executionContext

  // it can chain more than 2
  val Action3 = Action zip Action zip Action
  
  val Action4 = Action zip Action zip Action zip Action
  
  // ZippedAction can zip another ZipedAction
  val Action7 = Action3 zip Action4

  def index = Action7.any { case (_, _, _, _, _, _, _) =>
    Ok(views.html.index("7 action are zipped"))
  }

}

action-zipper's People

Contributors

gakuzzzz avatar

Stargazers

Jules Ivanic avatar KAWACHI Takashi avatar ⊣˚∆˚⊢ avatar kenji yoshida avatar Dario A Lencina-Talarico avatar Tao Yang avatar Joe Zulli avatar Sho Kohara avatar Ken Kaizu avatar Akiomi Kamakura avatar nazoking avatar Shohei Shimomura avatar  avatar Francis De Brabandere avatar Chris Birchall avatar

Watchers

Joe Zulli avatar kenji yoshida avatar  avatar James Cloos avatar  avatar

Forkers

xuwei-k

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.