Skip to content

Commit

Permalink
fix auto-derivation using custom instances
Browse files Browse the repository at this point in the history
  • Loading branch information
jatcwang committed Nov 2, 2024
1 parent 091b9c8 commit 8bb65dd
Show file tree
Hide file tree
Showing 23 changed files with 53 additions and 430 deletions.
8 changes: 6 additions & 2 deletions modules/core/src/main/scala-2/doobie/util/GetPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ trait GetPlatform {
import doobie.util.compat.=:=

/** @group Instances */
@deprecated("Use Get.derived instead to derive instances explicitly", "1.0.0-RC6")
@deprecated("Use Get#contramap to construct the instance or Read.derived, " +
"which yields the equivalent runtime behaviour", "1.0.0-RC7")
def unaryProductGet[A, L <: HList, H, T <: HList](
implicit
G: Generic.Aux[A, L],
C: IsHCons.Aux[L, H, T],
H: Lazy[Get[H]],
E: (H :: HNil) =:= L
): MkGet[A] = MkGet.unaryProductGet
): Get[A] = {
void(C) // C drives inference but is not used directly
H.value.tmap[A](h => G.from(h :: HNil))
}

}
26 changes: 0 additions & 26 deletions modules/core/src/main/scala-2/doobie/util/MkGetPlatform.scala

This file was deleted.

26 changes: 0 additions & 26 deletions modules/core/src/main/scala-2/doobie/util/MkPutPlatform.scala

This file was deleted.

7 changes: 5 additions & 2 deletions modules/core/src/main/scala-2/doobie/util/PutPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ trait PutPlatform {
import doobie.util.compat.=:=

/** @group Instances */
@deprecated("Use Put.derived instead to derive instances explicitly", "1.0.0-RC6")
@deprecated("Use contramap or Write.derived, which yields the equivalent runtime behaviour", "1.0.0-RC7")
def unaryProductPut[A, L <: HList, H, T <: HList](
implicit
G: Generic.Aux[A, L],
C: IsHCons.Aux[L, H, T],
H: Lazy[Put[H]],
E: (H :: HNil) =:= L
): MkPut[A] = MkPut.unaryProductPut
): Put[A] = {
void(E) // E is a necessary constraint but isn't used directly
H.value.contramap[A](a => G.to(a).head)
}

}
20 changes: 0 additions & 20 deletions modules/core/src/main/scala-3/doobie/util/MkGetPlatform.scala

This file was deleted.

20 changes: 0 additions & 20 deletions modules/core/src/main/scala-3/doobie/util/MkPutPlatform.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

package doobie.util

trait PutPlatform {}
trait PutPlatform
15 changes: 3 additions & 12 deletions modules/core/src/main/scala/doobie/generic/auto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@

package doobie.generic

import doobie.util.meta.Meta
import doobie.util.{Get, Put, Read, Write}
import doobie.util.{Read, Write}

trait AutoDerivation
extends Get.Auto
with Put.Auto
with Read.Auto
extends Read.Auto
with Write.Auto

object auto extends AutoDerivation {

// FIXME: move to AutoDerivation trait itself so shares with doobie.implicits
// re-export these instances so `Meta` takes priority, must be in the object
implicit def metaProjectionGet[A](implicit m: Meta[A]): Get[A] = Get.metaProjection
implicit def metaProjectionPut[A](implicit m: Meta[A]): Put[A] = Put.metaProjectionWrite
}
object auto extends AutoDerivation
6 changes: 0 additions & 6 deletions modules/core/src/main/scala/doobie/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

import doobie.util.{Get, Put}
import doobie.util.meta.{LegacyMeta, TimeMetaInstances}
// Copyright (c) 2013-2020 Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
Expand Down Expand Up @@ -30,11 +29,6 @@ package object doobie
with LegacyMeta
with syntax.AllSyntax {

// FIXME: do we still need these?
// re-export these instances so `Meta` takes priority, must be in the object
implicit def metaProjectionGet[A](implicit m: Meta[A]): Get[A] = Get.metaProjection
implicit def metaProjectionPut[A](implicit m: Meta[A]): Put[A] = Put.metaProjectionWrite

/** Only use this import if:
* 1. You're NOT using one of the database doobie has direct java.time isntances for (PostgreSQL / MySQL). (They
* have more accurate column type checks) 2. Your driver natively supports java.time.* types
Expand Down
26 changes: 0 additions & 26 deletions modules/core/src/main/scala/doobie/util/get.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ object Get extends GetInstances with GetPlatform {

def apply[A](implicit ev: Get[A]): ev.type = ev

def derived[A](implicit ev: MkGet[A]): Get[A] = ev

trait Auto {
implicit def deriveGet[A](implicit ev: MkGet[A]): Get[A] = ev
}

/** Get instance for a basic JDBC type. */
object Basic {

Expand Down Expand Up @@ -213,23 +207,3 @@ trait GetInstances {
ev.tmap(_.toVector)

}

sealed abstract class MkGet[A](
override val typeStack: NonEmptyList[Option[String]],
override val jdbcSources: NonEmptyList[JdbcType],
override val jdbcSourceSecondary: List[JdbcType],
override val vendorTypeNames: List[String],
override val get: Coyoneda[(ResultSet, Int) => *, A]
) extends Get[A](typeStack, jdbcSources, jdbcSourceSecondary, vendorTypeNames, get)

object MkGet extends MkGetPlatform {

def lift[A](g: Get[A]): MkGet[A] =
new MkGet[A](
typeStack = g.typeStack,
jdbcSources = g.jdbcSources,
jdbcSourceSecondary = g.jdbcSourceSecondary,
vendorTypeNames = g.vendorTypeNames,
get = g.get
) {}
}
30 changes: 2 additions & 28 deletions modules/core/src/main/scala/doobie/util/put.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,10 @@ sealed abstract class Put[A](

}

object Put extends PutInstances with PutPlatform {
object Put extends PutInstances {

def apply[A](implicit ev: Put[A]): ev.type = ev

def derived[A](implicit ev: MkPut[A]): Put[A] = ev

trait Auto {
implicit def derivePut[A](implicit ev: MkPut[A]): Put[A] = ev
}

object Basic {

def apply[A](
Expand Down Expand Up @@ -208,7 +202,7 @@ object Put extends PutInstances with PutPlatform {

}

trait PutInstances {
trait PutInstances extends PutPlatform {

/** @group Instances */
implicit val ContravariantPut: Contravariant[Put] =
Expand All @@ -226,23 +220,3 @@ trait PutInstances {
ev.tcontramap(_.toArray)

}

sealed abstract class MkPut[A](
override val typeStack: NonEmptyList[Option[String]],
override val jdbcTargets: NonEmptyList[JdbcType],
override val vendorTypeNames: List[String],
override val put: ContravariantCoyoneda[(PreparedStatement, Int, *) => Unit, A],
override val update: ContravariantCoyoneda[(ResultSet, Int, *) => Unit, A]
) extends Put[A](typeStack, jdbcTargets, vendorTypeNames, put, update)

object MkPut extends MkPutPlatform {

def lift[A](g: Put[A]): MkPut[A] =
new MkPut[A](
typeStack = g.typeStack,
jdbcTargets = g.jdbcTargets,
vendorTypeNames = g.vendorTypeNames,
put = g.put,
update = g.update
) {}
}
29 changes: 0 additions & 29 deletions modules/core/src/test/scala-2/doobie/util/GetSuitePlatform.scala

This file was deleted.

28 changes: 0 additions & 28 deletions modules/core/src/test/scala-2/doobie/util/PutSuitePlatform.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import doobie.util.log.{Parameters, ProcessingFailure, Success}
import shapeless._

trait QueryLogSuitePlatform { self: QueryLogSuite =>
import doobie.generic.auto._

test("[Query] n-arg success") {
val Sql = "select 1 where ? = ?"
Expand Down
12 changes: 0 additions & 12 deletions modules/core/src/test/scala-3/doobie/util/GetSuitePlatform.scala

This file was deleted.

23 changes: 0 additions & 23 deletions modules/core/src/test/scala-3/doobie/util/PutSuitePlatform.scala

This file was deleted.

Loading

0 comments on commit 8bb65dd

Please sign in to comment.