Last active
February 18, 2019 13:14
-
-
Save yasuabe/b2c3f4d9d9a55594ea84c659773632e1 to your computer and use it in GitHub Desktop.
minimal cake + tagless final
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 cats.data.Chain | |
// domain layer ----------------------------------------------------------- | |
case class Movie(id: Int, title: String) | |
trait MovieRepo[F[_]] { | |
def getMovie(id: Int): F[Option[Movie]] | |
} | |
trait UsesMovieRepo[F[_]] { | |
val movieRepo: MovieRepo[F] | |
} | |
trait MovieService[F[_]] extends UsesMovieRepo[F] { | |
def getMovie(id: Int): F[Option[Movie]] = movieRepo.getMovie(id) | |
} | |
// application layer ------------------------------------------------- | |
import monix.eval.Task | |
val db = Map[Int, Movie](42 -> Movie(42, "A Movie")) | |
object MovieRepoImpl extends MovieRepo[Task] { | |
def getMovie(id: Int): Task[Option[Movie]] = Task(db.get(id)) | |
} | |
trait MixInMovieRepo { | |
val movieRepo: MovieRepo[Task] = MovieRepoImpl | |
} | |
object MovieService extends MovieService[Task] with MixInMovieRepo | |
val program: Task[Option[Movie]] = MovieService.getMovie(42) | |
// runtime ------------------------------------------------------ | |
import scala.concurrent.duration._ | |
import scala.concurrent.Await | |
import monix.execution.Scheduler.Implicits.global | |
Await.result(program.runToFuture, 1.second) | |
//Some(Movie(42,A Movie)) | |
// test environment ------------------------------------------------------- | |
import cats.data.Writer | |
import cats.syntax.writer._ | |
import cats.syntax.option._ | |
type LogWriter[V] = Writer[Chain[String], V] | |
val logWriterMovieRepo = new MovieRepo[LogWriter] { | |
def getMovie(id: Int): LogWriter[Option[Movie]] = for { | |
_ <- Chain(s"getMovie($id)").tell | |
} yield Movie(id, "Dummy").some | |
} | |
object TestMovieService extends MovieService[LogWriter] { | |
val movieRepo = logWriterMovieRepo | |
} | |
TestMovieService.getMovie(42).run | |
//(Chain(getMovie(42)),Some(Movie(42,Dummy))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment