Skip to content

Instantly share code, notes, and snippets.

@tkawachi
Created September 6, 2018 02:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkawachi/9f89782d665d37b21bc983863dc03e35 to your computer and use it in GitHub Desktop.
Save tkawachi/9f89782d665d37b21bc983863dc03e35 to your computer and use it in GitHub Desktop.
package controllers
import akka.stream.SinkShape
import akka.stream.scaladsl.{Broadcast, GraphDSL, Sink}
import akka.util.ByteString
import play.api.libs.streams.Accumulator
import play.api.mvc.BodyParser
import scala.concurrent.ExecutionContext
object BodyParserFuncs {
def combine[A, B](bodyParser1: BodyParser[A], bodyParser2: BodyParser[B])(
implicit ec: ExecutionContext
): BodyParser[(A, B)] = BodyParser { rh =>
val sink1 = bodyParser1(rh).toSink
val sink2 = bodyParser2(rh).toSink
val sinkGraph = GraphDSL.create(sink1, sink2) {
case (fera, ferb) =>
for {
era <- fera
erb <- ferb
} yield {
for {
a <- era.right
b <- erb.right
} yield (a, b)
}
} { implicit builder => (s1, s2) =>
import GraphDSL.Implicits._
val bcast = builder.add(Broadcast[ByteString](2, eagerCancel = false))
bcast.out(0) ~> s1
bcast.out(1) ~> s2
SinkShape(bcast.in)
}
Accumulator(Sink.fromGraph(sinkGraph))
}
implicit class RichBP[A](bodyParser1: BodyParser[A]) {
def と[B](bodyParser2: BodyParser[B])(
implicit ec: ExecutionContext
): BodyParser[(A, B)] = combine(bodyParser1, bodyParser2)
}
}
@tkawachi
Copy link
Author

tkawachi commented Sep 6, 2018

bodyParsers.tolerantJson と bodyParsers.raw と bodyParsers.byteString 的につかう

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment