I'm using Typesafe config & have a config file in my resources directory which looks like this:
something { another { someconfig=abc anotherconfig=123 } }
How would I change the value of anotherconfig
using scala?
If you want to change the loaded config (i.e. create a new config based on the old one), you can use withValue:
val newConfig = oldConfig.withValue("something.another.anotherconfig", ConfigValueFactory.fromAnyRef(456))
You can't overwrite a value in the original Config object since it's immutable. What you can do is create a new Config object with your values, using the original as a fallback. So:
val myConfig = ConfigFactory.parseString("something.another.anotherconfig=456") val newConfig = myConfig.withFallback(oldConfig)
and then use newConfig everywhere instead of your original Config. A more maintainable option would be to have a 2nd config file with your changes and use:
val myConfig = ConfigFactory.load("local") val oldConfig = ConfigFactory.load val realConfig = myConfig.withFallback(oldConfig)
You could then use a System Property to set where to load myConfig
from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With