Skip to main content
Version: 2.x

Tutorial: Model a Real Configuration With Case Classes and Sealed Traits

Welcome! This tutorial is for Scala developers who have loaded a first, flat configuration with ZIO Config, for example by following Getting Started. You don't need to know anything else about ZIO Config to follow along.

Real configurations are rarely flat. They group settings, leave some out, repeat others, and offer a choice between alternatives. In Scala, we model exactly these shapes with case classes and sealed traits, which together are called algebraic data types (ADTs). ZIO Config reads such a model directly from a file.

Learning Objectivesโ€‹

By the end of this tutorial, you will understand:

  • How the shape of your Scala types decides the shape of the configuration file
  • How optional fields and default values make settings optional
  • How lists and maps are read
  • How a sealed trait lets the file choose between alternatives

We'll learn these concepts through:

  1. A flat case class
  2. Nested case classes
  3. Optional settings and defaults
  4. Lists and maps
  5. Alternatives with a sealed trait
  6. Alternatives selected by a type field

We recommend reading from top to bottom โ€” each section builds on the previous one.

Backgroundโ€‹

ZIO Config separates what a program needs from where the values come from. The what is a description of type Config[A]; the where is a ConfigProvider, such as a HOCON file. When the program's needs are expressed as Scala types, deriveConfig[A] writes the description for us: every field becomes a key, every nested case class becomes a nested object, and every sealed trait becomes a choice. Changing the type changes what the program expects from the file.

Throughout this tutorial, we build the configuration of a small notification service, one piece at a time.

1. A Flat Case Classโ€‹

Let's start with the simplest model: one case class with two fields. We also write a helper, load, that reads a HOCON string with a description and returns the result, so we can see what each step reads:

import zio._
import zio.config._
import zio.config.magnolia.deriveConfig
import zio.config.typesafe._

def load[A](config: Config[A], hocon: String): Either[Config.Error, A] =
Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe.run(ConfigProvider.fromHoconString(hocon).load(config).either).getOrThrow()
}

final case class Service(name: String, port: Int)

The code above sets up everything we need:

  • The four imports bring in ZIO, the ZIO Config syntax, derivation and the HOCON provider.
  • load builds a provider from a HOCON string, loads the description, and runs the effect so that we can print the result.
  • Service is our first model: a name and a port.

Now we derive the description and load a matching document:

load(deriveConfig[Service], """
name = "notifier"
port = 8080
""")
// res1: Either[Config.Error, Service] = Right(
// value = Service(name = "notifier", port = 8080)
// )

deriveConfig[Service] reads one key per field, named like the field. Try removing port from the document: the result becomes a Left with a MissingData error for port.

2. Nested Case Classesโ€‹

Settings that belong together go into their own case class. A field whose type is a case class reads its keys under the field's name:

final case class Smtp(host: String, port: Int)
final case class Notifier(service: Service, smtp: Smtp)

In the code above, Smtp groups the mail server settings, and Notifier holds both groups. The HOCON document mirrors the types, with one object per case class:

load(deriveConfig[Notifier], """
service { name = "notifier", port = 8080 }
smtp { host = "mail.internal", port = 25 }
""")
// res2: Either[Config.Error, Notifier] = Right(
// value = Notifier(
// service = Service(name = "notifier", port = 8080),
// smtp = Smtp(host = "mail.internal", port = 25)
// )
// )

Notice that both Service and Smtp have a field called port, and there is no clash: one is read as service.port, the other as smtp.port. The nesting of the types became the nesting of the keys.

3. Optional Settings and Defaultsโ€‹

Not every setting must be in the file. ZIO Config gives us two ways to say so, both in the type. A field of type Option[A] may be missing, and a field with a default value falls back to it:

final case class Smtp2(host: String, port: Int = 25, username: Option[String])

The code above changes our mail settings in two ways: port: Int = 25 gives the port a default value, and username: Option[String] makes the user name optional.

Now a document with only the host is enough:

