Skip to content

Instantly share code, notes, and snippets.

@ahjohannessen
Forked from ChristopherDavenport/Foo.scala
Created September 21, 2018 15:30
Show Gist options
  • Save ahjohannessen/2eb5c6cb4abc3c28762240a93b6eedc9 to your computer and use it in GitHub Desktop.
Save ahjohannessen/2eb5c6cb4abc3c28762240a93b6eedc9 to your computer and use it in GitHub Desktop.
ConnectionIO Final Tagless Example
import doobie._
import doobie.implicits._
// Something for FunctionK ~>
trait Foo[F[_]]{
def getFoo: F[Bar]
}
object Foo {
case class Bar(x: Int)
object Bar {
implicit val metaBar: Meta[Bar] = Meta[Int].imap(Bar(_), _.x)
}
// For Metadata Testing
private[Foo] val getBar : Query0[Bar] = sql"SELECT 1".query[Bar]
private def connectIOTrait: Foo[ConnectionIO] = new Foo[ConnectionIO]{
def getFoo: ConnectionIO[Bar] = getBar.unique
}
def mapK[F[_], G[_]](foo: Foo[F])(f: F ~> G): Foo[G] = new Foo[G]{
def getFoo: G[Bar] = f(foo.getFoo)
}
def impl[F[_]](t: Transactor[F]): Foo[F] = connectIOTrait.mapK(_.transact(t))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment