From 6b969dc082f9ba6c68b3579e5e71bb2304240826 Mon Sep 17 00:00:00 2001 From: vbergeron Date: Mon, 6 Nov 2023 17:21:44 +0100 Subject: [PATCH] feature: Add doobie support (#184) Co-authored-by: vbergeron --- build.sc | 11 ++++++++ .../src/io/github/iltotore/iron/doobie.scala | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 doobie/src/io/github/iltotore/iron/doobie.scala diff --git a/build.sc b/build.sc index c62e6f78..b62af4c1 100644 --- a/build.sc +++ b/build.sc @@ -401,3 +401,14 @@ object scalacheck extends SubModule { object test extends Tests } + +object doobie extends SubModule { + + def artifactName = "iron-doobie" + + def ivyDeps = Agg( + ivy"org.tpolecat::doobie-core::1.0.0-RC4" + ) + + object test extends Tests +} diff --git a/doobie/src/io/github/iltotore/iron/doobie.scala b/doobie/src/io/github/iltotore/iron/doobie.scala new file mode 100644 index 00000000..1404b821 --- /dev/null +++ b/doobie/src/io/github/iltotore/iron/doobie.scala @@ -0,0 +1,27 @@ +package io.github.iltotore.iron + +import cats.Show +import _root_.doobie.{Put, Get, Meta} + +/** + * Implicit [[Meta]]s, [[Put]]s and [[Get]]s for refined types. + */ +object doobie: + + inline given[A, C] (using inline get: Get[A])(using Constraint[A, C], Show[A]): Get[A :| C] = + get.temap[A :| C](_.refineEither) + + inline given[T](using m: RefinedTypeOps.Mirror[T], ev: Get[m.IronType]): Get[T] = + ev.asInstanceOf[Get[T]] + + inline given[A, C] (using inline put: Put[A])(using Constraint[A, C], Show[A]): Put[A :| C] = + put.tcontramap(identity) + + inline given[T](using m: RefinedTypeOps.Mirror[T], ev: Put[m.IronType]): Put[T] = + ev.asInstanceOf[Put[T]] + + inline given[A, C] (using inline meta: Meta[A])(using Constraint[A, C], Show[A]): Meta[A :| C] = + meta.tiemap[A :| C](_.refineEither)(identity) + + inline given[T](using m: RefinedTypeOps.Mirror[T], ev: Meta[m.IronType]): Meta[T] = + ev.asInstanceOf[Meta[T]]