Created
February 22, 2017 19:36
-
-
Save pvillega/62d7945eb1e2cd8ec7f4db1b131f545c to your computer and use it in GitHub Desktop.
Allow Refined types on case classes used by Doobie
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
// Credit to Kevin Horlick @beefyhalo fir the Meta implementation | |
// See https://gitter.im/tpolecat/doobie/archives/2015/12/22 | |
// It wasn't straightforward to find the answer, my Google-foo is weak, so I hope | |
// this helps people looking for this answer | |
// allows generation of doobie Meta objects from Refined types | |
implicit def refinedMeta[T: Meta, P, F[_, _]](implicit tt: TypeTag[F[T, P]], | |
ct: ClassTag[F[T, P]], | |
validate: Validate[T, P], | |
refType: RefType[F]): Meta[F[T, P]] = | |
Meta[T].xmap(refType.refine[P](_) match { | |
case Left(err) => throw InvalidObjectMapping(ct.runtimeClass, ct.getClass) | |
case Right(t) => t | |
}, refType.unwrap) | |
// allows generation of doobie Composite objects from Refined types | |
implicit def refinedComposite[T: Composite, P, F[_, _]](implicit tt: TypeTag[F[T, P]], | |
ct: ClassTag[F[T, P]], | |
validate: Validate[T, P], | |
refType: RefType[F]): Composite[F[T, P]] = | |
Composite[T].imap(refType.refine[P](_) match { | |
case Left(err) => throw InvalidObjectMapping(ct.runtimeClass, ct.getClass) | |
case Right(t) => t | |
})(refType.unwrap) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI we plan to have a
doobie-refined
module for 0.4.2