Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Scala Load Configuration With PureConfig

DZone's Guide to

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.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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!

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
types ,scala ,configurations ,java ,tutorial

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}