Skip to content

Instantly share code, notes, and snippets.

@pvillega
Created February 22, 2017 19:36
Show Gist options
  • Save pvillega/62d7945eb1e2cd8ec7f4db1b131f545c to your computer and use it in GitHub Desktop.
Save pvillega/62d7945eb1e2cd8ec7f4db1b131f545c to your computer and use it in GitHub Desktop.
Allow Refined types on case classes used by Doobie
// 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)
@tpolecat
Copy link

FYI we plan to have a doobie-refined module for 0.4.2

@pvillega
Copy link
Author

That's great! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment