从Slick HLIST获取元素(或将Slick HLIst转换为无形HList)

时间:2017-09-15 16:03:40

标签: scala slick shapeless

我使用光滑的codegen自动生成了scala代码。我看到一些表Rows是作为HLists实现的。 (但这些是光滑的HList,而不是正常的无形HList)

现在我希望通过光滑查询将HList中的特定元素作为Row返回。

我用Google搜索并找到了这个帖子

Getting elements from an HList

但这对于光滑的HList不起作用。它适用于Shapeless HList

我也尝试过apply方法

val x:Long = slickHList(2)

但是这不能编译,因为类型Any不符合Long的执行类型。我不想做.asInstanceOf

是否有类型安全的方法可以访问光滑的HList元素?

编辑:根据下面的输入,我编写了以下代码

package com.abhi

object SlickAndShapeless {
   import slick.collection.heterogeneous.{HCons, HList, HNil}
   import slick.collection.heterogeneous.syntax.HNil

   type MyRow = HCons[Long, HCons[String, HNil]]
   val row : MyRow = 1L :: "foo" :: HNil
   import HListExtensions._
   val hlist = row.asShapeless
   val container = new Container(hlist)
   val item = container.get(1)
}

class Container[L <: shapeless.HList](list: L) {
   import shapeless._
   import nat._
   import ops.hlist._
   def get(n: Nat)(implicit at: At[L, n.N]): at.Out = list[n.N]
}

object HListExtensions {
   import slick.collection.heterogeneous.{HNil => SHNil, HList => SHList, HCons}
   import shapeless.{::, HList, HNil}

   implicit class HListShapelessSlick(val list: HList) extends AnyVal {
      def asSlick : SHList = list match {
         case HNil => SHNil
         case head :: tail => head :: tail.asSlick
      }
   }

   implicit class HListSlickShapeless(val list: SHList) extends AnyVal {
      def asShapeless : HList = list match {
         case SHNil => HNil
         case HCons(head, tail) => head :: tail.asShapeless
      }
   }
}

上面代码的问题在于,从item获得的val item = container.get(1)类型为at.Out,而不是我期待的Long

build.sbt

libraryDependencies ++= Seq(
   "com.typesafe.slick" % "slick_2.12" % "3.2.1",
   "com.chuusai" % "shapeless_2.12" % "2.3.2"
)

我也看到两个编译器错误

Error:(19, 35) Implicit not found: shapeless.Ops.At[shapeless.HList, shapeless.Succ[shapeless._0]]. You requested to access an element at the position shapeless.Succ[shapeless._0], but the HList shapeless.HList is too short.
   val item : Long = container.get(1)
Error:(19, 35) not enough arguments for method get: (implicit at: shapeless.ops.hlist.At[shapeless.HList,shapeless.Succ[shapeless._0]])at.Out.
Unspecified value parameter at.
   val item : Long = container.get(1)

1 个答案:

答案 0 :(得分:3)

可以创建扩展方法:

nppSetStream(cudaStreamPerThread) 

示例:

object HListExtensions {

  import slick.collection.heterogeneous.{HNil => SHNil, HList => SHList, HCons}
  import shapeless.{ ::, HList, HNil }

  implicit class HListShapelessSlick(val list:HList) extends AnyVal {
    def asSlick:SHList = list match {
      case HNil => SHNil
      case head :: tail => head :: tail.asSlick
    }
  }

  implicit class HListSlickShapeless(val list:SHList) extends AnyVal {
    def asShapeless:HList = list match {
      case SHNil => HNil
      case HCons(head, tail) => head :: tail.asShapeless
    }
  }
}

我希望这会有所帮助。

编辑:这是类型级解决方案。

scala>import HListExtensions._
import HListExtensions._

scala> val x1:HList = 1 :: 2 ::  HNil
x1: slick.collection.heterogeneous.HList = 1 :: 2 :: HNil

scala> x1.asShapeless
res1: shapeless.HList = 1 :: 2 :: HNil

scala> x1.asShapeless.asSlick
res2: slick.collection.heterogeneous.HList = 1 :: 2 :: HNil

示例:

object HListsConvertersTypeLevel {

  import shapeless.{::}

  sealed trait HConv[From <: heterogeneous.HList, To <: shapeless.HList] {
    def convert(list: From): To
  }

  implicit def buildHConvNil: HConv[heterogeneous.HNil.type, shapeless.HNil] =
    new HConv[heterogeneous.HNil.type, shapeless.HNil] {
      override def convert(list: heterogeneous.HNil.type): shapeless.HNil = shapeless.HNil
    }

  implicit def buildHConv[H, T <: heterogeneous.HList, T2 <: shapeless.HList](
      implicit conv: HConv[T, T2]): HConv[HCons[H, T], ::[H, T2]] = new HConv[HCons[H, T], ::[H, T2]] {

    override def convert(list: HCons[H, T]): ::[H, T2] = {
      list.head :: conv.convert(list.tail)
    }
  }

  def toShapeless[A <: heterogeneous.HList, B <: shapeless.HList](list: A)(implicit conv: HConv[A, B]): B = conv.convert(list)

}