Created
September 26, 2016 20:08
-
-
Save vkostyukov/0bce591b73273520ae9416600f07b5f0 to your computer and use it in GitHub Desktop.
Dispatch on a case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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