Created
July 4, 2019 10:10
-
-
Save iamanandkris/f1e6b7f5fa6c7876743cb737d57d4543 to your computer and use it in GitHub Desktop.
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.{State, StateT} | |
import scalaz.zio._ | |
type UserID = String | |
case class UserProfile(name:String) | |
trait Database { | |
def lookup(id: UserID): Task[UserProfile] | |
def update(id: UserID, profile: UserProfile): Task[Unit] | |
} | |
trait Logger { | |
def info(id: String): Task[Unit] | |
} | |
sealed trait DBOp | |
final case class TestInstructionsState(ops: List[String]) | |
trait TestService{ | |
val ref: Ref[TestInstructionsState] | |
} | |
trait DatabaseTestService extends Database with TestService { | |
private var map: Map[UserID, UserProfile] = Map("abc" -> UserProfile("testName")) | |
override val ref: Ref[TestInstructionsState] | |
def lookup(id: UserID): Task[UserProfile] = { | |
ref.update(x => x.copy(ops = "LookedUP"::x.ops)).map(_ => map(id)) | |
} | |
def update(id: UserID, profile: UserProfile): Task[Unit] = | |
ref.update(x => x.copy(ops = "Updated"::x.ops)).map(_ => map = map + (id -> profile)) | |
} | |
trait LoggerTestService extends Logger with TestService { | |
override val ref: Ref[TestInstructionsState] | |
def info(id: String): Task[Unit] = ref.update(x => x.copy(ops = s"Logged - $id"::x.ops)).map(_ => ()) | |
} | |
val runtime = new DefaultRuntime {} | |
val initialRef = runtime.unsafeRun(Ref.make(TestInstructionsState(Nil))) | |
object FinalEnv extends DatabaseTestService with LoggerTestService { | |
override val ref = initialRef | |
} | |
val lookedUpProfile: ZIO[Database with Logger, Throwable, UserProfile] = | |
for { | |
profile <- ZIO.accessM[Database](x => x.lookup("abc")) | |
_ <- ZIO.accessM[Logger](x => x.info(profile.name)) | |
} yield profile | |
val v = runtime.unsafeRun(lookedUpProfile.provide(FinalEnv)) | |
runtime.unsafeRun(initialRef.get) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment