如果我正确恢复了您的代码片段,则类似于
import doobie.Read
import doobie.implicits.toSqlInterpolator
import doobie.util.fragment.Fragment
import scala.reflect.{ClassTag, classTag}
object App {
val snakeCase = ???
abstract class CRUDAbs[A: Read: ClassTag](val tableName: String) /*extends TransactSQL*/ {
def table: Fragment = Fragment.const(s"$tableName")
def columnsList: Array[String] = {
val cls = classTag[A].runtimeClass
cls.getDeclaredFields.map(_.getName).map(snakeCase)
}
def columns: Fragment = Fragment.const(columnsList.mkString(","))
def find(id: Int): doobie.Query0[Option[A]] =
(sql"select " ++ columns ++ sql" from " ++ table ++ sql" where " ++ Fragment.const(
s"${columnsList.head}"
) ++ sql" = $id").query[Option[A]]
}
}
整个编译错误是
Cannot find or construct a Read instance for type:
Option[A]
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 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, HLists, and shapeless records ensure that each element
has a Read instance in scope.
- Lather, rinse, repeat, recursively until you find the problematic bit.
You can check that an instance exists for Read in the REPL or in your code:
scala> Read[Foo]
and similarly with Get:
scala> Get[Foo]
And find the missing instance and construct it as needed. Refer to Chapter 12
of the book of doobie for more information.
) ++ sql" = $id").query[Option[A]]
请注意For Option types, ensure that a Read instance is in scope for the non-Option version.
类型有一个类型类的实例,前提是Read
类型有一个类型类的实例Option[A]
Get
A
implicit def fromGetOption[A](implicit ev: Get[A]): Read[Option[A]] =
new Read(List((ev, Nullable)), ev.unsafeGetNullable)
https://github.com/tpolecat/doobie/blob/main/modules/core/src/main/scala/doobie/util/read.scala#L76-L77
所以尝试使用修改类的CRUDAbs
定义doobie.Get
abstract class CRUDAbs[A: Get: ClassTag](val tableName: String)
实际上,让我们回到Read
上下文绑定。事实证明(上述编译错误中提到的“doobie 书第 12 章”)类型类Get
适用于非可选(不可为空)单变量(单列)类型,而类型类Put
也适用于可选(可为空)或多变量(向量)类型。让我们定义一个类似于Read.fromGetOption
implicit def fromGetOption[A](implicit ev: Get[A]): Read[Option[A]] =
new Read(List((ev, Nullable)), ev.unsafeGetNullable)
即让我们定义
implicit def fromReadOption[A: Read]: Read[Option[A]] = Read[A].map(Some(_)) // Read[A].map(Option(_))
现在下面的代码编译
import doobie.Read
import doobie.implicits.toSqlInterpolator
import doobie.util.fragment.Fragment
import scala.reflect.{ClassTag, classTag}
object App {
val snakeCase = ???
implicit def fromReadOption[A: Read]: Read[Option[A]] = Read[A].map(Some(_))
abstract class CRUDAbs[A: Read: ClassTag](val tableName: String) /*extends TransactSQL*/ {
def table: Fragment = Fragment.const(s"$tableName")
def columnsList: Array[String] = {
val cls = classTag[A].runtimeClass
cls.getDeclaredFields.map(_.getName).map(snakeCase)
}
def columns: Fragment = Fragment.const(columnsList.mkString(","))
def find(id: Int): doobie.Query0[Option[A]] =
(sql"select " ++ columns ++ sql" from " ++ table ++ sql" where " ++ Fragment.const(
s"${columnsList.head}"
) ++ sql" = $id").query[Option[A]]
}
case class Chain0(i: Int)
case class Chain(i: Int, s: String)
case class Chain1(i: Int, s: Option[String])
class CRUDChain0 extends CRUDAbs[Chain0]("chain") // compiles
class CRUDChain extends CRUDAbs[Chain]("chain") // compiles
class CRUDChain1 extends CRUDAbs[Chain1]("chain") // compiles
}
https://scastie.scala-lang.org/DmytroMitin/2kjpdtvaSVqxK6IhnmVEVQ
或者不定义隐式fromReadOption
而是用两个隐式参数替换上下文绑定
abstract class CRUDAbs[A: ClassTag](val tableName: String)(implicit r: Read[A], optR: Read[Option[A]])
或者
abstract class CRUDAbs[A: Read : ClassTag](val tableName: String)(implicit optR: Read[Option[A]])
或以kind-projector语法
abstract class CRUDAbs[A: Read : λ[X => Read[Option[X]]] : ClassTag](val tableName: String)
https://scastie.scala-lang.org/DmytroMitin/2kjpdtvaSVqxK6IhnmVEVQ/1