Last active
May 25, 2024 10:18
-
-
Save dacr/d2d2959dce6fe52a8ed2bf356d6c2559 to your computer and use it in GitHub Desktop.
ZIO learning - using standard ZIO config system with external provider / published by https://github.com/dacr/code-examples-manager #8c63c270-2be2-4bdf-ae7d-856768bb3a89/12255d5456e81868fdbf76f09c7f1d30c53bd2b7
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 - using standard ZIO config system with external provider | |
// keywords : scala, zio, learning, pure-functional, 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 : 8c63c270-2be2-4bdf-ae7d-856768bb3a89 | |
// created-on : 2023-04-28T08:23:40+02:00 | |
// 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 DatabaseConfig( | |
name: String, | |
host: String, | |
port: Int | |
) | |
object DatabaseConfig { | |
val config: Config[DatabaseConfig] = deriveConfig[DatabaseConfig].mapKey(toKebabCase).nested("database") | |
} | |
object Encapsulated extends ZIOAppDefault { | |
// ------------------------------------------------------------- | |
val configProvider = | |
ConfigProvider.fromHoconString( | |
input = s"""database { | |
| name = exampleDB | |
| host = localhost | |
| port = 43 | |
|} | |
|""".stripMargin | |
) | |
override val bootstrap: ZLayer[ZIOAppArgs, Any, Any] = | |
Runtime.setConfigProvider(configProvider) | |
// ------------------------------------------------------------- | |
val logic = for { | |
myConf <- ZIO.config(DatabaseConfig.config) | |
_ <- Console.printLine(myConf) | |
} yield () | |
// ------------------------------------------------------------- | |
override def run = { | |
logic | |
} | |
} | |
Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment