Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ZIO Config Documentation #1416

Open
wants to merge 15 commits into
base: series/4.x
Choose a base branch
from
Prev Previous commit
Next Next commit
make all codes type-safe at compile-time.
  • Loading branch information
khajavi committed May 21, 2024
commit 1ba774f8d17fe8a26d9dcba1ef1a23d3f5af59a8
19 changes: 15 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,16 @@ lazy val examples = crossProject(JVMPlatform)
})
.value
)
.dependsOn(zioConfig, zioConfigMagnolia, zioConfigRefined, zioConfigTypesafe, zioConfigYaml)
.dependsOn(
zioConfig,
zioConfigMagnolia,
zioConfigRefined,
zioConfigTypesafe,
zioConfigYaml,
zioConfigEnumeratum,
zioConfigScalaz,
zioConfigCats
)

lazy val examplesJVM = examples.jvm

Expand Down Expand Up @@ -436,8 +445,7 @@ lazy val docs = project
zioConfigTypesafeJVM,
zioConfigDerivationJVM,
zioConfigYamlJVM,
zioConfigRefinedJVM,
zioConfigMagnoliaJVM
zioConfigRefinedJVM
)
)
.settings(macroDefinitionSettings)
Expand All @@ -447,6 +455,9 @@ lazy val docs = project
zioConfigDerivationJVM,
zioConfigYamlJVM,
zioConfigRefinedJVM,
zioConfigMagnoliaJVM
zioConfigMagnoliaJVM,
zioConfigEnumeratumJVM,
zioConfigScalazJVM,
zioConfigCatsJVM
)
.enablePlugins(WebsitePlugin)
1 change: 0 additions & 1 deletion docs/automatic-validations.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Take a look at `zio.config.refined` package.
import zio.Config
import zio.ConfigProvider
import zio.config._, refined._

```

A few examples are given below.
Expand Down
104 changes: 44 additions & 60 deletions docs/integrations.md
Original file line number Diff line number Diff line change
@@ -1,89 +1,73 @@
---
id: integrations
title: "Integrations"
sidebar_label: Integrations
title: "Integrations with other Libraries"
---

`zio-config` is also integrated with `enumeratum`, `cats`, `scalaz`, `aws-sdk`, `zio-aws`, `refined` etc. Note that only a few of them is documented here. `refined` is already discussed under `automatic-validations`.

## Integration with other libraries
## Enumeratum

`zio-config` is also integrated with `enumeratum`, `cats`, `scalaz`, `aws-sdk`, `zio-aws`, `refined` etc.
Note that only a few of them is documented here. `refined` is already discussed under `automatic-validations`.
Many applications rely on [this beautiful library](https://github.com/lloydmeta/enumeratum). ZIO Config can directly load it from enumeratum's `enum` without relying on auto-derivation (and rely on Enumeratum's macro indirectly with additional features):

#### Enumeratum
```scala mdoc:compile-only
import zio._
import enumeratum.{Enum, EnumEntry}
import zio.config.enumeratum._

Many applications rely on this beautiful library https://github.com/lloydmeta/enumeratum.
Zio-config can directly load it from enumeratum's `enum` without relying on auto-derivation (and rely on Enumeratum's macro indirectly witha additional features).
sealed trait Greeting extends EnumEntry

```scala
object Greeting extends Enum[Greeting] {
val values = findValues

sealed trait Greeting extends EnumEntry
case object Hello extends Greeting
case object GoodBye extends Greeting
case object Hi extends Greeting
case object Bye extends Greeting
}

object Greeting extends Enum[Greeting] {
val mapProvider =
ConfigProvider.fromMap(Map(
"greeting" -> "Hello"
))

val values = findValues

case object Hello extends Greeting
case object GoodBye extends Greeting
case object Hi extends Greeting
case object Bye extends Greeting

}


// Load using zio-config
import zio.config.enumeratum._

val mapProvider =
ConfigProvider.fromMap(Map(
"greeting" -> "Hello"
))

val config =
`enum`(Greeting).nested("greeting")

val pgm: IO[Error, Greeting] =
mapProvider.load(config)

// Returns Hello

val config =
`enum`(Greeting).nested("greeting")

val pgm: IO[Config.Error, Greeting] = mapProvider.load(config)
// Returns Hello
```

