-
-
Save milessabin/6510067 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._ | |
import shapeless.poly._ | |
import shapeless.ops.hlist._ | |
import scala.util.Try | |
trait Deserializer[A, B] { | |
def apply(t: A): Either[String, B] | |
} | |
object Deserializer { | |
implicit def identity[A] = new Deserializer[A, A] { | |
def apply(t: A) = Right(t) | |
} | |
implicit def stringToInt = new Deserializer[String, Int] { | |
def apply(s: String) = try Right(s.toInt) catch { case _ => Left("error") } | |
} | |
} | |
trait DeserializeTo[In <: HList, Out <: HList] { | |
def apply(l: In): Out | |
} | |
object DeserializeTo { | |
private class BubbleLeftException(val left: Left[Any, Any]) extends RuntimeException | |
implicit def hnilDeserializer: DeserializeTo[HNil, HNil] = | |
new DeserializeTo[HNil, HNil] { | |
def apply(l: HNil) = HNil | |
} | |
implicit def hlistDeserializer[InH, InT <: HList, OutH, OutT <: HList](implicit des: Deserializer[InH, OutH], dt: DeserializeTo[InT, OutT]) = | |
new DeserializeTo[InH :: InT, OutH :: OutT] { | |
def apply(l: InH :: InT) = des(l.head) match { | |
case Right(x) => x :: dt(l.tail) | |
case left: Left[_, _] => throw new BubbleLeftException(left) | |
} | |
} | |
} | |
class HListDeserializer[T, Repr <: HList](gen: Generic.Aux[T, Repr]) { | |
def apply[L <: HList](l: L)(implicit des: DeserializeTo[L, Repr]) = gen.from(des(l)) | |
} | |
object HListDeserializer { | |
def apply[T](implicit gen: Generic[T] { type Repr <: HList }) = new HListDeserializer[T, gen.Repr](gen) | |
} | |
case class Foo(i1: Int, i2: Int) | |
val des = HListDeserializer[Foo] | |
des(1 :: 2 :: HNil) | |
// <console>:31: error: could not find implicit value for parameter des: DeserializeTo[shapeless.::[Int,shapeless.::[Int,shapeless.HNil]],des.gen.Repr] | |
// des(1 :: 2 :: HNil) | |
// ^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment