Cannot convert MappedProjection to ProvenShape due to ambiguous implicit

Could you explain to me how I can convert MappedProjectionto ProvenShapewhich is currently failing due to ambiguous implicit?

I use slick-pg for type support jsonbin Postgres DB.

I have a simple case class that I want to save as json, and there is an ID column with a value from the case 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)
}

earlier in code I have a conversion between json and case class

import spray.json._
implicit val TopicColumnType = MappedColumnType.base[Topic, JsValue ](
  { obj => obj.toJson }, { json => json.convertTo[Topic] }
)

What I don't like TopicRowabout is that it maps to a tuple. I wish it was something like this

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)
}

Unable to compile with error

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]

So, I specifically indicated each conversion step like this

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
}

what gives me an error

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$1: 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)
                              ^

, , , .

+4
1

, . , .

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)
}

- , , , .

+4

Source: https://habr.com/ru/post/1620842/


All Articles