Created
April 29, 2020 15:07
-
-
Save pathikrit/6ddf693a07c6a4f2d2ef8f80fec5c14b to your computer and use it in GitHub Desktop.
Case class morpher in scala
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._, syntax.singleton._, record._, ops.hlist._ | |
/** | |
* Given an instance A and it's generic representation AR and function f from AR => BR | |
* we can covert A to B if we also have the generic representation of BR as B | |
* We also handle misalignments using shapeless's align typeclass (https://stackoverflow.com/questions/29242873/shapeless-turn-a-case-class-into-another-with-fields-in-different-order) | |
*/ | |
case class Morph[A, AR](a: A)(implicit reprA: LabelledGeneric.Aux[A, AR]) { | |
// Why this DSL you say? Hack to get around scalac idiocy: https://stackoverflow.com/a/46614684/471136 | |
def to[B] = new { | |
//TODO: Add a better @ImplicitNotFound compile error message when a typo happens here | |
def apply[BR <: HList, BR2 <: HList](f: AR => BR2)(implicit reprB: LabelledGeneric.Aux[B, BR], align: Align[BR2, BR]): B = | |
reprB.from(align(f(reprA.to(a)))) | |
} | |
} | |
/* | |
e.g. | |
Morph(arm).to[CanonicalizedArmTransaction](_ | |
.:+('card_switch ->> arm.crd_port.map(_.take(3))) | |
.:+('derived_deci_cd ->> arm.deci_cd.orElse(arm.crd_port.collect{case port if port.startsWith("PAS") => "A"})) | |
.:+('canonical_mer_nm ->> canonical) | |
.:+('canonical_mer_nm_clean ->> cleaned) | |
.:+('canonicalized_acquirer_id ->> arm.acquirer_id.map(_.collapseWhiteSpaces.toUpperCase)) | |
.:+('canonicalized_crd_port ->> arm.crd_port.map(_.collapseWhiteSpaces.toUpperCase)) | |
.:+('canonicalized_sic_cd ->> arm.sic_cd.map(_.collapseWhiteSpaces.toUpperCase)) | |
.:+('canonicalized_mer_id ->> arm.mer_id.map(Canonicalizer.aggressivelySanitize).map(_.toUpperCase)) | |
) | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment