Last active
November 23, 2021 13:25
-
-
Save pierangeloc/ec408ca3a864f5d72dbc83a0e793e59a to your computer and use it in GitHub Desktop.
Doobie instances for NewType
This file contains hidden or 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 io.estatico.newtype.ops._ | |
import io.estatico.newtype.macros.newtype | |
import eu.timepit.refined.api.Refined | |
import doobie.implicits._ | |
import doobie.postgres.implicits._ | |
import doobie.refined.implicits._ | |
/** | |
* Derive a Put (so it can be interpolated in doobie sql expressions) for any newtype backed by a type that is | |
* supported by doobie (e.g. any wrapper of String, UUID, Boolean etc) | |
* | |
* if a: A has a Coercible[A, B] means one can transform a to b: B via a.coerce[B] | |
*/ | |
implicit def newTypePut[N: Coercible[*, R], R: Put]: Put[N] = Put[R].contramap[N](_.coerce[R]) | |
implicit def newTypeRead[N: Coercible[R, *], R: Read]: Read[N] = Read[R].map(_.asInstanceOf[N]) | |
/** If we have an Eq instance for Repr type R, derive an Eq instance for NewType N. */ | |
implicit def coercibleEq[R, N](implicit ev: Coercible[Eq[R], Eq[N]], R: Eq[R]): Eq[N] = | |
ev(R) | |
/* | |
* With this one can create value classes like these and embed them in doobie fragments | |
*/ | |
@newtype case class UserId(value: UUID) | |
val userId: UserId = ??? | |
val fragment = sql"select * from users where user_id = $userId" |
Thanks, I fixed it for 0.4.4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with newtype 0.4.4, I got
This compiled: