diff --git a/docs/defining-config-descriptors.md b/docs/defining-config-descriptors.md new file mode 100644 index 000000000..c29e659c3 --- /dev/null +++ b/docs/defining-config-descriptors.md @@ -0,0 +1,75 @@ +--- +id: defining-config-descriptors +title: "Defining Config Descriptors" +--- + +ZIO Config uses the `Config[A]` to describe the configuration of type `A`, which is part of ZIO core library. So before diving into ZIO Config, we need to understand the `Config[A]` data type. There is a [dedicated section](https://zio.dev/reference/configuration/) in the ZIO documentation that explains what are config descriptors and how we can create them. + +There are two ways to create ZIO config descriptors: +1. **Describing Configuration Descriptors Manually** — We can manually create a configuration descriptor using the `Config` data type and its compositional operators. +2. **Auto-derivation of Configuration Descriptors** — We can derive a configuration descriptor for a case class or sealed trait using the `zio-config-magnolia` module. + +Let's talk about both of these methods in detail. + +## Describing Configuration Descriptors Manually + +We must fetch the configuration from the environment to a case class (product) in scala. Let it be `MyConfig` + +```scala mdoc:silent +case class MyConfig(ldap: String, port: Int, dburl: String) +``` + +To perform any action using ZIO Config, we need a configuration description. Let's define a simple one. To generate a `Config[MyConfig]` we can first generate tuples of the primitive configurations like `string`, `int`, etc using the `zip` operator, then map them to their respective case class: + +```scala mdoc:silent +import zio._ +import zio.config._ +import zio.ConfigProvider +import zio.Config, Config._ + +object MyConfig { + val config: Config[MyConfig] = (string("LDAP") zip int("PORT") zip string("DB_URL")).to[MyConfig] +} +``` + +There are several other combinators which can be used to describe the configuration. To learn more please refer to the ZIO core reference section for [configuration](https://zio.dev/reference/configuration/). + +## Auto-derivation of Config Descriptors + +If we don't like describing our configuration manually, we can use the `zio-config-magnolia` module to derive the configuration descriptor for a case class or a sealed trait. Let's add this module to our `build.sbt` file: + +```scala +libraryDependencies += "dev.zio" %% "zio-config-magnolia" % "@VERSION@" +``` + +By importing the `zio.config.magnolia._` package, we can derive the configuration descriptor for a case class or a sealed trait using the `deriveConfig` method: + +```scala mdoc:silent:nest +import zio.config._ +import zio.config.magnolia._ + +case class MyConfig(ldap: String, port: Int, dburl: String) + +object MyConfig { + implicit val config: Config[MyConfig] = deriveConfig[MyConfig] +} +``` + +## Read config from various sources + +There are more information on various sources in [here](read-from-various-sources.md). + +Below given is a simple example. + +```scala mdoc:silent +val map = + Map( + "LDAP" -> "xyz", + "PORT" -> "8888", + "DB_URL" -> "postgres" + ) + +val source = ConfigProvider.fromMap(map) + +source.load(MyConfig.config) +``` diff --git a/docs/dive-into-zio-config.md b/docs/dive-into-zio-config.md index cda00e9b2..636ef6f8c 100644 --- a/docs/dive-into-zio-config.md +++ b/docs/dive-into-zio-config.md @@ -3,76 +3,6 @@ id: dive-into-zio-config title: "Dive Into ZIO Config" --- -ZIO Config uses the `Config[A]` to describe the configuration of type `A`, which is part of ZIO core library. So before diving into ZIO Config, we need to understand the `Config[A]` data type. There is a [dedicated section](https://zio.dev/reference/configuration/) in the ZIO documentation that explains what are config descriptors and how we can create them. - -There are two ways to create ZIO config descriptors: -1. **Describing Configuration Descriptors Manually** — We can manually create a configuration descriptor using the `Config` data type and its compositional operators. -2. **Auto-derivation of Configuration Descriptors** — We can derive a configuration descriptor for a case class or sealed trait using the `zio-config-magnolia` module. - -Let's talk about both of these methods in detail. - -## Describing Configuration Descriptors Manually - -We must fetch the configuration from the environment to a case class (product) in scala. Let it be `MyConfig` - -```scala mdoc:silent -case class MyConfig(ldap: String, port: Int, dburl: String) -``` - -To perform any action using ZIO Config, we need a configuration description. Let's define a simple one. To generate a `Config[MyConfig]` we can first generate tuples of the primitive configurations like `string`, `int`, etc using the `zip` operator, then map them to their respective case class: - -```scala mdoc:silent -import zio._ -import zio.config._ -import zio.ConfigProvider -import zio.Config, Config._ - -object MyConfig { - val config: Config[MyConfig] = (string("LDAP") zip int("PORT") zip string("DB_URL")).to[MyConfig] -} -``` - -There are several other combinators which can be used to describe the configuration. To learn more please refer to the ZIO core reference section for [configuration](https://zio.dev/reference/configuration/). - -## Auto-derivation of Config Descriptors - -If we don't like describing our configuration manually, we can use the `zio-config-magnolia` module to derive the configuration descriptor for a case class or a sealed trait. Let's add this module to our `build.sbt` file: - -```scala -libraryDependencies += "dev.zio" %% "zio-config-magnolia" % "@VERSION@" -``` - -By importing the `zio.config.magnolia._` package, we can derive the configuration descriptor for a case class or a sealed trait using the `deriveConfig` method: - -```scala mdoc:silent:nest -import zio.config._ -import zio.config.magnolia._ - -case class MyConfig(ldap: String, port: Int, dburl: String) - -object MyConfig { - implicit val config: Config[MyConfig] = deriveConfig[MyConfig] -} -``` - -## Read config from various sources - -There are more information on various sources in [here](read-from-various-sources.md). - -Below given is a simple example. - -```scala mdoc:silent -val map = - Map( - "LDAP" -> "xyz", - "PORT" -> "8888", - "DB_URL" -> "postgres" - ) - -val source = ConfigProvider.fromMap(map) - -source.load(MyConfig.config) -``` ## Accumulating all errors @@ -136,6 +66,7 @@ Now on, the only way to change keys is as follows: ### CollectAll ```scala mdoc:compile-only +import zio._ import zio.config._ final case class Variables(variable1: Int, variable2: Option[Int]) @@ -151,6 +82,7 @@ import zio.config._ ### orElseEither && Constant ```scala mdoc:compile-only +import zio._ import zio.config._ sealed trait Greeting diff --git a/docs/sidebars.js b/docs/sidebars.js index 3f4f1cbef..2f9f7f38c 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -8,6 +8,7 @@ const sidebars = { items: [ "index", "dive-into-zio-config", + "defining-config-descriptors" "auto-generation-of-config-documentation", "integrations", "automatic-derivation-of-config",