DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • Dear Micromanager: Your Distrust Has a Job; It’s Just Not the One You’re Doing
  • What Is Lambda Architecture? Ultimate Guide to Getting Started
  • Throughput vs Goodput: The Performance Metric You Are Probably Ignoring in LLM Testing
  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  1. DZone
  2. Coding
  3. Languages
  4. 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.

By 
Rishi Khandelwal user avatar
Rishi Khandelwal
·
Updated Jun. 14, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
22.8K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook