I'm encountering an issue while attempting to load two configs in Scala using the Typesafe Config library, where the second config needs to reference variables in the first one.
Here is my code:
class ScalaCentralSharedLibConfigManager() {
val configString =
"""
|logger {
| elasticsearch {
| servers {
| prod {
| host = "http://example.com"
| }
| }
| }
|}
""".stripMargin
var internalConsulConfig: Config = ConfigFactory.parseString(configString)
var internalConfig: Config = ConfigFactory.load("sharedlibapplication.conf").resolveWith(internalConsulConfig)
Within my sharedlibapplication.conf I have the following:
elasticSearch {
prod {
host = ${logger.elasticsearch.servers.prod.host}
}
}
When I run the code, I get the error: "Could not resolve substitution to a value: ${logger.elasticsearch.servers.prod.host}"
Any guidance or assistance with this issue would be much appreciated.
The problem is that method
loadis performing resolution of all internal references before you even get to the methodresolveWith(internalConsulConfig), so the latter does nothing in you case.So you can do it this way instead
Then you can see that it works by calling
with following output produced.
However, I'd recommend to stick to the built in mechanism of sharing config files of libraries via
reference.confin combination withapplication.conf, which is the main config file that fallbacks to all combinedreference.conf. This mechanism is used by libraries likeakkafor example and well understood by many developers.So if you use the same and well known principles, it will be better understood by other developers in your team for example. A special resolution of config files, like the one described above, would be the last option for me.
Here's a simple example how you can do it for your case with the help of
reference.confwith additional environment variable defined.reference.conf:Then just setting the environment variable
ELASTIC_SEARCH_HOST=http://example.comwith a config object loaded asConfigFactory.load(), which is doing the default resolution of config files with environment variables support, will be enough to provide correct settings into your library code.