Skip to content

Commit

Permalink
Update README to show new CompletedRequestMap constructors (#472)
Browse files Browse the repository at this point in the history
* Update doco with new CompletedRequestMap constructors

* Update doco and improve `fromIterableWith`
  • Loading branch information
kyri-petrou authored Apr 11, 2024
1 parent d921775 commit edc9414
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
12 changes: 5 additions & 7 deletions docs/creating-data-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,20 @@ val identifier: String = "UserDataSource"
We will define two different behaviors depending on whether we receive a single request or multiple requests at once. For each request, we need to insert into the result map a value of type `Exit` (`fail` for an error and `succeed` for a success).

```scala mdoc:silent
def run(requests: Chunk[GetUserName]): ZIO[Any, Nothing, CompletedRequestMap] = {
val resultMap = CompletedRequestMap.empty
def run(requests: Chunk[GetUserName]): ZIO[Any, Nothing, CompletedRequestMap] =
requests.toList match {
case request :: Nil =>
// get user by ID e.g. SELECT name FROM users WHERE id = $id
val result: Task[String] = ???
result.exit.map(resultMap.insert(request, _))
result.exit.map(CompletedRequestMap.single(request, _))
case batch =>
// get multiple users at once e.g. SELECT id, name FROM users WHERE id IN ($ids)
val result: Task[List[(Int, String)]] = ???
result.fold(
err => requests.foldLeft(resultMap) { case (map, req) => map.insert(req, Exit.fail(err)) },
_.foldLeft(resultMap) { case (map, (id, name)) => map.insert(GetUserName(id), Exit.succeed(name)) }
result.foldCause(
CompletedRequestMap.failCause(requests, _),
CompletedRequestMap.fromIterableWith(_)(kv => GetUserName(kv._1), kv => Exit.succeed(kv._2))
)
}
}
```

Now to build a `ZQuery`, we can use `ZQuery.fromRequest` and just pass the request and the data source:
Expand Down
18 changes: 5 additions & 13 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,27 @@ object ZQueryExample extends ZIOAppDefault {
new DataSource.Batched[Any, GetUserName] {
val identifier: String = "UserDataSource"

def run(requests: Chunk[GetUserName])(implicit trace: Trace): ZIO[Any, Nothing, CompletedRequestMap] = {
val resultMap = CompletedRequestMap.empty
def run(requests: Chunk[GetUserName])(implicit trace: Trace): ZIO[Any, Nothing, CompletedRequestMap] =
requests.toList match {
case request :: Nil =>
val result: Task[String] = {
// get user by ID e.g. SELECT name FROM users WHERE id = $id
ZIO.succeed(???)
}

result.exit.map(resultMap.insert(request, _))
result.exit.map(CompletedRequestMap.single(request, _))

case batch: Seq[GetUserName] =>
val result: Task[List[(Int, String)]] = {
// get multiple users at once e.g. SELECT id, name FROM users WHERE id IN ($ids)
ZIO.succeed(???)
}

result.fold(
err =>
requests.foldLeft(resultMap) { case (map, req) =>
map.insert(req, Exit.fail(err))
},
_.foldLeft(resultMap) { case (map, (id, name)) =>
map.insert(GetUserName(id), Exit.succeed(name))
}
result.foldCause(
CompletedRequestMap.failCause(requests, _),
CompletedRequestMap.fromIterableWith(_)(kv => GetUserName(kv._1), kv => Exit.succeed(kv._2))
)
}
}

}

def getUserNameById(id: Int): ZQuery[Any, Throwable, String] =
Expand Down
32 changes: 20 additions & 12 deletions zio-query/shared/src/main/scala/zio/query/CompletedRequestMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,6 @@ object CompletedRequestMap {
def fromIterable[E, A](iterable: Iterable[(Request[E, A], Exit[E, A])]): CompletedRequestMap =
Mutable(mutable.HashMap.from(iterable))

/**
* Constructs a completed requests map an iterable of requests and a function
* that maps each request to a result
*/
def fromIterableWith[E, A, B](
iterable: Iterable[A]
)(f: A => Exit[E, B])(implicit ev: A <:< Request[E, B]): CompletedRequestMap = {
val map = Mutable.empty(iterable.size)
iterable.foreach(req => map.update(req, f(req)))
map
}

/**
* Constructs a completed requests map from the specified optional results.
*/
Expand All @@ -195,6 +183,26 @@ object CompletedRequestMap {
map
}

/**
* Constructs a completed requests map an iterable of A and functions that map
* each A to a request and a result
*
* @param f1
* function that maps each element of A to a request
* @param f2
* function that maps each element of A to an Exit, e.g., Exit.succeed(_)
*/
def fromIterableWith[E, A, B](
iterable: Iterable[A]
)(
f1: A => Request[E, B],
f2: A => Exit[E, B]
): CompletedRequestMap = {
val map = Mutable.empty(iterable.size)
iterable.foreach(req => map.update(f1(req), f2(req)))
map
}

/**
* Constructs a completed requests map containing a single entry
*/
Expand Down
2 changes: 1 addition & 1 deletion zio-query/shared/src/main/scala/zio/query/DataSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ object DataSource {
new DataSource.Batched[Any, A] {
val identifier: String = name
def run(requests: Chunk[A])(implicit trace: Trace): ZIO[Any, Nothing, CompletedRequestMap] =
ZIO.succeed(CompletedRequestMap.fromIterableWith(requests)(a => Exit.succeed(f(a))))
ZIO.succeed(CompletedRequestMap.fromIterableWith(requests)(ev.apply, a => Exit.succeed(f(a))))
}

/**
Expand Down

0 comments on commit edc9414

Please sign in to comment.