我目前正在阅读关于猫的教程。
本教程为我提供了一个猫面包库(使用Slick),我不应该编辑它,然后要求我实现不同的方法。不幸的是,我坚持看似非常明显的事情。
我得到了方法
def findByName(name: String)(implicit ec: ExecutionContext): DBIO[Option[Breed]] = {
query.filter(_.name === name).result.headOption
}
breedsRepository
中的我应该实现以下方法:
def findBreed(name: Either[Int, String]): DBIO[Either[Int, Breed]] = ???
我想到了:
def findBreed(name: Either[Int, String]): DBIO[Either[Int, Breed]] =
name.traverse(n => breedsRepository.findByName(n).map(_.get))
但我收到错误could not find implicit value for parameter F: cats.Applicative[F]
谁能帮助我?
答案 0 :(得分:2)
以下代码编译:
import slick.jdbc.PostgresProfile.api._
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits.global
import cats.implicits._
import com.rms.miu.slickcats.DBIOInstances._
object breedsRepository {
case class Breed(name: String, price: Double)
class BreedTable(tag: Tag) extends Table[Breed](tag, "BREEDS") {
def name = column[String]("NAME", O.PrimaryKey)
def price = column[Double]("PRICE")
def * = (name, price) <> (Breed.tupled, Breed.unapply)
}
val query = TableQuery[BreedTable]
def findByName(name: String)(implicit ec: ExecutionContext): DBIO[Option[Breed]] = {
query.filter(_.name === name).result.headOption
}
def findBreed(name: Either[Int, String]): DBIO[Either[Int, Breed]] =
name.traverse[DBIO, Int, Breed](n => findByName(n).map(_.get))
}
build.sbt
name := "slickdemo"
version := "0.1"
scalaVersion := "2.12.6"
scalacOptions += "-Ypartial-unification"
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.2.3",
"org.slf4j" % "slf4j-nop" % "1.6.4",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.3"
)
libraryDependencies += "org.typelevel" %% "cats-core" % "1.0.1"
libraryDependencies += "com.rms.miu" %% "slick-cats" % "0.7.1.1"