#### Scalaz/Cats
## Scalaz/Cats

Highly polymorphic code end up relying on
typeclasses, and zio-config provides instances for `Config`.
Highly polymorphic code end up relying on typeclasses, and ZIO Config provides instances for `Config`.

This is a simple example to showcase the capability.

```scala
```scala mdoc:compile-only
import zio._
import _root_.scalaz._, Scalaz._
import zio.config.scalaz.instances._

import _root_.scalaz._, Scalaz._
import zio.config.scalaz.instances._

// Across the application, there can be various effect types, but there is only one addition!
def add[F[_]: Applicative, A: Monoid](primary: F[A], secondary: F[A]): F[A] =
primary.<*>(Applicative[F].map(secondary)(secondary => (primary: A) => primary.mappend(secondary)))

// Now even `Config` can take part in this addition given the values of config parameters should be Monoid,
// instead of using native `zip` and separately implementing addition for various types
val configResult = add(Config.int("marks1"), Config.int("marks2")))

ConfigProvider.fromMap(Map("marks1" -> "10", "marks2" -> "20")).load(configResult) // returns 30

// Across the application, there can be various effect types, but there is only one addition!
def add[F[_]: Applicative, A: Monoid](primary: F[A], secondary: F[A]): F[A] =
primary.<*>(Applicative[F].map(secondary)(secondary => (primary: A) => primary.mappend(secondary)))

// Now even `Config` can take part in this addition given the values of config parameters should be Monoid,
// instead of using native `zip` and separately implementing addition for various types
val configResult = add(Config.int("marks1"), Config.int("marks2"))

ConfigProvider.fromMap(Map("marks1" -> "10", "marks2" -> "20")).load(configResult) // returns 30
```

In addition to it, it can also load cats/scalaz specific datatypes

```scala

import zio.config.scalaz._
import _root_.scalaz.Maybe
```scala mdoc:compile-only
import zio._
import zio.config.scalaz._
import _root_.scalaz.Maybe


val config: Config[Maybe[Int]] = maybe(Config.int("age"))

val config: Config[Maybe[Int]] = maybe(Config.int("age"))
```

Have a look at modules of zio-config to know about other integrations such as `aws`, `zio-aws` etc
9 changes: 0 additions & 9 deletions docs/read-from-various-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ val hoconSource =
url : bla
region: useast
}

"""
)


val anotherHoconSource =
ConfigProvider.fromHoconString(
"""
Expand Down Expand Up @@ -84,7 +82,6 @@ val jsonString =
"url" : "bla"
"region": "useast"
}

"""

ConfigProvider.fromHoconString(jsonString)
Expand All @@ -98,7 +95,6 @@ Similar to Hocon source, we have `ConfigProvider.fromYamlString`
import zio.config.yaml._

ConfigProvider.fromYamlString

```

## Xml String
Expand Down Expand Up @@ -138,7 +134,6 @@ val config =
|""".stripMargin

val parsed = ConfigProvider.fromYamlString(config).load(Configuration.config)

```


Expand Down Expand Up @@ -179,11 +174,8 @@ val map =
"employees[1].name" -> "foo",
"employees[1].departments" -> "<nil>",
)


ConfigProvider.fromMap(map).load(derivedConfig[Config])


```

Although we support indexing within Flat, formats such as Json/HOCON/XML is far better to work with indexing,
Expand Down Expand Up @@ -212,5 +204,4 @@ final case class Employee(age: Int, name: String)
val provider = ConfigProvider.fromMap(map)
val config = Config.listOf("employees", deriveConfig[Employee]).nested("department")
val result = provider.load(config)

```
Loading