Kanaka-io / play-monadic-actions

A simple scala DSL to allow clean and monadic style for Play! Actions
Other
128 stars 29 forks source link

Does not work with tuple forms #1

Closed GitsMcGee closed 9 years ago

GitsMcGee commented 9 years ago

Hi everyone. It appears that this library does not handle forms of tuple type. For example:

package controllers

import controllers.ActionDSL.MonadicActions
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}

object MonadicActionsController extends Controller with MonadicActions {

  val singleFieldForm = Form(
    single("message" -> nonEmptyText)
  )

  def singleField = Action.async { implicit request =>

    for {
      message <- singleFieldForm.bindFromRequest ?| (formWithErrors => BadRequest(formWithErrors.errorsAsJson))
    } yield { Ok(message) }
  }

  val twoFieldForm = Form(
    tuple("id" -> longNumber,
      "message" -> nonEmptyText)
  )

  def twoFields = Action.async { implicit request =>

    for {
      (id, message) <- twoFieldForm.bindFromRequest ?| (formWithErrors => BadRequest(formWithErrors.errorsAsJson))
    } yield { Ok }
  }
}

In the above example, singleField works great, but twoFields doesn't compile. The specific error message I get is: could not find implicit value for parameter M: scalaz.Monoid[play.api.mvc.Result]. Is this a bug, or could I be doing something wrong?

vil1 commented 9 years ago

Hi,

You're not doing anything wrong. Actually, I already came accross this problem and I must confess I have no solution at the moment, but it's definitly the main item on my todo list for this project. So stay tuned, I'll keep you updated as soon as I have a clue.

Thanks for raising that issue (and happy new year, depending on your timezone).

GitsMcGee commented 9 years ago

Hi @vil1

Sounds good. I will try and think of a solution as well, though you will probably find one before I do.

Happy New Year!

vil1 commented 9 years ago

The issue should be solved in 1.0.1-SNAPSHOT but the solution have some possibly unwanted consequences. Please see README for explanations. I'll publish a 1.0.1 release if you find the solution satisfying.

GitsMcGee commented 9 years ago

Interesting. Your solutions works for me. :+1:

vil1 commented 9 years ago

Ok, version 1.0.1 is released, it should be soon available on sonatype