Last active
May 25, 2024 10:20
-
-
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/43d9c61dcc59b8fd2936f6f25e7c5cdf54249c6e
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
// 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.4.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