Skip to content

Instantly share code, notes, and snippets.

@afsalthaj
Last active April 28, 2018 03:19
Show Gist options
  • Save afsalthaj/4b0b2fe49bc5c778b9ce9ac08ad96cc1 to your computer and use it in GitHub Desktop.
Save afsalthaj/4b0b2fe49bc5c778b9ce9ac08ad96cc1 to your computer and use it in GitHub Desktop.
package com.telstra.dxhub.productmapper
import shapeless.{::, Generic, HList, HNil, Poly, Poly1}
import shapeless.PolyDefns.{Case, Case0}
import shapeless.ops.hlist.Mapper
trait ProductMapper[A, B, P] {
def apply(a: A): B
}
object ProductMapper {
implicit def productMapper[A, B, ARepr <: HList, BRepr <: HList, HF <: Poly](
implicit genA: Generic.Aux[A, ARepr],
genB: Generic.Aux[B, BRepr],
mapper: Mapper.Aux[HF, ARepr, BRepr]
): ProductMapper[A, B, HF] = {
a => genB.from(mapper.apply(genA.to(a)))
}
case class Afsal(x: Int)
case class BAfsal(y: Int)
object conversions extends Poly1 {
implicit val intCase: Case.Aux[Int, Int] = at( _ +10)
}
/* implicit class ToProductMapperOps[A, B, HF](a: A)(implicit m: ProductMapper[A, B, HF]){
def mapTo: B = m.apply(a)
}*/
implicit class ToProductMapper[A](a: A) {
class Builder[B] {
def apply[P <: Poly](poly: P)(implicit pm: ProductMapper[A, B, P]): B =
pm.apply(a)
}
def mapTo[B]: Builder[B] = new Builder[B]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment