config icon indicating copy to clipboard operation
config copied to clipboard

Resolving Variable Substitutions Between Multiple Config

Open RechaviaAmit opened this issue 1 year ago • 1 comments

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. I probably doing something wrong but I can't find any online solution.

Here is my code:

class ScalaCentralSharedLibConfigManager() {
  val configString =
    """
      |logger {
      |  elasticsearch {
      |    servers {
      |      prod {
      |        host = "http://example.com"
      |      }
      |    }
      |  }
      |}
      """.stripMargin
  var internalConsulConfig: Config = ConfigFactory.parseString(configString).resolve()
  var internalConfig: Config = ConfigFactory.load("sharedlibapplication.conf").withFallback(internalConsulConfig).resolve()

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 desired output is:

println(internalConfig.getString("logger.elasticsearch.servers.prod.host"))
-- >> "http://example.com"

RechaviaAmit avatar Jul 07 '23 08:07 RechaviaAmit

the docs for load(String) explain the likely issue - load does the resolve already before you add the fallback. You could use parse instead, or could use load(Config).

havocp avatar Jul 07 '23 13:07 havocp