0

In my use case, I have a scenario where I want to populate some of the configs from the typesafe config file itself and some of those, I want to set programmatically. For example, for below myconfig.conf file

env=staging
topic=${env}_${event}

In above, snippet, topic should be resolved as staging_someevent event where someevent is set in one of the method which resolves this value based on argument in the job using args.

I am able to resolve one of those but not both. Below code resolves ${event}

String event = "someevent";
File file = new File("myconfig.conf");
InputStream inputStream = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(inputStream);
String configText = ConfigFactory.parseReader(reader)
  .resolveWith(ConfigFactory.parseString("event=" + event))
  .root
  .render(ConfigRenderOptions.concise().setJson(false));
System.out.println(configText);

Similarly, below code resolves ${env} but how can I resolve both?

String configText = ConfigFactory.parseReader(reader)
  .resolve()
  .root
  .render(ConfigRenderOptions.concise().setJson(false));
System.out.println(configText);
Piyush Patel
  • 1,646
  • 1
  • 14
  • 26
  • 1
    Have you tried using `withFallback` to chain configs together? – Tim May 11 '22 at 09:48
  • @Tim I tried that but didn't work out for this case. I think `withFallback` just overrides? Not sure though – Piyush Patel May 11 '22 at 13:25
  • @Tim I think you were right. `withFallback` helps. I was able to resolve them by having two configs. When I tried, I didn't call `resolve()` on fallback config which didn't resolve even though it was just straight `key=value` config. Thank you – Piyush Patel May 11 '22 at 19:49

0 Answers0