What to do for overriding the PureConfig behavior in Scala ?

Knoldus Inc.
Knoldus - Technical Insights
3 min readJun 15, 2017

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 :
application.conf:
[code language=”scala”]
company {
full-name = “Knoldus Software LLP”
started = 2012
employees = “80–120”
offices = [“India”, “Singapore”, “US”, “Canada”]
offices-in-india {
head-office = “Delhi”
development = “Noida”
}
}
[/code]

Reading configuration:
[code language=”scala”]
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}”)
}
[/code]

Output :
[code language=”scala”]
Knoldus Software LLP
2012
80–120
List(India, Singapore, US, Canada)
Map(development -> Noida, head-office -> Delhi)
[/code]

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:
[code language=”scala”]
implicit val overrideStrReader = ConfigReader.fromString[String](catchReadError(_.toUpperCase()))

[/code]

After adding above line, lets take a look on output :
[code language=”scala”]
KNOLDUS SOFTWARE LLP
2012
80–120
List(INDIA, SINGAPORE, US, CANADA)
Map(development -> NOIDA, head-office -> DELHI)
[/code]

View full code on github.

2. ConfigWriter:

Add below line to override the String write behavior:
[code language=”scala”]
implicit val myStringWriter = ConfigWriter.toString[String](n => s”$n !!!”)
[/code]

Write config:
[code language=”scala”]
val companyDetails = CompanyDetails(“Knoldus Software LLP”, 2012, “80–120”, Nil, Map(), None)
val conf = Company(companyDetails).toConfig
println(conf)
[/code]

output:
[code language=”scala”]
SimpleConfigObject({“company”:{“employees”:”80–120 !!!”,”full-name”:”Knoldus Software LLP !!!”,”offices”:[],”offices-in-india”:{},”started”:2012}})[/code]

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:
[code language=”scala”]
implicit val myStringConvert = ConfigConvert.viaString[String](
catchReadError(s => s.toUpperCase ), n => s”$n !!!”)
[/code]

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 !!!

KNOLDUS-advt-sticker

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com