Search code examples
scalaconfigconfiguration-filestypesafetypesafe-config

Typesafe: Read array of configs


My tasks.conf file looks like below

tasks = [
  {
    prop_1 = "string-1"
    prop_2 = "string-2"
    prop_3 = 3
  },

  {
    prop_1 = "string-1"
    prop_2 = "string-2"
    prop_3 = 3
  }
]

After successfully parsing the task.conf file (located outside jar / classpath) using val config: Config = ConfigFactory.parseFile(file), I've tried reading it as follows without success

  • config.getObjectList("tasks")
  • config.getConfigList("tasks")

It complains

ConfigException$Missing: No configuration setting found for key 'tasks'

Upon asserting whether or not the path exists using config.hasPath("tasks"), it returns false.

My queries are as follows:

  • Is the structure of my conf file correct? Even if it's correct, is there a better way to structure it?
  • If the structure of conf file is correct (since parseFile succeeds), then where am I going wrong in reading the file?

Solution

  • Looks like your config file could not be read! Your application is probably trying to read the default application.conf, and your tasks config key is not found there.

    You could probably try:

    ConfigSource.FromFile(path)
    

    Use the complete absolute path and give it a try!