Providing default value on typesafe config getters

vatsal mevada picture vatsal mevada · Sep 3, 2018 · Viewed 8k times · Source

So I am having multiple occurrences of similar snippets in my code:

val optionValue = try {
      Some(config.getString(key))
    } catch {
      case _: Missing => None
    }

I want to somehow eliminate this duplicates from my code. I know that typesafe provides a way to give a fallback config file to provide default config values. However, in my case, I am not having any default values for some properties. They are optional properties.

What will be the most optimal way to refactor this code.

Answer

Thomas Decaux picture Thomas Decaux · Mar 19, 2019

This is not how the library works, according https://github.com/lightbend/config#how-to-handle-defaults.

You should use withFallback method to provide a clean configuration.