Skip to content

Commit

Permalink
Remove auto derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
guymers committed Dec 28, 2024
1 parent 14ad4f6 commit 2f151db
Show file tree
Hide file tree
Showing 34 changed files with 137 additions and 155 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**foobie** is a fork of [doobie](https://github.com/tpolecat/doobie) that aims to try and keep source compatability.

Currently contains the following changes:
- `Read` and `Write` derivation is now opt-in via `import doobie.util.Read.Auto.*` and `import doobie.util.Write.Auto.*`
- There is no `Read` or `Write` auto derivation
- Java time instances are available without an explicit import
- removed Scala 2.12 support along with *IO implicits in `doobie.implicit.*` import
- PostGIS instances have been moved to the new `postgis` module and are available under `doobie.postgis.instances.{geography,geometry}`
Expand Down
5 changes: 0 additions & 5 deletions modules/core/src/main/scala-2/doobie/util/ReadPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,3 @@ trait ReadPlatform {

def derived[A]: Read[A] = macro Magnolia.gen[A]
}

trait ReadAutoPlatform extends ReadPlatform {

implicit def genRead[A]: Read[A] = macro Magnolia.gen[A]
}
5 changes: 0 additions & 5 deletions modules/core/src/main/scala-2/doobie/util/WritePlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,3 @@ trait WritePlatform {

def derived[A]: Write[A] = macro Magnolia.gen[A]
}

trait WriteAutoPlatform extends WritePlatform {

implicit def genWrite[A]: Write[A] = macro Magnolia.gen[A]
}
5 changes: 0 additions & 5 deletions modules/core/src/main/scala-3/doobie/util/ReadPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,3 @@ class ReadPlatformInstance[A](m: Mirror.ProductOf[A], typeclasses: => List[Read[
m.fromProduct(Tuple.fromArray(values))
}
}

trait ReadAutoPlatform extends ReadPlatform {

inline implicit def genRead[A](using m: Mirror.ProductOf[A]): Read[A] = derived[A]
}
5 changes: 0 additions & 5 deletions modules/core/src/main/scala-3/doobie/util/WritePlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,3 @@ class WritePlatformInstance[A](typeclasses: => List[Write[?]]) extends Write[A]
}
}
}

trait WriteAutoPlatform extends WritePlatform {

inline implicit def genWrite[A](using m: Mirror.ProductOf[A]): Write[A] = derived[A]
}
3 changes: 0 additions & 3 deletions modules/core/src/main/scala/doobie/util/read.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ This can happen for a few reasons, but the most common case is that a data
member somewhere within this type doesn't have a Get instance in scope. Here are
some debugging hints:
- For auto derivation ensure `doobie.util.Read.Auto.*` is being imported
- For Option types, ensure that a Read instance is in scope for the non-Option version.
- For types you expect to map to a single column ensure that a Get instance is in scope.
- For case classes and tuples ensure that each element has a Read instance in scope.
Expand Down Expand Up @@ -92,8 +91,6 @@ object Read extends Read1 {
F: Read[F],
): Read[(A, B, C, D, E, F)] = (A, B, C, D, E, F).tupled

object Auto extends ReadAutoPlatform

