Search code examples
scalashapeless

Converting case class to another recursively structural identical case class


I am trying to use Shapeless to convert case class like these:

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)

Using Generic I can easily convert between A and AnotherA, but not for B and AnotherB because (of course) the a member is of different type.

I think I can convert case class into a nested HList following this example, but how do I convert the nested HList back to case class or there is a more straightforward way to do it ?


Solution

  • Here's a possible implementation of a DeepHUnlister which recursively replaces the sub-Hlists within the generic representation (nested HList) with their respective case class instances:

    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
    
    }
    

    Example:

    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)
    

    Maybe it's possible to enhance this solution to avoid passing the representation type as parameter to the unlister.


    Update

    Here's a version that omits the source type, but unfortunately requires a cast:

    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
    
    }
    

    Usage:

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