mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Refactor occurrences folding streams of strings
This commit is contained in:
@ -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
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user