Deny permission at boot time in config config
I want to deny permission a.b
. I want to replace it param
with a different config. Like this:
val d = ConfigFactory.load(ConfigFactory.parseString(
"""
|param = x
|a.b = ${param}
""".stripMargin))
val a = ConfigFactory.parseString("param = 1")
val result = a.withFallback(d).resolve()
In this case it param
gets the value 1, but it a.b
remains x
I tried to install ConfigResolveOptions.defaults().setAllowUnresolved(true)
when loading config d
, but it doesn't work.
How can I overcome this?
source to share
Found a workaround for my problem:
So, if I have a config file application.conf
that it uses include
to include config files containing substitution syntax and files containing declaration of config values to be replaced.
val a = ConfigFactory.parseString(s"""param = 1""")
val z = ConfigFactory.parseResources("application.conf") //this doesn't resolve substitutions
val result = a.withFallback(z).resolve().withFallback(ConfigFactory.load("application.conf"))
source to share
I did a bit of tinkering with the same: trying to use "backups" to override values when it was designed to link / merge configurations
Assuming I understand your use case, I recommend using a file instead.
In mine application.conf
I have a default
a.b = "placeholder"
And at the bottom I have the following
# Local overrides - for development use
include "local.conf"
Finally, in local.conf
param = 1
a.b = ${param}
The end result is something that a.b
will be overridden with1
source to share
The problem is that it Config.load
allows the substitution immediately. If you choose this, it resolves how you want:
val p = ConfigFactory.parseString(
"""
|param = x
|a.b = ${param}
""".stripMargin)
val a = ConfigFactory.parseString("param = 1")
val result = a.withFallback(p).resolve()
println(result.getString("a.b"))
Will print 1.
You don't need to use Config.load
, if you don't want to use reference.conf
, etc. If you want to use Config.load
, you should do so after you have compiled all the configs using withFallback
,
For example, this also prints 1:
val p = ConfigFactory.parseString(
"""
|param = x
|a.b = ${param}
""".stripMargin)
val d = ConfigFactory.load(p)
val a = ConfigFactory.parseString("param = 1")
val result = a.withFallback(p)
val loaded = ConfigFactory.load(result)
println(loaded.getString("a.b"))
Or let's say you have a application.conf
c include
that you want to use with ConfigFactory.load()
(per your comment).
If application.conf
it looks like
include "foo"
and foo.conf
looks like
a.b = ${param}
then this also prints 1:
val a = ConfigFactory.parseString("param = 1")
val app = ConfigFactory.load("application", ConfigParseOptions.defaults,
ConfigResolveOptions.defaults.setAllowUnresolved(true))
val result = a.withFallback(app).resolve
println(result.getString("a.b"))
In general, if you want A to override B to override C, you should use A.withFallback(B).withFallback(C)
.
source to share