Last active
May 2, 2018 19:06
-
-
Save bpholt/4479674b530b865e0a9c3eb4215d8fca to your computer and use it in GitHub Desktop.
Generic Case Class Migration, but explicitly setting missing values
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._ | |
import shapeless.ops.hlist._ | |
import shapeless.syntax.singleton._ | |
object GenericCaseClassMigration extends App { | |
case class X(s1: String, s2: String) | |
case class Y(s2: String, i: Int, s1: String) | |
implicit class GenericCaseClassMigrator[A](f: A) { | |
class PartiallyApplied[B] { | |
def apply[ARepr <: HList, BRepr <: HList, Common <: HList, Added <: HList, Unaligned <: HList](added: Added) | |
(implicit | |
fromGen: LabelledGeneric.Aux[A, ARepr], | |
toGen: LabelledGeneric.Aux[B, BRepr], | |
@typeHelper inter: Intersection.Aux[ARepr, BRepr, Common], | |
@typeHelper diff: Diff.Aux[BRepr, Common, Added], | |
prepend: Prepend.Aux[Added, ARepr, Unaligned], | |
align: Align[Unaligned, BRepr] | |
): B = | |
toGen.from(align(prepend(added, fromGen.to(f)))) | |
} | |
def migrateTo[B] = new PartiallyApplied[B] | |
} | |
val migratedY = X("s1", "s2").migrateTo[Y]('i ->> 42 :: HNil) | |
assert(migratedY == Y("s2", 42, "s1")) | |
} | |
class typeHelper extends deprecated("not deprecated—this is only here to prevent the compiler from emitting unused param warnings", "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment