将case类转换为另一个递归结构相同的case类

时间:2016-08-04 09:18:12

标签: scala shapeless

我正在尝试使用Shapeless来转换像这样的案例类:

case class A(int: Int, str: String)
case class B(a: A, str: String)

case class AnotherA(int: Int, str: String)
case class AnotherB(a: AnotherA, str: String)

使用Generic我可以轻松地在AAnotherA之间进行转换,但不能在BAnotherB之间转换,因为(当然){{1} }成员属于不同类型。

我想我可以将case类转换为this example之后的嵌套a,但是如何将嵌套的HList转换回case类,或者有一种更简单的方法可以做它?

1 个答案:

答案 0 :(得分:1)

这里有一个DeepHUnlister的可能实现,它以递归方式替换泛型表示中的子Hlist(嵌套HList)及其各自的case类实例:

trait DeepHUnlister[L <: HList, R <: HList] extends (L => R)

object DeepHUnlister {

  implicit object hnil extends DeepHUnlister[HNil, HNil] {
    def apply(r: HNil) = HNil
  }

  implicit def headList[H <: HList, T <: HList, HR <: HList, TR <: HList, A](
    implicit
      gen: Generic.Aux[A, HR],
      dhh: Lazy[DeepHUnlister[H, HR]],
      dht: Lazy[DeepHUnlister[T, TR]]):
        DeepHUnlister[H :: T, A :: TR] =
    new DeepHUnlister[H :: T, A :: TR] {
      def apply(r: H :: T) = gen.from(dhh.value(r.head)) :: dht.value(r.tail)
    }

  implicit def headAtomic[H, T <: HList, TR <: HList](
    implicit dht: Lazy[DeepHUnlister[T, TR]]):
      DeepHUnlister[H :: T, H :: TR] =
    new DeepHUnlister[H :: T, H :: TR] {
      def apply(r: H :: T) = r.head :: dht.value(r.tail)
    }

  def apply[T <: HList, R <: HList](implicit du: DeepHUnlister[T, R]):
    DeepHUnlister[T, R] = du

}

示例:

case class A(int: Int, str: String)
case class B(a: A, str: String)
case class C(b: B, d: Double)

case class A1(int: Int, str: String)
case class B1(a: A1, str: String)
case class C1(a: B1, d: Double)

type ARepr = Int :: String :: HNil
type BRepr = ARepr :: String :: HNil
type CRepr = BRepr :: Double :: HNil

val c = C(B(A(1, "a"), "b"), 1.0)

val lister = DeepHLister[C :: HNil]
val repr = lister(c :: HNil)
println(repr)

val unlister = DeepHUnlister[CRepr :: HNil, C1 :: HNil]
val c1 = unlister(repr).head
// c1 = C1(B1(A1(1,a),b),1.0)

也许可以增强此解决方案以避免将表示类型作为参数传递给unlister。

<强>更新

这是一个省略源类型的版本,但遗憾的是需要强制转换:

trait DepInvFn1[T] {
  type In
  def apply(i: In): T
}

trait DeepHUnlister[R <: HList] extends DepInvFn1[R] { type In <: HList }

trait DeepHUnlisterLP {

  type Aux[L <: HList, R <: HList] = DeepHUnlister[R] { type In = L }

  implicit def headAtomic[H, TR <: HList](
    implicit dt: Lazy[DeepHUnlister[TR]]):
      Aux[H :: dt.value.In, H :: TR] =
    new DeepHUnlister[H :: TR] {
      type In = H :: dt.value.In
      def apply(r: H :: dt.value.In) = r.head :: dt.value(r.tail)
    }

}

object DeepHUnlister extends DeepHUnlisterLP {

  implicit object hnil extends DeepHUnlister[HNil] {
    type In = HNil
    def apply(r: HNil) = HNil
  }

  implicit def headList[HR <: HList, TR <: HList, A](
    implicit
      gen: Generic.Aux[A, HR],
      dh: Lazy[DeepHUnlister[HR]],
      dt: Lazy[DeepHUnlister[TR]]):
        Aux[dh.value.In :: dt.value.In, A :: TR] =
    new DeepHUnlister[A :: TR] {
      type In = dh.value.In :: dt.value.In
      def apply(r: dh.value.In :: dt.value.In) = gen.from(dh.value(r.head)) :: dt.value(r.tail)
    }

  def apply[R <: HList](implicit du: DeepHUnlister[R]): DeepHUnlister[R] = du

}

用法:

val unlister = DeepHUnlister[C1 :: HNil]
val c1 = unlister(repr.asInstanceOf[unlister.In]).head
// c1 = C1(B1(A1(1,a),b),1.0)