Configuration Sources

Ciris provides support for reading environment variables and system properties. If you’re looking for a common configuration source not yet supported, please file an issue or, even better, submit a pull-request. If you require other configurations sources, you can easily define your own. You’ll find helper methods for creating custom configuration sources in the companion object of ConfigSource. Let’s illustrate this by writing a configuration source which captures the current system properties at a certain point in time, ignoring any later changes.

To create a configuration source, we need to provide a ConfigKeyType which is the name of the type of key the source reads (to support sensible error messages). We can convert the current system properties to a Map and create a ConfigSource from it using the fromMap method. If we make the source implicit and have it in scope, the read method will read ConfigValue configuration values from it. We can convert a ConfigValue[T] to an Either[ConfigError, T] by calling the value method.

import ciris._
// import ciris._

implicit val fixedProperties = {
  val keyType = ConfigKeyType[String]("fixed system property")
// fixedProperties: ciris.ConfigSource[String] = ConfigSource(ConfigKeyType(fixed system property))

// res1: ciris.ConfigValue[String] = ConfigValue(Right(UTF-8))

// res2: ciris.ConfigValue[String] = ConfigValue(Right(UTF-8))

To verify that the source does not change, let’s delete the file.encoding key, and verify that we can still read it.

// res3: Option[String] = Some(UTF-8)

// res4: ciris.ConfigValue[String] = ConfigValue(Right(UTF-8))

// res5: ciris.ConfigValue[String] = ConfigValue(Left(MissingKey(file.encoding, Property)))