What to do for overriding the PureConfig behavior in Scala ?

PureConfig has its own predefined behavior for reading and writing to the configuration files, but sometimes we got the tricky requirement in which we need some specific behavior; for example to read the config.

It is possible to override the behavior of PureConfig for a certain type by implementing another instance of ConfigReader, ConfigWriter or ConfigConvert. So in this blog we will discuss all 3 override types.

1. ConfigReader:
The default behavior of PureConfig for String is to return the string itself in the configuration.
For example, when below configuration will be read by PureConfig, it will be read as it is :

company {
  full-name = "Knoldus Software LLP"
  started = 2012
  employees = "80-120"
  offices = ["India", "Singapore", "US", "Canada"]
  offices-in-india {
    head-office = "Delhi"
    development = "Noida"

Reading configuration:

import pureconfig.error.ConfigReaderFailures
import pureconfig.loadConfig

class SimpleConfig {
  def getConfig: Either[ConfigReaderFailures, Company] = loadConfig[Company]

case class Company(company: CompanyDetails)

case class CompanyDetails(fullName: String,
                          started: Int,
                          employees: String,
                          offices: List[String],
                          officesInIndia: Map[String, String],
                          extraActivity: Option[String])

// Calling and displaying the configuration
val simpleConfig = new SimpleConfig().getConfig

  simpleConfig match {
    case Left(ex) => ex.toList.foreach(println)

    case Right(config) => 
      println(s"Company's Name ${config.company.fullName}")
      println(s"Company started at ${config.company.started}")
      println(s"Company's strength is ${config.company.employees}")
      println(s"Company's presence are in  ${config.company.offices}")
      println(s"Company's office in India are  ${config.company.officesInIndia}")
      println(s"Company's extra activity is  ${config.company.extraActivity}")

Output :

Knoldus Software LLP
List(India, Singapore, US, Canada)
Map(development -> Noida, head-office -> Delhi)

This is the default behavior of ConfigReader in which the output is same as it is defined in configuration file.

Now let’s try to override the above behavior. Now we want that Strings are always read upper case. For this, we need to define custom ConfigReader instance for String:

  implicit val overrideStrReader = ConfigReader.fromString[String](catchReadError(_.toUpperCase()))

After adding above line, lets take a look on output :

Map(development -> NOIDA, head-office -> DELHI)

View full code on github.

2. ConfigWriter:

Add below line to override the String write behavior:

  implicit val myStringWriter = ConfigWriter.toString[String](n => s"$n !!!")

Write config:

val companyDetails = CompanyDetails("Knoldus Software LLP", 2012, "80-120", Nil, Map(), None)
    val conf = Company(companyDetails).toConfig


SimpleConfigObject({"company":{"employees":"80-120 !!!","full-name":"Knoldus Software LLP !!!","offices":[],"offices-in-india":{},"started":2012}})

You can see above that “!!!” has been appended to the each string value.

View full code on github

3. ConfigConvert:

If you want to define both operations, the easier way to add full support for a class is by creating a ConfigConvert:

implicit val myStringConvert = ConfigConvert.viaString[String](
  catchReadError(s => s.toUpperCase ), n => s"$n !!!")

A ConfigConvert is both an instance of ConfigReader and an instance of ConfigWriter, so it can be used everywhere one of them is required.

View full code on github

That’s it. Hope you enjoy the reading.

Happy Blogging !!!

Happy Coding !!!


About Rishi Khandelwal

Lead Consultant having more than 6 years industry experience. He has working experience in various technologies such as Scala, Java8, Play, Akka, Lagom, Spark, Hive, Kafka, Cassandra, Akka-http, Akka-Streams, ElasticSearch, Backbone.js, html5, javascript, Less, Amazon EC2, WebRTC, SBT
This entry was posted in Agile, Best Practices, big data, knoldus, Reactive, Scala. Bookmark the permalink.

One Response to What to do for overriding the PureConfig behavior in Scala ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s