Skip to content

Instantly share code, notes, and snippets.

@bilal-fazlani
Last active April 7, 2022 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bilal-fazlani/546dd538f5d0fc7ca38c365654c758a7 to your computer and use it in GitHub Desktop.
Save bilal-fazlani/546dd538f5d0fc7ca38c365654c758a7 to your computer and use it in GitHub Desktop.
zio config examples with scala 3
import com.typesafe.config.Config
import zio.*
import zio.config.ConfigDescriptor.*
import zio.config.ConfigSource.*
import zio.config.*
import zio.config.magnolia.Descriptor
import zio.config.typesafe.TypesafeConfigSource
import zio.cli.Args
import java.time.ZonedDateTime
import java.time.LocalDate
import scala.util.Try
// #region manual
package ManualFromApplicationAndReferenceConf {
case class Human(name: String, age: Int)
object App extends zio.ZIOAppDefault {
val humanDescription =
((string("name") ?? "name of the human") zip (int("age") ?? "age of the human")).to[Human]
val source = TypesafeConfigSource.fromResourcePath
val desc = humanDescription from source
val data = read(desc)
override def run = for {
d <- data
_ <- Console.printLine(d)
} yield ()
}
}
// #endregion
// #region automatic
package AutomaticDerivation {
case class Human(name: String, age: Int)
val humanDescription = Descriptor[Human]
}
// #endregion
// #region fallback file
package FallbacConfFile {
case class Human(name: String, age: Int, address: String)
val humanDescription = Descriptor[Human]
object App extends zio.ZIOAppDefault {
val addressConf = ZIO.attempt(com.typesafe.config.ConfigFactory.parseResources("address.conf"))
val source =
TypesafeConfigSource.fromResourcePath <>
TypesafeConfigSource.fromTypesafeConfig(addressConf)
val desc = humanDescription from source
val data = read(desc)
override def run = {
for {
d <- data
_ <- Console.printLine(d)
} yield ()
}
}
}
// #endregion
// #region nested conf
package NestedConf {
case class Kafka(topics: List[String])
val description = Descriptor[Kafka]
object App extends zio.ZIOAppDefault {
val source = TypesafeConfigSource.fromResourcePath
val desc = nested("kafka")(description from (source))
override def run = {
for {
data <- read(desc)
_ <- Console.printLine(data)
} yield ()
}
}
}
// #endregion
// #region case class custom
package CustomDecodedTypeConf {
val hoconValue = """
kafka = {
topics = ["one", "two"]
broker: "127.0.0.1:28000"
}
"""
case class Address(host: String, port: Int) {
override def toString = s"$host:$port"
}
object Address {
given Descriptor[Address] =
Descriptor.from[Address](Descriptor[String].transformOrFailLeft(Address.parse)(_.toString))
def parse(value: String): Either[String, Address] = value match {
case s"$host:$port" if port.toIntOption.nonEmpty => Right(Address(host, port.toInt))
case e => Left(s"invalid address: $e")
}
}
case class Kafka(topics: List[String], broker: Address)
val descriptor: ConfigDescriptor[Kafka] = Descriptor[Kafka]
object App extends zio.ZIOAppDefault {
val source = TypesafeConfigSource.fromHoconString(hoconValue)
val desc = nested("kafka")(descriptor from source)
override def run = {
for {
data <- read(desc)
_ = assert(data == Kafka(List("one", "two"), Address("127.0.0.1", 28000)))
_ <- Console.printLine(data)
} yield ()
}
}
}
// #endregion
// #region opqaue type
package OpaqueCustomDecodedTypeConf {
val hoconValue = """
kafka = {
topics = ["one", "two"]
}
"""
opaque type Topic = String
given Descriptor[Topic] = Descriptor.from(ConfigDescriptor.string)
case class Kafka(topics: List[Topic])
val descriptor: ConfigDescriptor[Kafka] = Descriptor[Kafka]
object App extends zio.ZIOAppDefault {
val source = TypesafeConfigSource.fromHoconString(hoconValue)
val desc = nested("kafka")(descriptor from source)
override def run = {
for {
data <- read(desc)
_ <- Console.printLine(data)
} yield ()
}
}
}
// #endregion
name = "john"
kafka = {
topics = ["one", "two"]
broker = "127.0.0.1:27000"
}
address = "India"
age = 40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment