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 > Scala Load Configuration With PureConfig

Scala Load Configuration With PureConfig

PureConfig is a nifty library that serves as a front-end for other libraries. Its real strength is as a boilerplate-free way of loading Scala config files.

Rishi Khandelwal user avatar by
Rishi Khandelwal
·
Jun. 14, 17 · Java Zone · Tutorial
Like (3)
Save
Tweet
21.17K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, we will discuss a better way of loading the configuration of certain types. Nowadays, we use Typesafe config for the same purpose. PureConfig also uses Typesafe config internally but also provides the better way of doing this.

PureConfig is not a configuration library. It can be seen as a better front-end for existing libraries. It uses the Typesafe Config library for loading raw configurations and then uses the raw configurations to do its magic.

The goal of PureConfig is to create, at compile-time, the boilerplate necessary to load a configuration of a certain type. In other words, you define what to load and PureConfig provides how to load it.

To use the PurConfig, we need to add the dependency in build.sbt for scala 2.11, 2.12:

"com.github.pureconfig" %% "pureconfig" % "0.7.2"


Now define the config in the application.conf file:

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


As you can see above, there are different types of values present: List, Map, String, Int.

Now define the Scala types for the config to be converted:

case class Company(company: CompanyDetails)

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


By analyzing the config and the case class, you will notice that field’s name in the config is in Kebab case (full-name), while in the case class it is in camel case (fullName). This is the default behavior. We will see the customized behavior for defining a field’s name in some upcoming posts.

Now load the config:

import pureconfig.error.ConfigReaderFailures
import pureconfig.loadConfig

val simpleConfig: Either[ConfigReaderFailures, Company] = loadConfig[Company]

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}")
  }


That’s it! Hope you enjoyed the read! If you want to explore more, you can get the full code here.

Happy Config'ing!

Scala (programming language)

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

  • Maven Tutorial: Nice and Easy [Video]
  • After COVID, Developers Really Are the New Kingmakers
  • Java: Why Core-to-Core Latency Matters
  • Open Source Security Risks

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