I was using the typesafe config and I noticed if I called getString it would not return an option.
The play configuration which uses the typesafe config always returns Options.
Is this just to be more scala like?
Also, is it possible to do this using play's configuration:
val c = config.atPath("myapp-prefix")
c.getString("some-key")
I liked how with typesafe config I could jump to a specific section in my config and start referencing everything from there.
Because I just want to crash my app if it didn't load the config correctly, I don't need to deal with options.
If you have a look at Play's Configuration class, you'll see it's just a thin layer above typesafe config that checks if a key exists and returns an option instead of an exception.
It's the scala way of avoiding exceptions, and allows you to give sane defaults to your config.
If you have a look at the scaladoc, there is a underlying
field that gives you access to the typesafe config object.
If you want to make your application "crash" in case of bad config, just use that one.
Otherwise, you could also use for comprehension to retrieve your config and return an error message in case of missing keys; something like:
for {
subConfig <- config.getConfig("myKey")
cfg1 <- subConfig.getString("k1")
cfg2 <- subConfig.getString("k2")
} yield {
... insert here the code using cfg1 and cfg2 ...
}
If a key is missing, that for comprehension will return you a None
value.