Search code examples
scalaliftscalaz

Liftweb return response from scalaz


I have a next code, which return:

 import com.twitter.util.{Future, NonFatal}
 import net.liftweb.http.{LiftResponse, OkResponse}

 def service(str: Strign) : Future[ValidationNel[String, LiftResponse]] = {
   if str == "ok"
     Future(OkResponse().successNel[String])
   else
     Future.value("Invalid string".failureNel[LiftResponse]) 
 }

 //and routes for it 

 serve {
   case "user" :: str :: _ Get _ => 
     service(str)   
 } 

but when i run, got No implicit view available from com.twitter.util.Future[scalaz.ValidationNel[String,net.liftweb.http.LiftResponse]] => net.liftweb.http.LiftResponse. How to convert this to ListResponse?


Solution

  • Well the error basically says that Lift can't render

    com.twitter.util.Future[scalaz.ValidationNel[String,net.liftweb.http.LiftResponse]]
    

    (At compile time.) To solve your problem you probably have to decompose your task. First, find out whether Lift can render that "Validation" thing:

    scalaz.ValidationNel[String,net.liftweb.http.LiftResponse]
    

    If it can't then you'll have to teach Lift. So you should create an implicit (visible in the scope) with the type signature ValidationNel[***] => LiftResponse. You can probably just convert this to a Box, which has a default way of rendering an error (empty box). Or you should write your custom code with "if" and 2 branches, one of which is BadResponse and another is the success.

    Another task would be to check whether Lift knows hot to render twitter-s Future-s. Again, you should create a simple data and try to render it out. Like Future(OkResponse()). In order to teach Lift about twitter Futures you probably would have to implement an implicit again, with the type signature Future[T] => LiftResponse. That would probably involve a RestContinuation.