Problem with custom directive: provide(x) doesn't progress to inner route

24 views
Skip to first unread message

mar

unread,
Jun 25, 2015, 9:26:51 AM6/25/15
to spray...@googlegroups.com

I am trying to create a custom directive that transforms a value by using a deserializer and providing the value to an inner route. The use case looks somewhat like this:

  implicit val tokenDecrypter: Deserializer[String, SalesToken] = ... 

  val route: Route = {
    path("foo") {
      entity(as[SalesRegistration]) { registration =>
        transform(registration.encryptedToken.into[SalesToken]) { token =>
          complete(s"decrypted token: ${token.toString}")
        }
      }
    }
  }

I have defined a Transformer type like so

  trait Transformer[From] {
    type To
    def from: From
    def deserializer: Deserializer[From, To]
  }

  implicit class TransformProxy[From](value: From) {
    def into[T](implicit d: Deserializer[From, T]) = new Transformer[From] {
      type To = T
      val from = value
      val deserializer = d
    }
  }

… and implement the directive:

  def transform[From](transformer: Transformer[From]) = new Directive1[transformer.To] {
    override def happly(f: transformer.To :: HNil => Route): Route = { ctx =>
      transformer.deserializer(transformer.from) match {
        case Right(transformed) =>
          provide(transformed)

        case Left(error: MalformedContent) =>
          ctx.reject(ValidationRejection(error.errorMessage))

        case Left(error) =>
          ctx.reject(ValidationRejection("Failed to deserialize item"))
      }
    }
  }

This compiles but the inner complete() is never reached and the request dies after a timeout.

I hope someone can shed some light on the issue.

mar

unread,
Jun 25, 2015, 9:30:25 AM6/25/15
to spray...@googlegroups.com
To be more specific: after stepping through the code the deserialiser handle its business well and I end up in the case Right(transformed) section of the match, after which everything stalls

mar

unread,
Jun 25, 2015, 12:21:39 PM6/25/15
to spray...@googlegroups.com
I figured it out: my error was I didn't apply the 'f' function to my transformed value.


On Thursday, 25 June 2015 15:26:51 UTC+2, mar wrote:
Reply all
Reply to author
Forward
0 new messages