Re-resolve scala config object
I would like to re-resolve the config object. for example if i define this config:
val conf = ConfigFactory.parseString(
"""
| foo = a
| bar = ${foo}1
| baz = ${foo}2
""".stripMargin).resolve()
I will get these values:
conf.getString("bar") //res0: String = a1
conf.getString("baz") //res1: String = a2
given an object conf
, I want to be able to change the value foo
and get updated values for bar
and baz
. Something like:
val conf2 = conf
.withValue("foo", ConfigValueFactory.fromAnyRef("b"))
.resolve()
and get:
conf2.getString("bar") //res0: String = b1
conf2.getString("baz") //res1: String = b2
but running this code will result in:
conf2.getString("foo") //res0: String = b
conf2.getString("bar") //res1: String = a1
conf2.getString("baz") //res2: String = a2
is it even possible?
source to share
This is not possible if called resolve
. The documentation forresolve
says:
Returns a substitution configuration with substitutions allowed ... Allowing an already allowed configuration is a harmless no-op.
In other words, as soon as you call resolve
, all substitutions occur and there is no reference to the original replacement HOCON syntax.
You can of course store the unresolved Config
object as a variable and then use withValue
:
val rawConf = ConfigFactory.parseString(
"""
| foo = a
| bar = ${foo}1
| baz = ${foo}2
""".stripMargin)
val conf2 = rawConf.withValue("foo", ConfigValueFactory.fromAnyRef("b")).resolve
val conf = rawConf.resolve
conf.getString("bar") //a1
conf2.getString("bar") //b1, as desired
source to share