Search code examples
scalapureconfig

Pureconfig Typesafe Config with Hierarchical Root Keys


I have the below in application.conf and trying to figure out the best way to define my class class to load the configurations:

allKeys {
   mysql {
     dev {
       host = <host1>
       user = <user1>
     }
     prod {
       host = <host1>
       user = <user1>
   }
   hdfs {
     endpoint = <host1>
     port = <port1>
   }
}

my case classes:
  case class Settings(mysql: DbSettings, hdfs: HdfsSettings)
  case class DbSettings(host: String, user: String)
  case class HdfsSettings(endpoint: String, port: String)

I'm having issues with knowing how to properly load this so that it doesn't fail when it looks for similar keys in hdfs.


Solution

  • You need to define your case class's to fit the config structure.

    case class HdfsConfig(endpoint: String, port: Int)
    case class DbConfig(host: String, user: String)
    case class MySqlConfig(dev: DbConfig, prod: DbConfig)
    case class AllConfigs(mysql: MySqlConfig, hdfs: HdfsConfig)
    
    case class MyConfig(allKeys: AllConfigs)
    

    Now you can read these as,

    loadConfig[MyConfig](conf)