Created
November 19, 2016 22:21
-
-
Save sergeykolbasov/364b244e9f9fc5e888bc20cbe6236bb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on https://gist.github.com/ImLiar/0022a663d3bf83bfbb7a63021447b6d8