tminglei / slick-pg

Slick extensions for PostgreSQL
BSD 2-Clause "Simplified" License
837 stars 180 forks source link

Could not find implicit value for parameter tt: slick.ast.TypedType[List[List[Int]]] #418

Open binshi opened 6 years ago

binshi commented 6 years ago

When trying to define below def bbox = column[List[List[Int]]]("bbox")

I get the following error

Could not find implicit value for parameter tt: slick.ast.TypedType[List[List[Int]]]

jacktok commented 5 years ago

hi @binshi you can custom parse string <=> List[List[Int]] by add implicit val of AdvancedArrayJdbcType to your PgProfile.Api

example

    val white: WhitespaceApi.Wrapper = fastparse.WhitespaceApi.Wrapper({
      import fastparse.all._
      NoTrace(" ").rep
    })
    import fastparse.noApi._
    import white._
    val number: P[Int] = P( "-".? ~ CharIn('0'to'9').rep(1)).!.map(_.toInt)
    val open  = "{"
    val close = "}"
    val listNumber: P[List[Int]] = P(open ~ number.rep(sep = ",") ~ close).map(_.toList)
    val list2DNumber: P[List[List[Int]]] = P(Start ~ open ~ listNumber.rep(sep = ",") ~ close ~ End).map(_.toList)

    def listToString[T](list: List[T]): String = list.mkString("{", ",", "}")
    def list2DNumberToString(list: List[List[Int]]): String = listToString(list.map(listToString))

    def parse(string: String): List[List[Int]] = {
      list2DNumber.parse(string) match {
        case fastparse.all.Parsed.Success(value, _) => value
        case fastparse.all.Parsed.Failure(lastParser, index, extra) => throw new Exception(lastParser.toString)// do something
      }
    }

    implicit val intList2DTypeMapper: DriverJdbcType[List[List[Int]]] = new AdvancedArrayJdbcType[List[Int]]("int []",
      s => parse(s), // string => list[list[int]]
      v => list2DNumberToString(v.toList) // seq[list[int]] => string
    ).to(_.toList)

This parse for Postgres int [][] only. you can custom it. https://github.com/lihaoyi/fastparse this is helpful for parse string to some type

glothos commented 4 years ago

Did you find a solution for this? I'm seeing the same error for a simple List[Long] type.

package com.lux.database

import com.github.tminglei.slickpg._
import slick.basic.Capability
import slick.jdbc._
import play.api.libs.json._

trait PgProfile extends ExPostgresProfile
  with PgDateSupportJoda
  with PgArraySupport
  with PgDate2Support
  with PgRangeSupport
  with PgHStoreSupport
  with PgSearchSupport
  with PgNetSupport
  with PgLTreeSupport
  with PgPlayJsonSupport {

  def pgjson = "jsonb"

  override protected def computeCapabilities: Set[Capability] =
    super.computeCapabilities + JdbcCapabilities.insertOrUpdate

  override val api: API = MyAPI

  object MyAPI extends super.API
    with JodaDateTimeImplicits
    with DateTimeImplicits
    with JsonImplicits
    with NetImplicits
    with LTreeImplicits
    with RangeImplicits
    with ArrayImplicits
    with HStoreImplicits
    with SearchImplicits
    with SearchAssistants {
    implicit val longListTypeMapper = new SimpleArrayJdbcType[Long]("int8").to(_.toList)
    implicit val playJsonArrayTypeMapper =
      new AdvancedArrayJdbcType[JsValue](pgjson,
        (s) => utils.SimpleArrayUtils.fromString[JsValue](Json.parse(_))(s).orNull,
        (v) => utils.SimpleArrayUtils.mkString[JsValue](_.toString())(v)
      ).to(_.toList)
  }
}

object PgProfile extends PgProfile
could not find implicit value for parameter tt: slick.ast.TypedType[List[Long]]