-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #695 from finagle/vk/body-with-ct
Add new body endpoints
- Loading branch information
Showing
14 changed files
with
215 additions
and
63 deletions.
There are no files selected for viewing
29 changes: 23 additions & 6 deletions
29
benchmarks/src/main/scala/io/finch/benchmarks/BodyBenchmark.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,46 @@ | ||
package io.finch.benchmarks | ||
|
||
import com.twitter.io.Buf | ||
import com.twitter.util.Return | ||
import io.finch._ | ||
import io.finch.internal.BufText | ||
import org.openjdk.jmh.annotations.{Benchmark, Scope, State} | ||
|
||
@State(Scope.Benchmark) | ||
class BodyBenchmark extends FinchBenchmark { | ||
|
||
implicit val decodeJsonAsString: Decode.Json[String] = | ||
Decode.json((b, cs) => Return(BufText.extract(b, cs))) | ||
|
||
val input = Input.post("/").withBody[Text.Plain](Buf.Utf8("x" * 1024)) | ||
|
||
val bodyAsString = body.as[String] | ||
val bodyOptionAsString = bodyOption.as[String] | ||
|
||
val bodyAsString2 = body[String, Application.Json] | ||
val bodyOptionAsString2 = bodyOption[String, Application.Json] | ||
|
||
@Benchmark | ||
def jsonOption: Option[String] = bodyOptionAsString(input).value.get | ||
|
||
@Benchmark | ||
def json: String = bodyAsString(input).value.get | ||
|
||
@Benchmark | ||
def bufOption: Option[Buf] = bodyOption(input).value.get | ||
def jsonOption2: Option[String] = bodyOptionAsString2(input).value.get | ||
|
||
@Benchmark | ||
def buf: Buf = body(input).value.get | ||
def json2: String = bodyAsString2(input).value.get | ||
|
||
@Benchmark | ||
def stringOption: Option[String] = bodyStringOption(input).value.get | ||
def stringOption: Option[String] = stringBodyOption(input).value.get | ||
|
||
@Benchmark | ||
def string: String = bodyString(input).value.get | ||
def string: String = stringBody(input).value.get | ||
|
||
@Benchmark | ||
def byteArrayOption: Option[Array[Byte]] = bodyByteArrayOption(input).value.get | ||
def byteArrayOption: Option[Array[Byte]] = binaryBodyOption(input).value.get | ||
|
||
@Benchmark | ||
def byteArray: Array[Byte] = bodyByteArray(input).value.get | ||
def byteArray: Array[Byte] = binaryBody(input).value.get | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.finch.endpoint | ||
|
||
import com.twitter.util.{Future, Return, Throw} | ||
import io.catbird.util.Rerunnable | ||
import io.finch._ | ||
import io.finch.internal._ | ||
import io.finch.items._ | ||
import scala.reflect.ClassTag | ||
|
||
private[finch] abstract class Body[A, B, CT <: String]( | ||
d: Decode.Aux[A, CT], ct: ClassTag[A]) extends Endpoint[B] { | ||
|
||
protected def whenNotPresent: Rerunnable[Output[B]] | ||
protected def prepare(a: A): B | ||
|
||
private[this] def decode(i: Input): Future[Output[B]] = | ||
d(i.request.content, i.request.charsetOrUtf8) match { | ||
case Return(r) => Future.value(Output.payload(prepare(r))) | ||
case Throw(t) => Future.exception(Error.NotParsed(items.BodyItem, ct, t)) | ||
} | ||
|
||
final def apply(input: Input): Endpoint.Result[B] = | ||
if (input.request.isChunked) None | ||
else { | ||
val rr = input.request.contentLength match { | ||
case None => whenNotPresent | ||
case _ => Rerunnable.fromFuture(decode(input)) | ||
} | ||
|
||
Some(input -> rr) | ||
} | ||
|
||
override def item: RequestItem = items.BodyItem | ||
override def toString: String = "body" | ||
} | ||
|
||
private[finch] final class RequiredBody[A, CT <: String]( | ||
d: Decode.Aux[A, CT], ct: ClassTag[A]) extends Body[A, A, CT](d, ct) { | ||
|
||
protected def prepare(a: A): A = a | ||
protected def whenNotPresent: Rerunnable[Output[A]] = | ||
Rs.BodyNotPresent.asInstanceOf[Rerunnable[Output[A]]] | ||
} | ||
|
||
private[finch] final class OptionalBody[A, CT <: String]( | ||
d: Decode.Aux[A, CT], ct: ClassTag[A]) extends Body[A, Option[A], CT](d, ct) { | ||
|
||
protected def prepare(a: A): Option[A] = Some(a) | ||
protected def whenNotPresent: Rerunnable[Output[Option[A]]] = | ||
Rs.OutputNone.asInstanceOf[Rerunnable[Output[Option[A]]]] | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.finch.internal | ||
|
||
import com.twitter.util.Future | ||
import io.catbird.util.Rerunnable | ||
import io.finch._ | ||
import io.finch.items._ | ||
import shapeless.HNil | ||
|
||
/** | ||
* Predefined, Finch-specific instances of [[Rerunnable]]. | ||
*/ | ||
private[finch] object Rs { | ||
// See https://github.com/travisbrown/catbird/pull/32 | ||
final def const[A](a: A): Rerunnable[A] = new Rerunnable[A] { | ||
override def run: Future[A] = Future.value(a) | ||
} | ||
|
||
final val OutputNone: Rerunnable[Output[Option[Nothing]]] = | ||
new Rerunnable[Output[Option[Nothing]]] { | ||
override val run: Future[Output[Option[Nothing]]] = | ||
Future.value(Output.None) | ||
} | ||
|
||
final val BodyNotPresent: Rerunnable[Nothing] = new Rerunnable[Nothing] { | ||
override val run: Future[Nothing] = | ||
Future.exception(Error.NotPresent(BodyItem)) | ||
} | ||
|
||
final val OutputHNil: Rerunnable[Output[HNil]] = | ||
new Rerunnable[Output[HNil]] { | ||
override val run: Future[Output[HNil]] = Future.value(Output.payload(HNil)) | ||
} | ||
} |
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
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
Oops, something went wrong.