implicit val ReadApply: Apply[Read] = new Apply[Read] {
override def map[A, B](fa: Read[A])(f: A => B) = fa.map(f)
override def ap[A, B](ff: Read[A => B])(fa: Read[A]) = fa.ap(ff)
Expand Down
2 changes: 0 additions & 2 deletions modules/core/src/main/scala/doobie/util/write.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ object Write extends Write1 {
F: Write[F],
): Write[(A, B, C, D, E, F)] = (A, B, C, D, E, F).tupled

object Auto extends WriteAutoPlatform

implicit val WriteContravariantSemigroupal: ContravariantSemigroupal[Write] = new ContravariantSemigroupal[Write] {
override def contramap[A, B](fa: Write[A])(f: B => A) = fa.contramap(f)
override def product[A, B](fa: Write[A], fb: Write[B]) = fa.product(fb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ trait ReadSuitePlatform { self: ReadSuite.type =>

protected def platformTests = List(
test("Read should exist for AnyVal") {
import doobie.util.Read.Auto.*

implicit val read: Read[X] = Read.derived
assertTrue(Read[X].length == 1)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ trait WriteSuitePlatform { self: WriteSuite.type =>

protected def platformTests = List(
test("exist for AnyVal") {
import doobie.util.Write.Auto.*

implicit val write: Write[X] = Write.derived
assertTrue(Write[X].length == 1)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import zio.test.assertCompletes
import zio.test.assertTrue

object FragmentSuite extends H2DatabaseSpec {
import doobie.util.Read.Auto.*

private val a = 1
private val b = "two"
Expand Down
3 changes: 2 additions & 1 deletion modules/core/src/test/scala/doobie/util/FragmentsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import doobie.util.meta.Meta
import zio.test.assertTrue

object FragmentsSuite extends H2DatabaseSpec {
import doobie.util.Write.Auto.*
import doobie.util.fragments.*

private val nel = NonEmptyList.of(1, 2, 3)
Expand Down Expand Up @@ -126,11 +125,13 @@ object FragmentsSuite extends H2DatabaseSpec {
case class Person(name: String, age: Int)
object Person {
implicit val read: Read[Person] = Read.derived
implicit val write: Write[Person] = Write.derived
}

case class Contact(person: Person, address: Option[String])
object Contact {
implicit val read: Read[Contact] = Read.derived
implicit val write: Write[Contact] = Write.derived
}

}
1 change: 0 additions & 1 deletion modules/core/src/test/scala/doobie/util/QuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import doobie.util.query.Query0
import zio.test.assertTrue

object QuerySuite extends H2DatabaseSpec {
import doobie.util.Read.Auto.*

private val q = Query[String, Int]("select 123 where ? = 'foo'", None)
private val pairQuery = Query[String, (String, Int)]("select 'xxx', 123 where ? = 'foo'", None)
Expand Down
16 changes: 6 additions & 10 deletions modules/core/src/test/scala/doobie/util/ReadSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ object ReadSuite extends H2DatabaseSpec with ReadSuitePlatform {
case class X(x: Int) extends AnyVal

case class LenStr1(n: Int, s: String)
object LenStr1 {
implicit val read: Read[LenStr1] = Read.derived
}

case class LenStr2(n: Int, s: String)
object LenStr2 {
Expand Down Expand Up @@ -44,6 +47,9 @@ object ReadSuite extends H2DatabaseSpec with ReadSuitePlatform {
}

case class Woozle(a: (String, Int), b: (Int, String), c: Boolean)
object Woozle {
implicit val read: Read[Woozle] = Read.derived
}

override val spec = suite("Read")(
test("tuples derive") {
Expand All @@ -61,8 +67,6 @@ object ReadSuite extends H2DatabaseSpec with ReadSuitePlatform {
assertTrue(Read.derived[LenStr1].length == 2)
},
test("exist for Unit") {
import doobie.util.Read.Auto.*

assertTrue(Read[Unit].length == 0) &&
assertTrue(Read[(Int, Unit)].length == 1)
},
Expand All @@ -75,30 +79,22 @@ object ReadSuite extends H2DatabaseSpec with ReadSuitePlatform {
assertCompletes
},
test("exist for option of Unit") {
import doobie.util.Read.Auto.*

assertTrue(Read[Option[Unit]].length == 0) &&
assertTrue(Read[Option[(Int, Unit)]].length == 1)
},
test("select multi-column instance by default") {
import doobie.util.Read.Auto.*

assertTrue(Read[LenStr1].length == 2)
},
test("select 1-column instance when available") {
assertTrue(Read[LenStr2].length == 1)
},
test("exist for some fancy types") {
import doobie.util.Read.Auto.*

val _ = Read[Woozle]
val _ = Read[(Woozle, String)]
val _ = Read[(Int, (Woozle, Woozle, String))]
assertCompletes
},
test("exist for option of some fancy types") {
import doobie.util.Read.Auto.*

val _ = Read[Option[Woozle]]
val _ = Read[Option[(Woozle, String)]]
val _ = Read[Option[(Int, (Woozle, Woozle, String))]]
Expand Down
7 changes: 4 additions & 3 deletions modules/core/src/test/scala/doobie/util/UpdateSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ object UpdateSuite extends H2DatabaseSpec {
conn.transact
},
test("write") {
import doobie.util.Read.Auto.*
import doobie.util.Write.Auto.*

case class Id(v: Int)
object Id {
implicit val get: Get[Id] = Get[Int].map(apply(_))
implicit val put: Put[Id] = Put[Int].contramap(_.v)
}
val someId: Option[Id] = Some(Id(2))
val noneId: Option[Id] = None

Expand Down
37 changes: 7 additions & 30 deletions modules/core/src/test/scala/doobie/util/WriteSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ object WriteSuite extends H2DatabaseSpec with WriteSuitePlatform {
case class X(x: Int) extends AnyVal

case class LenStr1(n: Int, s: String)
object LenStr1 {
implicit val write: Write[LenStr1] = Write.derived
}

case class LenStr2(n: Int, s: String)
object LenStr2 {
Expand All @@ -34,6 +37,9 @@ object WriteSuite extends H2DatabaseSpec with WriteSuitePlatform {
}

case class Woozle(a: (String, Int), b: (Int, String), c: Boolean)
object Woozle {
implicit val write: Write[Woozle] = Write.derived
}

override val spec = suite("Write")(
test("tuples derive") {
Expand All @@ -47,8 +53,6 @@ object WriteSuite extends H2DatabaseSpec with WriteSuitePlatform {
assertTrue(Write.derived[LenStr1].length == 2)
},
test("deriving instances should work correctly from class scope") {
import doobie.util.Write.Auto.*

class Foo[A: Write, B: Write] {
locally {
val _ = Write[(A, B)]
Expand All @@ -57,8 +61,6 @@ object WriteSuite extends H2DatabaseSpec with WriteSuitePlatform {
assertCompletes
},
test("exist for Unit") {
import doobie.util.Write.Auto.*

val _ = Write[Unit]
assertTrue(Write[(Int, Unit)].length == 1)
},
Expand All @@ -70,40 +72,27 @@ object WriteSuite extends H2DatabaseSpec with WriteSuitePlatform {
val _ = Write[Option[(Int, Option[(Int, String)])]]
assertCompletes
},
test("auto derives nested types") {
import doobie.util.Write.Auto.*

assertTrue(Write[Widget].length == 3)
},
test("does not auto derive nested types without an import") {
test("does not auto derive") {
val _ = illTyped("Write.derived[Widget]")
assertCompletes
},
test("exist for option of Unit") {
import doobie.util.Write.Auto.*

assertTrue(Write[Option[Unit]].length == 0) &&
assertTrue(Write[Option[(Int, Unit)]].length == 1)
},
test("select multi-column instance by default") {
import doobie.util.Write.Auto.*

assertTrue(Write[LenStr1].length == 2)
},
test("select 1-column instance when available") {
assertTrue(Write[LenStr2].length == 1)
},
test("exist for some fancy types") {
import doobie.util.Write.Auto.*

val _ = Write[Woozle]
val _ = Write[(Woozle, String)]
val _ = Write[(Int, (Woozle, Woozle, String))]
assertCompletes
},
test("exist for option of some fancy types") {
import doobie.util.Write.Auto.*

val _ = Write[Option[Woozle]]
val _ = Write[Option[(Woozle, String)]]
val _ = Write[Option[(Int, (Woozle, Woozle, String))]]
Expand All @@ -124,19 +113,13 @@ object WriteSuite extends H2DatabaseSpec with WriteSuitePlatform {
t2 <- insert(Test(None, Some("str")))(Test.write)
t3 <- insert(Test(Some(3), Some("str")))(Test.write)

tAuto <- {
import doobie.util.Write.Auto.*
insert(Test(Some(3), Some("str")))
}

tup1 <- insert[(Option[Int], Option[String])]((None, None))(Test.writeTuple)
tup2 <- insert[(Option[Int], Option[String])]((None, Some("str")))(Test.writeTuple)
tup3 <- insert[(Option[Int], Option[String])]((Some(3), Some("str")))(Test.writeTuple)
} yield {
assertTrue(t1 == Test(None, None)) &&
assertTrue(t2 == Test(None, Some("str"))) &&
assertTrue(t3 == Test(Some(3), Some("str"))) &&
assertTrue(tAuto == Test(Some(3), Some("str"))) &&
assertTrue(tup1 == Test(None, None)) &&
assertTrue(tup2 == Test(None, Some("str"))) &&
assertTrue(tup3 == Test(Some(3), Some("str")))
Expand All @@ -159,19 +142,13 @@ object WriteSuite extends H2DatabaseSpec with WriteSuitePlatform {
t2 <- insert(Test(None, Some("str")))(Test.write)
t3 <- insert(Test(Some(3), Some("str")))(Test.write)

tAuto <- {
import doobie.util.Write.Auto.*
insert(Test(Some(3), Some("str")))
}

tup1 <- insert[(Option[Int], Option[String])]((None, None))(Test.writeTuple)
tup2 <- insert[(Option[Int], Option[String])]((None, Some("str")))(Test.writeTuple)
tup3 <- insert[(Option[Int], Option[String])]((Some(3), Some("str")))(Test.writeTuple)
} yield {
assertTrue(t1 == Test(None, None)) &&
assertTrue(t2 == Test(None, Some("str"))) &&
assertTrue(t3 == Test(Some(3), Some("str"))) &&
assertTrue(tAuto == Test(Some(3), Some("str"))) &&
assertTrue(tup1 == Test(None, None)) &&
assertTrue(tup2 == Test(None, Some("str"))) &&
assertTrue(tup3 == Test(Some(3), Some("str")))
Expand Down
11 changes: 10 additions & 1 deletion modules/docs/src/main/mdoc/docs/04-Selecting.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import doobie.syntax.connectionio.*
import doobie.syntax.stream.*
import doobie.syntax.string.*
import doobie.util.ExecutionContexts
import doobie.util.Read.Auto.*
import doobie.util.Read
import doobie.util.transactor.Transactor
import cats.*
import cats.data.*
Expand Down Expand Up @@ -139,6 +139,9 @@ Mapping rows to a case class.

```scala mdoc:silent
case class Country(code: String, name: String, pop: Int, gnp: Option[Double])
object Country {
implicit val read: Read[Country] = Read.derived
}
```

```scala mdoc
Expand All @@ -154,7 +157,13 @@ You can also nest case classes, and/or tuples arbitrarily as long as the eventua

```scala mdoc:silent
case class Code(code: String)
object Code {
implicit val read: Read[Code] = Read.derived
}
case class Country2(name: String, pop: Int, gnp: Option[Double])
object Country2 {
implicit val read: Read[Country2] = Read.derived
}
```

```scala mdoc
Expand Down
7 changes: 5 additions & 2 deletions modules/docs/src/main/mdoc/docs/05-Parameterized.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import doobie.HPS
import doobie.free.connection.ConnectionIO
import doobie.syntax.string.*
import doobie.util.ExecutionContexts
import doobie.util.Read.Auto.*
import doobie.util.Read
import doobie.util.Write
import doobie.util.Write.Auto.*
import doobie.util.transactor.Transactor
import cats.*
import cats.data.*
Expand Down Expand Up @@ -62,6 +61,10 @@ Let's set up our Country class and re-run last chapter's query just to review.

```scala mdoc:silent
case class Country(code: String, name: String, pop: Int, gnp: Option[Double])
object Country {
implicit val read: Read[Country] = Read.derived
implicit val write: Write[Country] = Write.derived
}
```

```scala mdoc
Expand Down
Loading

0 comments on commit 2f151db

Please sign in to comment.