Last active
April 29, 2023 20:50
-
-
Save dacr/8ca4d6ac0a82083a1a3af0d0e98892b3 to your computer and use it in GitHub Desktop.
ZIO learning - using standard ZIO config system / published by https://github.com/dacr/code-examples-manager #52b2d4e4-116b-4eb6-be42-909b5a8090ce/a290ec89a2de9e40911c087478976bacb0a5c0a
This file contains 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 | |
// keywords : scala, zio, learning, pure-functional, 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 : 52b2d4e4-116b-4eb6-be42-909b5a8090ce | |
// created-on : 2023-04-14T18:40:53+02: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 "dev.zio::zio-config:4.0.0-RC14" | |
// --------------------- | |
import zio.* | |
import zio.config.* | |
case class DatabaseConfig( | |
name: String, | |
host: String, | |
port: Int | |
) | |
object DatabaseConfig { | |
val config: Config[DatabaseConfig] = | |
(Config.string("name").withDefault("example") ++ | |
Config.string("host").withDefault("127.0.0.1") ++ | |
Config.int("port").withDefault(42).validate("port must be > 0 and < 65535")(v => v > 0 && v < 65535)) | |
.to[DatabaseConfig] | |
} | |
object Encapsulated extends ZIOAppDefault { | |
// ------------------------------------------------------------- | |
val logic = for { | |
myConf <- ZIO.config(DatabaseConfig.config.nested("database")) | |
_ <- Console.printLine(myConf) | |
_ <- Console.printLine("Customizable using properties database.name, database.host & database.port") | |
_ <- Console.printLine("Customizable using environment variables DATABASE_NAME, DATABASE_HOST & DATABASE_PORT") | |
} 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