Skip to content

Instantly share code, notes, and snippets.

@sergeykolbasov
Created November 19, 2016 22:21
Show Gist options
  • Save sergeykolbasov/364b244e9f9fc5e888bc20cbe6236bb3 to your computer and use it in GitHub Desktop.
Save sergeykolbasov/364b244e9f9fc5e888bc20cbe6236bb3 to your computer and use it in GitHub Desktop.
import shapeless.{HList, HNil, LabelledGeneric}
import deepcopy._
import shapeless.ops.record.Merger
import shapeless.syntax.singleton._
object copyandmerge extends App {
implicit class MergeAndCopy[A](a: A) {
class Converter[B] {
def apply[AR <: HList, BR <: HList, MR <: HList, Fields <: HList](fields: Fields)(implicit
aLg: LabelledGeneric.Aux[A, AR],
bLg: LabelledGeneric.Aux[B, BR],
merger: Merger.Aux[AR, Fields, MR],
deepCopy: DeepCopy.Aux[MR, BR]): B = {
val from = aLg.to(a)
val merged = merger(from, fields)
val copy = deepCopy(merged)
bLg.from(copy)
}
}
def convertWith[B]: Converter[B] = new Converter[B]
}
case class A(i: Int)
case class B(a: A)
case class C(b: B, i: Int)
case class A2(i: Int)
case class B2(a: A2)
case class C2(b: B2)
println(C2(B2(A2(123))).convertWith[C](('i ->> 43) :: HNil))
}
@sergeykolbasov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment