Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/3c80ee59cc8346df175cd23ced0a744d to your computer and use it in GitHub Desktop.
Save dacr/3c80ee59cc8346df175cd23ced0a744d to your computer and use it in GitHub Desktop.
ZIO learning - defining and injecting an effect based custom config layer into the environment / published by https://github.com/dacr/code-examples-manager #3cc42c59-4c46-4917-a37e-2df21f55027c/26359feba7e8e4e384f7266835a6794148966012
// summary : ZIO learning - defining and injecting an effect based custom config layer into the environment
// keywords : scala, zio, learning, pure-functional, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 3cc42c59-4c46-4917-a37e-2df21f55027c
// created-on : 2022-01-11T15:14:53+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.2.2"
//> using dep "dev.zio::zio:2.0.13"
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0"
// ---------------------
import zio.*, zio.worksheet.*
// -------------------------------------------------------------
case class AuthConfig(username: String, password: String)
case class Config(auth: AuthConfig)
val configLogic = for {
username <- System.envOrElse("APP_USERNAME", "admin")
password <- System.envOrElse("APP_PASSWORD", "admin")
auth = AuthConfig(username, password)
} yield Config(auth)
val configLayer = ZLayer(configLogic)
// -------------------------------------------------------------
val logic: ZIO[Config, Exception, Unit] = for {
auth <- ZIO.environment[Config].map(_.get.auth)
_ <- Console.printLine(s"Hello ${auth.username} !")
} yield ()
// -------------------------------------------------------------
logic.provideLayer(configLayer).unsafeRun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment