DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Overriding PureConfig Behavior in Scala

Overriding PureConfig Behavior in Scala

PureConfig allows devs more control over configuring Scala output. Learn how to use it to tweak your reading and writing capabilities.

Rishi Khandelwal user avatar by
Rishi Khandelwal
·
Jun. 16, 17 · Java Zone · Tutorial
Like (1)
Save
Tweet
7.35K Views

Join the DZone community and get the full member experience.

Join For Free

PureConfig has its own predefined behavior for reading and writing to configuration files, but sometimes we get a tricky requirement where 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 three override types.

ConfigReader

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

application.conf:

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
2012
80-120
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 say we want it so that Strings are always read in upper case. For this, we need to define a custom ConfigReader instance for String:

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


After adding the above line, let's take a look at the output:

KNOLDUS SOFTWARE LLP
2012
80-120
List(INDIA, SINGAPORE, US, CANADA)
Map(development -> NOIDA, head-office -> DELHI)


View the full code on GitHub.

ConfigWriter

Add the line below 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
println(conf)


Output:

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 the full code on GitHub

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 anywhere one of them is required.

View full code on GitHub

That’s it! Hope you enjoy the reading.

Happy coding!

Strings Scala (programming language) Data Types GitHub Coding (social sciences) Requirement

Published at DZone with permission of Rishi Khandelwal, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java: Why Core-to-Core Latency Matters
  • Querying Kafka Topics Using Presto
  • Open Source Security Risks
  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo