Skip to content

Commit

Permalink
Update zio examples in README (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
barambani authored Nov 25, 2022
1 parent 5dfc3a1 commit f538f1e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 47 deletions.
77 changes: 46 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ To create instances for `ZIO` some useful constructors can be found [here](./zio
```scala
// Case 1: from a possible config
val logNameLiveFromConfig: ULayer[ZLogName] =
aConfigLive >>> ZLayer.fromFunctionM { env =>
ZIO.succeed(LogName(env.get[AConfig].logName))
}
aConfigLive >>> ZLayer(ZIO.service[AConfig].map(c => LogName(c.logName)))

val log4sCase1: TaskLayer[ZLogWriter] =
logNameLiveFromConfig >>> log4sLayerFromName
Expand Down Expand Up @@ -182,60 +180,77 @@ val scribeCase3: TaskLayer[ZLogWriter] =
*full compiling example [here](./zio/src/test/scala/ReadmeRioConstructionCodeSnippetsTest.scala)*
```scala
// Case 1: from a possible config in a Layer (gives a Layer)
val log4sCase1: RLayer[Has[AConfig], ZLogWriter] =
ZLayer.fromServiceM { config =>
log4sFromName.provide(config.logName)
}
val scribe4sCase1: RLayer[Has[AConfig], ZLogWriter] =
ZLayer.fromServiceM { config =>
scribeFromName.provide(config.logName)
}
val log4sCase1: RLayer[AConfig, ZLogWriter] =
ZLayer(ZIO.serviceWithZIO { c =>
log4sFromName.provideEnvironment(ZEnvironment(c.logName))
})
val scribe4sCase1: RLayer[AConfig, ZLogWriter] =
ZLayer(ZIO.serviceWithZIO { c =>
scribeFromName.provideEnvironment(ZEnvironment(c.logName))
})

// Case 2: from a name
val log4sCase2: Task[Unit] =
(log4sFromName >>> someZioProgramUsingLogs) provide aLogName

log4sFromName.provideEnvironment(ZEnvironment(aLogName)).flatMap { logger =>
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logger))
}
val scribeCase2: Task[Unit] =
(scribeFromName >>> someZioProgramUsingLogs) provide aLogName
scribeFromName.provideEnvironment(ZEnvironment(aLogName)).flatMap { logger =>
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logger))
}

// Case 3: from a logger
val log4sCase3: Task[Unit] =
Task.effect(l4s.getLogger(aLogName)) >>= { logger =>
(log4sFromLogger >>> someZioProgramUsingLogs) provide logger
}
for {
logger <- ZIO.attempt(l4s.getLogger(aLogName))
logWriter <- log4sFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()
val julCase3: Task[Unit] =
Task.effect(jul.Logger.getLogger(aLogName)) >>= { logger =>
(julFromLogger >>> someZioProgramUsingLogs) provide logger
}
for {
logger <- ZIO.attempt(jul.Logger.getLogger(aLogName))
logWriter <- julFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()
val scribeCase3: Task[Unit] =
Task.effect(scribe.Logger(aLogName)) >>= { logger =>
(scribeFromLogger >>> someZioProgramUsingLogs) provide logger
}
for {
logger <- ZIO.attempt(scribe.Logger(aLogName))
logWriter <- scribeFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()

// Case 4: from a class
val log4sCase4: Task[Unit] = {
case class LoggerClass()
(log4sFromClass >>> someZioProgramUsingLogs) provide classOf[LoggerClass]
case class LoggerClass();
log4sFromClass.provideEnvironment(ZEnvironment(classOf[LoggerClass])).flatMap { logger =>
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logger))
}
}
val scribeCase4: Task[Unit] = {
case class LoggerClass()
(scribeFromClass >>> someZioProgramUsingLogs) provide classOf[LoggerClass]
case class LoggerClass();
scribeFromClass.provideEnvironment(ZEnvironment(classOf[LoggerClass])).flatMap { logger =>
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logger))
}
}

// Case 5 (Jul): from global logger object
val julCase5: Task[Unit] =
julGlobal >>> someZioProgramUsingLogs
julGlobal.flatMap(logger =>
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logger))
)

// Case 6: console logger
val console1: Task[Unit] =
someZioProgramUsingLogs provide consoleLog
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(consoleLog))

val console2: Task[Unit] =
someZioProgramUsingLogs provide consoleLogUpToLevel(LogLevels.Warn)
someZioProgramUsingLogs.provideEnvironment(
ZEnvironment(consoleLogUpToLevel(LogLevels.Warn))
)

// Case 7: No-op logger
val noOp: Task[Unit] =
someZioProgramUsingLogs provide noOpLog
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(noOpLog))
```


Expand Down
34 changes: 18 additions & 16 deletions zio/src/test/scala/ReadmeRioConstructionCodeSnippetsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,30 @@ import scala.annotation.nowarn
log4sFromName.provideEnvironment(ZEnvironment(aLogName)).flatMap { logger =>
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logger))
}

val scribeCase2: Task[Unit] =
scribeFromName.provideEnvironment(ZEnvironment(aLogName)).flatMap { logger =>
someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logger))
}

// Case 3: from a logger
val log4sCase3: Task[Unit] = for {
logger <- ZIO.attempt(l4s.getLogger(aLogName))
logWriter <- log4sFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()
val julCase3: Task[Unit] = for {
logger <- ZIO.attempt(jul.Logger.getLogger(aLogName))
logWriter <- julFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()
val scribeCase3: Task[Unit] = for {
logger <- ZIO.attempt(scribe.Logger(aLogName))
logWriter <- scribeFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()
val log4sCase3: Task[Unit] =
for {
logger <- ZIO.attempt(l4s.getLogger(aLogName))
logWriter <- log4sFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()
val julCase3: Task[Unit] =
for {
logger <- ZIO.attempt(jul.Logger.getLogger(aLogName))
logWriter <- julFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()
val scribeCase3: Task[Unit] =
for {
logger <- ZIO.attempt(scribe.Logger(aLogName))
logWriter <- scribeFromLogger.provideEnvironment(ZEnvironment(logger))
_ <- someZioProgramUsingLogs.provideEnvironment(ZEnvironment(logWriter))
} yield ()

// Case 4: from a class
val log4sCase4: Task[Unit] = {
Expand Down

0 comments on commit f538f1e

Please sign in to comment.