Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 28, 2023 06:45
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/a17b78ac03c6073dd7f9016baf7875b2 to your computer and use it in GitHub Desktop.
Save dacr/a17b78ac03c6073dd7f9016baf7875b2 to your computer and use it in GitHub Desktop.
ZIO learning - application configuration revisited / published by https://github.com/dacr/code-examples-manager #bfdc2ecc-d046-48fd-9686-8dbe40ee02cc/72ebe9d0498fa117b8487613bf69b0f27ae2bc4f
// summary : ZIO learning - application configuration revisited
// keywords : scala, zio, learning, pure-functional, kebacase, typesafeconfig, config, @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 : bfdc2ecc-d046-48fd-9686-8dbe40ee02cc
// created-on : 2021-03-29T19:16:09Z
// 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 "dev.zio::zio-config:4.0.0-RC14"
//> using dep "dev.zio::zio-config-typesafe:4.0.0-RC14"
//> using dep "dev.zio::zio-config-magnolia:4.0.0-RC14"
// ---------------------
import zio.*
import zio.config.*
import zio.config.typesafe.*
import zio.config.magnolia.*
case class Truc(
name: String,
myParam: Int,
bidule: Option[Double]
)
case class MyConfig(
truc: Truc
)
object Encapsulated extends ZIOAppDefault {
// -------------------------------------------------------------
val hoconConfigSource =
ConfigProvider.fromHoconString(
input = s"""truc {
| name = blah
| my-param = 42
|// bidule = 42.0
|}
|""".stripMargin
)
// -------------------------------------------------------------
val config = for {
config <- hoconConfigSource.load(deriveConfig[MyConfig].mapKey(toKebabCase)) // read(descriptor[MyConfig].mapKey(toKebabCase) from hoconConfigSource)
} yield config
// -------------------------------------------------------------
val logic = for {
config <- ZIO.service[MyConfig]
_ <- Console.printLine(config.truc.name + " " + config.truc.myParam + " " + config.truc.bidule)
} yield ()
// -------------------------------------------------------------
val configLayer = ZLayer.fromZIO(config)
// -------------------------------------------------------------
override def run = {
logic.provideLayer(configLayer)
}
}
Encapsulated.main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment