From 53393f8ad8765d4782d566009439ae2bf600c27d Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Tue, 2 Apr 2024 11:44:15 +0200 Subject: [PATCH 1/2] Add zio-query instance and bump some dependencies --- README.md | 5 +- build.sbt | 22 +++++- docs/_docs/index.md | 3 + project/plugins.sbt | 8 +-- zio-query/src/main/scala/zioquery.scala | 13 ++++ .../src/test/scalajvm/BaseZQueryTest.scala | 21 ++++++ zio-query/src/test/scalajvm/ZQueryTests.scala | 68 +++++++++++++++++++ 7 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 zio-query/src/main/scala/zioquery.scala create mode 100644 zio-query/src/test/scalajvm/BaseZQueryTest.scala create mode 100644 zio-query/src/test/scalajvm/ZQueryTests.scala diff --git a/README.md b/README.md index 534f7ed..b6fac47 100644 --- a/README.md +++ b/README.md @@ -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 ) ``` @@ -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) @@ -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: diff --git a/build.sbt b/build.sbt index cd5aac9..b490243 100644 --- a/build.sbt +++ b/build.sbt @@ -1,4 +1,4 @@ -val scala3 = "3.2.2" +val scala3 = "3.3.1" Global / concurrentRestrictions += Tags.limit(Tags.All, 1) @@ -41,6 +41,7 @@ lazy val root = project ++ cats.projectRefs ++ zio2.projectRefs ++ zio1.projectRefs + ++ zioquery.projectRefs )*) lazy val avocado = projectMatrix @@ -78,14 +79,14 @@ lazy val zio2 = projectMatrix name := "avocADO-zio-2", libraryDependencies ++= Seq( "dev.zio" %%% "zio" % "2.0.2" - ) + ), + 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) @@ -99,3 +100,18 @@ lazy val zio1 = projectMatrix .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)) diff --git a/docs/_docs/index.md b/docs/_docs/index.md index aec0039..d86e75e 100644 --- a/docs/_docs/index.md +++ b/docs/_docs/index.md @@ -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 ) ``` @@ -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) @@ -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: diff --git a/project/plugins.sbt b/project/plugins.sbt index f2af332..fb78cbb 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -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") diff --git a/zio-query/src/main/scala/zioquery.scala b/zio-query/src/main/scala/zioquery.scala new file mode 100644 index 0000000..b0589d0 --- /dev/null +++ b/zio-query/src/main/scala/zioquery.scala @@ -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) + } +} diff --git a/zio-query/src/test/scalajvm/BaseZQueryTest.scala b/zio-query/src/test/scalajvm/BaseZQueryTest.scala new file mode 100644 index 0000000..8313a65 --- /dev/null +++ b/zio-query/src/test/scalajvm/BaseZQueryTest.scala @@ -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) + } + } +} diff --git a/zio-query/src/test/scalajvm/ZQueryTests.scala b/zio-query/src/test/scalajvm/ZQueryTests.scala new file mode 100644 index 0000000..67ac924 --- /dev/null +++ b/zio-query/src/test/scalajvm/ZQueryTests.scala @@ -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)) + +} From 09620905d0c1396af4ea118199da4c7d3c31e9ec Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Tue, 2 Apr 2024 11:53:55 +0200 Subject: [PATCH 2/2] Bump some more dependencies --- build.sbt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/build.sbt b/build.sbt index b490243..e9e4196 100644 --- a/build.sbt +++ b/build.sbt @@ -1,4 +1,4 @@ -val scala3 = "3.3.1" +val scala3 = "3.3.3" Global / concurrentRestrictions += Tags.limit(Tags.All, 1) @@ -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 ) ) @@ -63,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) @@ -78,7 +78,7 @@ 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") ) @@ -93,7 +93,7 @@ lazy val zio1 = projectMatrix .settings( name := "avocADO-zio-1", libraryDependencies ++= Seq( - "dev.zio" %%% "zio" % "1.0.17" + "dev.zio" %%% "zio" % "1.0.18" ) ) .dependsOn(avocado)