newtypes icon indicating copy to clipboard operation
newtypes copied to clipboard

Doobie integration

Open ioleo opened this issue 1 year ago • 1 comments

It would be great to see doobie integration (Get and Put instances for arbitrary newtypes).

ioleo avatar Aug 12 '23 12:08 ioleo

For now I'm using

import cats.Show
import monix.newtypes.*

given newtypesGet[T, S](using
  B: HasBuilder.Aux[T, S],
  G: Get[S],
  V: Show[S]
): Get[T] =
  G.temap(s => B.build(s).left.map(ex => s"Invalid ${ex.message.fold("")(m => s" — $m")}"))

given newtypesPut[T, S](using
  E: HasExtractor.Aux[T, S],
  P: Put[S]
): Put[T] =
  P.contramap(t => E.extract(t))

ioleo avatar Aug 12 '23 13:08 ioleo