Skip to content

Commit

Permalink
Merge pull request #96 from VirtusLab/zquery-instance
Browse files Browse the repository at this point in the history
zio-query instance
  • Loading branch information
KacperFKorban authored Apr 2, 2024
2 parents 2f3be2f + 0962090 commit 99f0fe7
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ libraryDependencies ++= Seq(
"org.virtuslab" %% "avocado" % "version from the badge",
"org.virtuslab" %% "avocado-cats" % "version from the badge", // for Cats
"org.virtuslab" %% "avocado-zio-2" % "version from the badge", // for ZIO 2.x
"org.virtuslab" %% "avocado-zio-1" % "version from the badge", // for ZIO 1.x
"org.virtuslab" %% "avocado-zio-1" % "version from the badge", // for ZIO 1.x,
"org.virtuslab" %% "avocado-zio-query" % "version from the badge", // for ZIO Query
)
```

Expand All @@ -55,6 +56,7 @@ libraryDependencies ++= Seq(
//> using lib "org.virtuslab::avocado-cats:version from the badge" // for Cats
//> using lib "org.virtuslab::avocado-zio-2:version from the badge" // for ZIO 2.x
//> using lib "org.virtuslab::avocado-zio-1:version from the badge" // for ZIO 1.x
//> using lib "org.virtuslab::avocado-zio-query:version from the badge" // for ZIO Query
```

## Usage (in code)
Expand All @@ -66,6 +68,7 @@ import avocado.* // This line exposes the `ado` function - entrypoint of the lib
import avocado.instances.cats.given
import avocado.instances.zio2.given
import avocado.instances.zio1.given
import avocado.instances.zioquery.given
```

And that's it! All that's left is to wrap the `for`-comprehensions that you want to parallelize with a call to `ado` an watch your program run in parallel! Like so:
Expand Down
32 changes: 24 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val scala3 = "3.2.2"
val scala3 = "3.3.3"

Global / concurrentRestrictions += Tags.limit(Tags.All, 1)

Expand All @@ -25,7 +25,7 @@ val commonSettings = Seq(
"-feature"
),
libraryDependencies ++= Seq(
"org.scalameta" %%% "munit" % "1.0.0-M6" % Test
"org.scalameta" %%% "munit" % "1.0.0-M11" % Test
)
)

Expand All @@ -41,6 +41,7 @@ lazy val root = project
++ cats.projectRefs
++ zio2.projectRefs
++ zio1.projectRefs
++ zioquery.projectRefs
)*)

lazy val avocado = projectMatrix
Expand All @@ -62,8 +63,8 @@ lazy val cats = projectMatrix
.settings(
name := "avocADO-cats",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-core" % "2.9.0",
"org.typelevel" %%% "cats-effect" % "3.4.8" % Test
"org.typelevel" %%% "cats-core" % "2.10.0",
"org.typelevel" %%% "cats-effect" % "3.5.4" % Test
)
)
.dependsOn(avocado)
Expand All @@ -77,25 +78,40 @@ lazy val zio2 = projectMatrix
.settings(
name := "avocADO-zio-2",
libraryDependencies ++= Seq(
"dev.zio" %%% "zio" % "2.0.2"
)
"dev.zio" %%% "zio" % "2.0.21"
),
scalacOptions := scalacOptions.value.filterNot(_ == "-Xcheck-macros")
)
.dependsOn(avocado)
.jvmPlatform(scalaVersions = List(scala3))
.jsPlatform(scalaVersions = Seq(scala3))
.nativePlatform(scalaVersions = Seq(scala3))


lazy val zio1 = projectMatrix
.in(file("zio-1"))
.settings(commonSettings)
.settings(
name := "avocADO-zio-1",
libraryDependencies ++= Seq(
"dev.zio" %%% "zio" % "1.0.17"
"dev.zio" %%% "zio" % "1.0.18"
)
)
.dependsOn(avocado)
.jvmPlatform(scalaVersions = List(scala3))
.jsPlatform(scalaVersions = Seq(scala3))
.nativePlatform(scalaVersions = Seq(scala3))

lazy val zioquery = projectMatrix
.in(file("zio-query"))
.settings(commonSettings)
.settings(
name := "avocADO-zio-query",
libraryDependencies ++= Seq(
"dev.zio" %% "zio-query" % "0.6.1"
),
scalacOptions := scalacOptions.value.filterNot(_ == "-Xcheck-macros")
)
.dependsOn(avocado)
.jvmPlatform(scalaVersions = List(scala3))
.jsPlatform(scalaVersions = Seq(scala3))
.nativePlatform(scalaVersions = Seq(scala3))
3 changes: 3 additions & 0 deletions docs/_docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ libraryDependencies ++= Seq(
"org.virtuslab" %% "avocado-cats" % "version from the badge", // for Cats
"org.virtuslab" %% "avocado-zio-2" % "version from the badge", // for ZIO 2.x
"org.virtuslab" %% "avocado-zio-1" % "version from the badge", // for ZIO 1.x
"org.virtuslab" %% "avocado-zio-query" % "version from the badge", // for ZIO Query
)
```

