Skip to content

Instantly share code, notes, and snippets.

@vkostyukov
Created September 26, 2016 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkostyukov/0bce591b73273520ae9416600f07b5f0 to your computer and use it in GitHub Desktop.
Save vkostyukov/0bce591b73273520ae9416600f07b5f0 to your computer and use it in GitHub Desktop.
Dispatch on a case
import io.finch._
sealed trait Req
object Req {
case class Foo(i: Int) extends Req
case class Bar(s: String) extends Req
}
def dispatch[A](pf: PartialFunction[Req, Output[A]]): Endpoint[A] = new Endpoint[A] {
override def apply(i: Input): Endpoint.Result[A] = body.as[Req].apply(i).flatMap {
case (remainder, output @ Output.Payload(value, _)) if pf.isDefinedAt(value) =>
output.flatMap(a => pf(a))
case _ => None
}
}
val foo: Endpoint[Int] = dispatch {
case Foo(i) => Ok(i)
}
val bar: Endpoint[String] = dispatch {
case Bar(s) => Ok(s)
}
val api = post("my" :: "endpoint") :: (foo :+: bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment