-
Notifications
You must be signed in to change notification settings - Fork 221
/
AccumulatingDecoders.scala
51 lines (43 loc) · 1.65 KB
/
AccumulatingDecoders.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package io.finch.circe
import java.nio.charset.StandardCharsets
import cats.MonadError
import cats.data.Validated
import io.circe._
import io.circe.iteratee._
import io.circe.jawn.{decodeAccumulating, decodeByteBufferAccumulating}
import io.finch.internal.HttpContent
import io.finch.{Application, Decode, DecodeStream}
import io.iteratee.{Enumeratee, Enumerator}
trait AccumulatingDecoders {
/**
* Maps a Circe's [[Decoder]] to Finch's [[Decode]].
*/
implicit def decodeCirce[A: Decoder]: Decode.Json[A] = Decode.json { (b, cs) =>
val attemptJson = cs match {
case StandardCharsets.UTF_8 =>
decodeByteBufferAccumulating[A](b.asByteBuffer)
case _ => decodeAccumulating[A](b.asString(cs))
}
attemptJson.fold[Either[Throwable, A]](nel => Left(Errors(nel)), Right.apply)
}
implicit def iterateeCirceDecoder[F[_], A: Decoder](implicit
monadError: MonadError[F, Throwable]
): DecodeStream.Json[Enumerator, F, A] = DecodeStream.instance[Enumerator, F, A, Application.Json] { (enum, cs) =>
val parsed = cs match {
case StandardCharsets.UTF_8 =>
enum.map(_.asByteArray).through(byteStreamParser[F])
case _ =>
enum.map(_.asString(cs)).through(stringStreamParser[F])
}
parsed.through(decoderAccumulating[F, A])
}
private def decoderAccumulating[F[_], A](implicit
F: MonadError[F, Throwable],
decode: Decoder[A]
): Enumeratee[F, Json, A] = Enumeratee.flatMap(json =>
decode.decodeAccumulating(json.hcursor) match {
case Validated.Invalid(errors) => Enumerator.liftM(F.raiseError(Errors(errors)))
case Validated.Valid(a) => Enumerator.enumOne(a)
}
)
}