由于含糊不清的隐式,无法将 MappedProjection 转换为 ProvenShape

Can't convert MappedProjection to ProvenShape due to ambiguous implicit

能否请您向我解释一下如何将 MappedProjection 转换为 ProvenShape,目前由于含糊不清的隐式而失败?

我使用 slick-pg 来支持 Postgres 数据库中的 jsonb 类型。

我有一个简单的案例 class,我想将其存储为 json,并且 ID 列的值来自案例 class.

case class Topic(id: String, title: String)

class TopicRow(tag: Tag) extends Table[(String, Topic)](tag, Topic.tableName) {

  def id = column[String]("id", O.PrimaryKey)
  def json = column[Topic]("json")

  def * = (id, json)
}

在代码的前面,我在 json 和大小写 class

之间进行了转换
import spray.json._
implicit val TopicColumnType = MappedColumnType.base[Topic, JsValue ](
  { obj => obj.toJson }, { json => json.convertTo[Topic] }
)

我不喜欢 TopicRow 的是它映射到元组。我希望它是这样的

class TopicRow(tag: Tag) extends Table[Topic](tag, Topic.tableName) {

  def id = column[String]("id", O.PrimaryKey)
  def json = column[Topic]("json")

  val tupled: ((String, Topic)) => (Topic) = tu => tu._2
  val unapply: (Topic) => Option[(String, Topic)] = t => Option((t.id, t))
  def * = (id, json) <> (tupled, unapply)
}

编译失败

Error:(43, 22) type mismatch;
 found   : slick.lifted.MappedProjection[co.younetworks.medstream.server.models.db.Topic,(String, co.younetworks.medstream.server.models.db.Topic)]
 required: slick.lifted.ProvenShape[co.younetworks.medstream.server.models.db.Topic]

所以我像这样明确指定了转换的每个步骤

def * = {
  val shape: ProvenShape[Topic] = {
    val tupled: ((String, Topic)) => (Topic) = tu => tu._2
    val unapply: (Topic) => Option[(String, Topic)] = t => Option((t.id, t))
    val toToShapedValue: ToShapedValue[(Rep[String], Rep[Topic])] = anyToToShapedValue((id, json))
    val mappedProjection: MappedProjection[Topic, (String, Topic)] = toToShapedValue <>(tupled, unapply)
    ProvenShape.proveShapeOf(mappedProjection)
  }
  shape
}

这给了我错误

Error:(52, 31) ambiguous implicit values:
 both method mappedProjectionShape in object MappedProjection of type [Level >: slick.lifted.FlatShapeLevel <: slick.lifted.ShapeLevel, T, P]=> slick.lifted.Shape[Level,slick.lifted.MappedProjection[T,P],T,slick.lifted.MappedProjection[T,P]]
 and method repColumnShape in trait RepShapeImplicits of type [T, Level <: slick.lifted.ShapeLevel](implicit evidence: slick.ast.BaseTypedType[T])slick.lifted.Shape[Level,slick.lifted.Rep[T],T,slick.lifted.Rep[T]]
 match expected type slick.lifted.Shape[_ <: slick.lifted.FlatShapeLevel, slick.lifted.MappedProjection[co.younetworks.medstream.server.models.db.Topic,(String, co.younetworks.medstream.server.models.db.Topic)], U, _]
      ProvenShape.proveShapeOf(mappedProjection)
                              ^

我怀疑我将编译器与映射列的存在混淆了,但我不知道如何解决它。

好吧,隐式的显式规范很有帮助。不过不好看。

class TopicRow(tag: Tag) extends Table[Topic](tag, Topic.tableName) with JsonMarshallers {

  def id = column[String]("id", O.PrimaryKey)
  def json = column[Topic]("json")

  private val fromTuple : ((String, Topic)) => (Topic) = { case (_, value) => value }
  private val toTuple = (v : Topic) => Option((v.id, v))
  def * = ProvenShape.proveShapeOf((id, json) <> (fromTuple, toTuple))(MappedProjection.mappedProjectionShape)
}

如果有人知道如何使其短路,请告诉我。