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

Scala 3 support #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
28 changes: 8 additions & 20 deletions README.md/README.md → README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SlickCats
=========
==========

[Cats](https://github.com/typelevel/cats) instances for [Slick's](http://slick.typesafe.com/) `DBIO` including:
* Monad
Expand All @@ -14,9 +14,7 @@ SlickCats
* Equals

## Using

To add *slick-cats* dependency to a project, add the following to your build definition:

```scala
libraryDependencies += "com.rms.miu" %% "slick-cats" % version
```
Expand All @@ -25,6 +23,7 @@ Because of possible binary incompatibilities, here are the dependency versions u

| slick-cats version | slick version | cats version |
|:------------------:|:-------------:|:------------:|
| 0.10.6 | 3.5.0 | 2.10.0 |
| 0.10.5 | 3.4.1 | 2.9.0 |
| 0.10.4 | 3.3.3 | 2.3.1 |
| 0.10.3 | 3.3.2 | 2.2.0 |
Expand All @@ -34,18 +33,14 @@ Because of possible binary incompatibilities, here are the dependency versions u
Artifacts are publicly available on Maven Central starting from version *0.6*.

## Accessing the Instances

Some or all of the following imports may be needed:

```scala
import cats._
import slick.dbio._
import com.rms.miu.slickcats.DBIOInstances._
```

Additionally, be sure to have an implicit `ExecutionContext` in scope. The implicit conversions require it
and will fail with non-obvious errors if it's missing.

```scala
implicitly[Monad[DBIO]]
// error: could not find implicit value for parameter e: cats.Monad[slick.dbio.DBIO]
Expand All @@ -58,7 +53,6 @@ import scala.concurrent.ExecutionContext.Implicits.global
```

instances will be available for:

```scala
implicitly[Monad[DBIO]]
implicitly[MonadError[DBIO, Throwable]]
Expand All @@ -68,31 +62,29 @@ implicitly[Applicative[DBIO]]
```

If a Monoid exists for `A`, here taken as Int, then the following is also available

```scala
implicitly[Group[DBIO[Int]]]
implicitly[Semigroup[DBIO[Int]]]
implicitly[Monoid[DBIO[Int]]]
```

## Known Issues

Instances are supplied for `DBIO[A]` only. Despite being the same thing,
type aliases will not match for implicit conversion. This means that the following

```scala
def monad[F[_] : Monad, A](fa: F[A]): F[A] = fa

val fail1: DBIOAction[String, NoStream, Effect.All] = DBIO.successful("hello")
// fail1: DBIOAction[String, NoStream, Effect.All] = SuccessAction("hello")
// fail1: DBIOAction[String, NoStream, Effect.All] = SuccessAction(
// value = "hello"
// )
val fail2 = DBIO.successful("hello")
// fail2: DBIOAction[String, NoStream, Effect] = SuccessAction("hello")
// fail2: DBIOAction[String, NoStream, Effect] = SuccessAction(value = "hello")
val success: DBIO[String] = DBIO.successful("hello")
// success: DBIO[String] = SuccessAction("hello")
// success: DBIO[String] = SuccessAction(value = "hello")
```

will _not_ compile

```scala
monad(fail1)
monad(fail2)
Expand All @@ -117,16 +109,12 @@ monad(fail2)
// monad(fail2)
// ^^^^^
```

but

```scala
monad(success)
// res10: DBIO[String] = SuccessAction("hello")
// res10: DBIO[String] = SuccessAction(value = "hello")
```

will compile fine.

## Extras

This README is compiled using [mdoc](https://scalameta.org/mdoc/) to ensure that only working examples are given.
39 changes: 15 additions & 24 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,18 @@ name := "slick-cats-parent"
sourcesInBase := false
publish / skip := true

val scala212 = "2.12.18"
val scala213 = "2.13.12"
val scala3 = "3.4.0"

val commonSettings = Seq(
organization := "com.rms.miu",

scalaVersion := "2.13.10",
crossScalaVersions := Seq("2.12.17","2.13.10"),

scalacOptions ++= Seq(
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-language:implicitConversions",
"-language:higherKinds",
"-unchecked",
"-Xfatal-warnings",
"-Xlint",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen",
"-Ywarn-value-discard"
)
scalaVersion := scala213,
crossScalaVersions := Seq(scala212, scala213, scala3)
)

val catsVersion = "2.9.0"
val catsVersion = "2.10.0"

lazy val slickcats =
project.in(file("slick-cats"))
Expand All @@ -33,11 +23,11 @@ lazy val slickcats =
name := "slick-cats",
description := "Cats instances for Slick's DBIO",
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.4.1",
"com.typesafe.slick" %% "slick" % "3.5.0",
"org.typelevel" %% "cats-core" % catsVersion,
"org.typelevel" %% "cats-laws" % catsVersion % Test,
"org.typelevel" %% "discipline-scalatest" % "2.2.0" % Test,
"org.scalatest" %% "scalatest" % "3.2.14" % Test,
"org.scalatest" %% "scalatest" % "3.2.17" % Test,
"org.scalacheck" %% "scalacheck" % "1.17.0" % Test
)
)
Expand All @@ -49,7 +39,7 @@ lazy val docs =
.settings(commonSettings)
.settings(
name := "slick-cats-docs",
mdoc / scalacOptions --= Seq("-Ywarn-unused-import", "-Xlint"),
mdocOut := baseDirectory.value / "..",
publish / skip := true
)