Expand All @@ -53,6 +54,7 @@ libraryDependencies ++= Seq(
//> using lib "org.virtuslab::avocado-cats:version from the badge" // for Cats
//> using lib "org.virtuslab::avocado-zio-2:version from the badge" // for ZIO 2.x
//> using lib "org.virtuslab::avocado-zio-1:version from the badge" // for ZIO 1.x
//> using lib "org.virtuslab::avocado-zio-query:version from the badge" // for ZIO Query
```

## Usage (in code)
Expand All @@ -64,6 +66,7 @@ import avocado.* // This line exposes the `ado` function - entrypoint of the lib
import avocado.instances.cats.given
import avocado.instances.zio2.given
import avocado.instances.zio1.given
import avocado.instances.zioquery.given
```

And that's it! All that's left is to wrap the `for`-comprehensions that you want to parallelize with a call to `ado` an watch your program run in parallel! Like so:
Expand Down
8 changes: 4 additions & 4 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.2")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.12")
addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.9.0")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.17")
addSbtPlugin("com.eed3si9n" % "sbt-projectmatrix" % "0.10.0")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")
13 changes: 13 additions & 0 deletions zio-query/src/main/scala/zioquery.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package avocado.instances

import avocado.AvocADO
import zio.query.ZQuery

object zioquery {
given [E, R]: AvocADO[[X] =>> ZQuery[R, E, X]] = new AvocADO[[X] =>> ZQuery[R, E, X]] {
def pure[A](a: A): ZQuery[R, E, A] = ZQuery.succeed(a)
def map[A, B](fa: ZQuery[R, E, A], f: A => B): ZQuery[R, E, B] = fa.map(f)
def zip[A, B](fa: ZQuery[R, E, A], fb: ZQuery[R, E, B]): ZQuery[R, E, (A, B)] = fa.zipPar(fb)
def flatMap[A, B](fa: ZQuery[R, E, A], f: A => ZQuery[R, E, B]): ZQuery[R, E, B] = fa.flatMap(f)
}
}
21 changes: 21 additions & 0 deletions zio-query/src/test/scalajvm/BaseZQueryTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package avocado.tests

import zio.*
import zio.query.*

abstract class BaseZQueryTest extends munit.FunSuite {

def testWithTimeLimit[R, E, A](name: String, maxMillis: Long)(body: ZQuery[R, E, A])(expected: Either[E, A]): Unit = {
test(name) {
val start = java.lang.System.currentTimeMillis()
val res: Either[E, A] = Unsafe.unsafe { unsafe ?=>
zio.Runtime.default.unsafe.run(
body.either.run.asInstanceOf[ZIO[Any, Any, Either[E, A]]]
).getOrThrowFiberFailure()
}
val end = java.lang.System.currentTimeMillis()
assertEquals(res, expected)
assert(end - start < maxMillis)
}
}
}
68 changes: 68 additions & 0 deletions zio-query/src/test/scalajvm/ZQueryTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package avocado.tests

import avocado.*
import avocado.instances.zioquery.given
import scala.concurrent.duration.*
import zio.*
import zio.query.ZQuery

class ZQueryTests extends BaseZQueryTest {

testWithTimeLimit("ZQuery comprehension 1", 4000) {
val wait = ZQuery.fromZIO(ZIO.sleep(1000.millis))
ado {
for {
a <- wait.map(_ => 1)
} yield a
}
}(Right(1))

testWithTimeLimit("ZQuery comprehension 2", 900) {
val wait = ZQuery.fromZIO(ZIO.sleep(500.millis))
ado {
for {
a <- wait.map(_ => 1)
b <- wait.map(_ => 2)
} yield a + b
}
}(Right(3))

testWithTimeLimit("ZQuery comprehension 3", 1400) {
val wait = ZQuery.fromZIO(ZIO.sleep(500.millis))
ado {
for {
a <- wait.map(_ => 1)
b <- wait.map(_ => 2)
c <- ZQuery.fromZIO(ZIO.succeed(a + b))
d <- wait.map(_ => 4)
} yield c + d
}
}(Right(7))

testWithTimeLimit("ZQuery comprehension 4", 1400) {
val wait = ZQuery.fromZIO(ZIO.sleep(500.millis))
ado {
for {
a <- wait.map(_ => 1)
b <- wait.map(_ => 2)
c <- ZQuery.fromZIO(ZIO.succeed(a + b))
d <- wait.map(_ => 4)
e <- wait.map(Function.const(5))
} yield c + d
}
}(Right(7))

testWithTimeLimit("ZQuery comprehension 5", 1400) {
val wait = ZQuery.fromZIO(ZIO.sleep(500.millis))
ado {
for {
a <- wait.map(_ => 1)
b <- wait.map(_ => 2)
c <- ZQuery.fromZIO(ZIO.succeed(a + b))
d <- wait.map(_ => 4)
e <- wait.map(Function.const(5))
} yield c + d
}
}(Right(7))

}

0 comments on commit 99f0fe7

Please sign in to comment.