Enumeratum Module

The ciris-enumeratum module provides support for decoding enumeratum enumerations. Enumerations are especially useful for dealing with multiple environments. Let’s take a look at an example, where we define an enumeration and try to load values of that enumeration. Enumeratum provides mixins, like Lowercase below, which can be used to customize the name of our enumeration values, and in turn, what values we will be able to decode.

import enumeratum._
import enumeratum.EnumEntry._

object environments {
  sealed abstract class AppEnvironment extends EnumEntry with Lowercase

  object AppEnvironment extends Enum[AppEnvironment] {
    case object Local extends AppEnvironment
    case object Testing extends AppEnvironment
    case object Production extends AppEnvironment

    val values = findValues
  }
}

We also define a custom configuration source, holding some values we can attempt to decode.

import ciris.{ConfigKeyType, ConfigSource}
import ciris.enumeratum._
import environments._

val source = {
  val keyType = ConfigKeyType[String]("enumeratum key")
  ConfigSource.fromMap(keyType)(Map(
    "localEnv" -> "local",
    "testingEnv" -> "testing",
    "TestingEnv" -> "Testing",
    "invalidEnv" -> "invalid"
  ))
}

Finally, we can read values from the source and decode them as enumeration values.

source.read("localEnv").decodeValue[AppEnvironment]
// res0: ciris.ConfigEntry[ciris.api.Id,String,String,environments.AppEnvironment] = ConfigEntry(localEnv, ConfigKeyType(enumeratum key), Right(local), Right(Local))

source.read("testingEnv").decodeValue[AppEnvironment]
// res1: ciris.ConfigEntry[ciris.api.Id,String,String,environments.AppEnvironment] = ConfigEntry(testingEnv, ConfigKeyType(enumeratum key), Right(testing), Right(Testing))

source.read("TestingEnv").decodeValue[AppEnvironment]
// res2: ciris.ConfigEntry[ciris.api.Id,String,String,environments.AppEnvironment] = ConfigEntry(TestingEnv, ConfigKeyType(enumeratum key), Right(Testing), Left(WrongType(TestingEnv, ConfigKeyType(enumeratum key), Right(Testing), Testing, environments$AppEnvironment)))

source.read("invalidEnv").decodeValue[AppEnvironment]
// res3: ciris.ConfigEntry[ciris.api.Id,String,String,environments.AppEnvironment] = ConfigEntry(invalidEnv, ConfigKeyType(enumeratum key), Right(invalid), Left(WrongType(invalidEnv, ConfigKeyType(enumeratum key), Right(invalid), invalid, environments$AppEnvironment)))