Expand All @@ -68,9 +58,10 @@ developers := List(
)

publishMavenStyle := true
publishTo := Some(
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Opts.resolver.sonatypeOssSnapshots
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Opts.resolver.sonatypeStaging
)
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Because of possible binary incompatibilities, here are the dependency versions u

| slick-cats version | slick version | cats version |
|:------------------:|:-------------:|:------------:|
| 0.10.6 | 3.5.0 | 2.10.0 |
| 0.10.5 | 3.4.1 | 2.9.0 |
| 0.10.4 | 3.3.3 | 2.3.1 |
| 0.10.3 | 3.3.2 | 2.2.0 |
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.8.0
sbt.version=1.9.8
7 changes: 4 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "1.3.6")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.17")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.5.1")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.10.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.2.1")
addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.4.19")
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,22 @@ private[slickcats] class DBIOGroup[A](implicit A: Group[A], ec: ExecutionContext
}

private[slickcats] sealed trait DBIOInstances0 extends DBIOInstances1 {
def dbioComonad(atMost: FiniteDuration, db: BasicBackend#DatabaseDef)(implicit ec: ExecutionContext): Comonad[DBIO] =
def dbioComonad(atMost: FiniteDuration, db: BasicBackend#BasicDatabaseDef)(implicit ec: ExecutionContext): Comonad[DBIO] =
new DBIOCoflatMap with Comonad[DBIO] {
def extract[A](x: DBIO[A]): A =
Await.result(db.run(x), atMost)
}

def dbioOrder[A: Order](atMost: FiniteDuration, db: BasicBackend#DatabaseDef)(implicit ec: ExecutionContext): Order[DBIO[A]] =
def dbioOrder[A: Order](atMost: FiniteDuration, db: BasicBackend#BasicDatabaseDef)(implicit ec: ExecutionContext): Order[DBIO[A]] =
(x: DBIO[A], y: DBIO[A]) => Await.result(db.run((x zip y).map { case (a, b) => a compare b }), atMost)
}

private[slickcats] sealed trait DBIOInstances1 extends DBIOInstances2 {
def dbioPartialOrder[A: PartialOrder](atMost: FiniteDuration, db: BasicBackend#DatabaseDef)(implicit ec: ExecutionContext): PartialOrder[DBIO[A]] =
def dbioPartialOrder[A: PartialOrder](atMost: FiniteDuration, db: BasicBackend#BasicDatabaseDef)(implicit ec: ExecutionContext): PartialOrder[DBIO[A]] =
(x: DBIO[A], y: DBIO[A]) => Await.result(db.run((x zip y).map { case (a, b) => a partialCompare b }), atMost)
}

private[slickcats] sealed trait DBIOInstances2 {
def dbioEq[A: Eq](atMost: FiniteDuration, db: BasicBackend#DatabaseDef)(implicit ec: ExecutionContext): Eq[DBIO[A]] =
def dbioEq[A: Eq](atMost: FiniteDuration, db: BasicBackend#BasicDatabaseDef)(implicit ec: ExecutionContext): Eq[DBIO[A]] =
(x: DBIO[A], y: DBIO[A]) => Await.result(db.run((x zip y).map { case (a, b) => a === b }), atMost)
}
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "0.10.4"
ThisBuild / version := "0.10.6"