lightbend / config

configuration library for JVM languages using HOCON files
https://lightbend.github.io/config/
6.16k stars 967 forks source link

Resolving Variable Substitutions Between Multiple Config #790

Open RechaviaAmit opened 1 year ago

RechaviaAmit commented 1 year ago

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"
havocp commented 1 year ago

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).