Refactor occurrences folding streams of strings

This commit is contained in:
eikek
2021-09-23 14:53:42 +02:00
parent 071f4067bf
commit 40727d205a
2 changed files with 14 additions and 29 deletions

View File

@ -14,24 +14,14 @@ import io.circe._
import io.circe.parser._
trait StreamSyntax {
implicit class StringStreamOps[F[_]](s: Stream[F, String]) {
def parseJsonAs[A](implicit d: Decoder[A], F: Sync[F]): F[Either[Throwable, A]] =
s.fold("")(_ + _)
.compile
.last
.map(optStr =>
s.compile.string
.map(str =>
for {
str <-
optStr
.map(_.trim)
.toRight(new Exception("Empty string cannot be parsed into a value"))
json <- parse(str).leftMap(_.underlying)
value <- json.as[A]
} yield value
)
}
}