无法为类型 User 构造 Read 实例。 Scala 中对 Doobie 的类型误解

Cannot construct a Read instance for type User. Type misunderstanding with Doobie in Scala

我正在尝试 return 使用 doobie、http4s 和 cats 从数据库中获取用户记录。我一直受到类型系统的阻碍,它根据以下代码提供以下错误:

路由器:

val httpRoutes = HttpRoutes.of[IO] {
    case GET -> Root / "second" / id =>
      val intId : Integer = Integer.parseInt(id)
      //if i make thie ConnectionIO[Option[Unit]] it compiles, but returns a cats Free object
      val userOption: ConnectionIO[Option[User]] = UserModel.findById(intId, transactor.transactor)
      Ok(s"userOption is instance of: ${userOption.getClass} object: ${userOption.toString}")
  }.orNotFound

型号:

case class User(
                 id: Read[Integer],
                 username: Read[String],
                 email: Read[String],
                 passwordHash: Read[String], //PasswordHash[SCrypt],
                 isActive: Read[Boolean],
                 dob: Read[Date]
               ) {
//  def verifyPassword(password: String) : VerificationStatus = SCrypt.checkpw[cats.Id](password, passwordHash)
}

object UserModel {
  def findById[User: Read](id: Integer, transactor: Transactor[ConnectionIO]): ConnectionIO[Option[User]] = findBy(fr"id = ${id.toString}", transactor)

  private def findBy[User: Read](by: Fragment, transactor: Transactor[ConnectionIO]): ConnectionIO[Option[User]] = {
    (sql"SELECT id, username, email, password_hash, is_active, dob FROM public.user WHERE " ++ by)
      .query[User]
      .option
      .transact(transactor)
  }
}

错误:

Error:(35, 70) Cannot find or construct a Read instance for type:
  core.model.User
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.
      val userOption: ConnectionIO[Option[User]] = UserModel.findById(intId, transactor.transactor)

如果我将行更改为 ConnectionIO[Option[User] 到 ConnectionIO[Option[Unit]],它会编译并运行,但是 returns 是来自我的 cats 库的一个 Free(...) 对象一直无法弄清楚如何解析,我不明白为什么我不应该 return 我的案例 class!

另请参阅 findBy 和 findById 方法的类型声明。在我添加那些之前,有一个编译错误,说它找到了一个用户,但需要一个 Read[User]。我尝试将相同的类型声明应用于路由器中 findById 的调用,但它给出了上面提供的相同错误。

在此先感谢您的帮助,还请大家多多包涵。我从未遇到过比我更聪明的类型系统!

这里有很多东西要打开...

  1. 您不需要在 Read 中换行 User 中的字段。
  2. 不需要使用 User 参数化函数,因为您知道返回的是什么类型。
  3. 大多数情况下,如果您手动处理 Read 个实例,那么您做错了什么。构建 Read 实例仅在您正在读取的数据未直接映射到您的类型时有用。
  4. Transactor 是通过调用连接从 ConnectionIO(在 JDBC 连接上的某些操作)到其他一些单子(例如 IO)的转换,执行交易中的动作,并处理所述动作。 Transactor[ConnectionIO] 对此没有多大意义,并且可能会导致死锁(因为您最终会在保持连接的同时尝试调用连接)。只需在 ConnectionIO 中编写您的 DB 逻辑,然后 transact 整个事情。
  5. Integer 除了与 Java 互操作外,在 Scala 代码中不使用
  6. Integer,Doobie 没有 Get/Put 实例。
  7. 在您的路线中,您选择 ConnectionIO[Option[User]],然后执行 .toString。这不会执行您想要的 - 它只是将您构建的操作变成无用的字符串,而没有实际评估它。要真正获得 Option[User],您需要评估您的行为。

将所有这些放在一起,我们最终得到这样一段代码:

import java.util.Date
import cats.effect.IO
import doobie.{ConnectionIO, Fragment, Transactor}
import doobie.implicits._
import org.http4s.HttpRoutes
import org.http4s.dsl.io._
import org.http4s.syntax.kleisli._

def httpRoutes(transactor: Transactor[IO]) = HttpRoutes.of[IO] {
  case GET -> Root / "second" / IntVar(intId) =>
    UserModel.findById(intId)
      .transact(transactor)
      .flatMap { userOption =>
        Ok(s"userOption is instance of: ${userOption.getClass} object: ${userOption.toString}")
      }
}.orNotFound

final case class User(
  id: Int,
  username: String,
  email: String,
  passwordHash: String,
  isActive: Boolean,
  dob: Date
)

object UserModel {
  def findById(id: Int): ConnectionIO[Option[User]] = findBy(fr"id = ${id.toString}")

  private def findBy(by: Fragment): ConnectionIO[Option[User]] =
    (sql"SELECT id, username, email, password_hash, is_active, dob FROM public.user WHERE " ++ by)
      .query[User]
      .option
}

userOption这里是Option[User].