load(deriveConfig[Smtp2], """host = "mail.internal"""")
// res3: Either[Config.Error, Smtp2] = Right(
// value = Smtp2(host = "mail.internal", port = 25, username = None)
// )

The port fell back to 25, and the user name is None. Defaults only cover missing keys: try port = "twenty-five", and the load fails instead of quietly using the default.

4. Lists and Mapsโ€‹

Some settings repeat. A field of type List[A] reads a HOCON array, and a field of type Map[String, A] reads every key inside an object:

final case class Routing(admins: List[String], quietHours: Map[String, Int])

The code above models two kinds of repetition: admins is a list of addresses that receive every alert, and quietHours maps a team name to the hour when its quiet time starts.

The document uses an array and an object:

load(deriveConfig[Routing], """
admins = ["ops@example.com", "cto@example.com"]
quietHours { payments = 22, search = 23 }
""")
// res4: Either[Config.Error, Routing] = Right(
// value = Routing(
// admins = List("ops@example.com", "cto@example.com"),
// quietHours = Map("payments" -> 22, "search" -> 23)
// )
// )

The list elements can be case classes too; each array element is then an object. The keys of the map come from the file, not from our code, which is what makes a map different from a case class.

5. Alternatives With a Sealed Traitโ€‹

Our service can send notifications in different ways, and each way needs different settings. A sealed trait expresses "one of these":

sealed trait Channel

object Channel {
final case class Email(to: String) extends Channel
final case class Slack(webhook: String) extends Channel
case object Disabled extends Channel
}

final case class Alerts(channel: Channel)

The code above defines three alternatives: Email and Slack each carry their own settings, and Disabled carries no settings at all.

By default, the file selects an alternative by its name. A case class alternative is an object named after the class, and a case object is just its name:

load(deriveConfig[Alerts], """channel { Slack { webhook = "https://hooks.example/abc" } }""")
// res5: Either[Config.Error, Alerts] = Right(
// value = Alerts(channel = Slack(webhook = "https://hooks.example/abc"))
// )
load(deriveConfig[Alerts], "channel = Disabled")
// res6: Either[Config.Error, Alerts] = Right(
// value = Alerts(channel = Disabled)
// )

Here is the "aha" moment: the file can only express a combination that our types allow. Try channel = Pigeon, or channel { Email {} } without the to setting: both fail to load, because no alternative matches.

6. Alternatives Selected by a Type Fieldโ€‹

Many teams prefer a flatter style, where a field such as type names the alternative and its settings sit next to it. The @discriminator annotation on the sealed trait switches to that style:

import zio.config.derivation.discriminator

@discriminator("type")
sealed trait Channel2

object Channel2 {
final case class Email(to: String) extends Channel2
final case class Slack(webhook: String) extends Channel2
case object Disabled extends Channel2
}

final case class Alerts2(channel: Channel2)

The only change from the previous section is @discriminator("type"). Now the type key selects the alternative:

load(deriveConfig[Alerts2], """
channel {
type = Email
to = "ops@example.com"
}
""")
// res7: Either[Config.Error, Alerts2] = Right(
// value = Alerts2(channel = Email(to = "ops@example.com"))
// )

Putting It Togetherโ€‹

This example combines all the concepts we've learned into the full configuration of the notification service: nested groups, an optional user name, a default port, a list, a map, and a channel chosen by a type field. It reuses Service, Routing and Channel2 from the sections above:

final case class Mail(host: String, port: Int = 25, username: Option[String])

final case class NotificationService(
service: Service,
mail: Mail,
routing: Routing,
channel: Channel2
)

val notificationService: Config[NotificationService] = deriveConfig[NotificationService]

One HOCON document describes the whole service:

load(notificationService, """
service { name = "notifier", port = 8080 }
mail { host = "mail.internal" }
routing {
admins = ["ops@example.com"]
quietHours { payments = 22 }
}
channel { type = Slack, webhook = "https://hooks.example/abc" }
""")
// res8: Either[Config.Error, NotificationService] = Right(
// value = NotificationService(
// service = Service(name = "notifier", port = 8080),
// mail = Mail(host = "mail.internal", port = 25, username = None),
// routing = Routing(
// admins = List("ops@example.com"),
// quietHours = Map("payments" -> 22)
// ),
// channel = Slack(webhook = "https://hooks.example/abc")
// )
// )

What You've Learnedโ€‹

In this tutorial, you learned:

  • That the shape of your case classes and sealed traits decides the shape of the configuration file
  • How Option fields and default values make a setting optional, and that defaults cover only missing keys
  • How List and Map fields read arrays and objects
  • How a sealed trait lets the file choose one alternative, either by name or with @discriminator

You now have a solid foundation for modelling configuration with types. The next step is to load such a model in a running ZIO application.

Where to Go Nextโ€‹

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–