Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Errors in More Data Source Constructors #194

Merged
merged 1 commit into from
Feb 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions core/src/main/scala/zquery/DataSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,18 @@ object DataSource {
*/
def fromFunctionBatchedM[R, E, A, B](
name: String
)(f: Iterable[A] => ZIO[R, Nothing, Iterable[B]])(implicit ev: A <:< Request[E, B]): DataSource[R, A] =
)(f: Iterable[A] => ZIO[R, E, Iterable[B]])(implicit ev: A <:< Request[E, B]): DataSource[R, A] =
new DataSource[R, A] {
val identifier = name
def run(requests: Iterable[A]): ZIO[R, Nothing, CompletedRequestMap] =
f(requests).map { results =>
(requests zip results).foldLeft(CompletedRequestMap.empty) {
case (map, (k, v)) => map.insert(k)(Right(v))
}
}
f(requests)
.fold(
failure => requests.map((_, Left(failure))),
results => requests.zip(results.map(Right(_)))
)
.map(_.foldLeft(CompletedRequestMap.empty) {
case (map, (k, v)) => map.insert(k)(v)
})
}

/**
Expand Down Expand Up @@ -201,12 +204,12 @@ object DataSource {
*/
def fromFunctionM[R, E, A, B](
name: String
)(f: A => ZIO[R, Nothing, B])(implicit ev: A <:< Request[E, B]): DataSource[R, A] =
)(f: A => ZIO[R, E, B])(implicit ev: A <:< Request[E, B]): DataSource[R, A] =
new DataSource[R, A] {
val identifier = name
def run(requests: Iterable[A]): ZIO[R, Nothing, CompletedRequestMap] =
ZIO
.foreachPar(requests)(k => ZIO.succeed(k).zip(f(k)))
.map(_.foldLeft(CompletedRequestMap.empty) { case (map, (k, v)) => map.insert(k)(Right(v)) })
.foreachPar(requests)(k => ZIO.succeed(k).zip(f(k).either))
.map(_.foldLeft(CompletedRequestMap.empty) { case (map, (k, v)) => map.insert(k)(v) })
}
}