1

I have a map in my YAML file:

custom:
 map:
  '[A]': B

But in some situations, I would like to set this map with an env variable value. Let's say we have:

export ENV_VAR="key:value"

YAML file allows us to use env variable interchangeably with some default value, here's an example with a single field key-value:

custom:
 uri: ${ENV_VAR:https://google.com}

So, the question here is how can I use the env variable in YAML interchangeably with the default value for maps? The code I want to work out:

custom:
 map: ${ENV_VAR:'[key]':value}
Afrodeziak
  • 15
  • 7
  • Maybe you can pass the default value as a JSON-like string, see this answer : https://stackoverflow.com/a/61535146/5612858 – Arnaud Mar 28 '22 at 12:18
  • See this: https://dev.kuffel.io/env-variables-spring-default/ and check if it helps or not. – pringi Mar 28 '22 at 12:21
  • @pringi, unfortunately, it didn't work out. We have here map structure, not the single field – Afrodeziak Mar 28 '22 at 12:30
  • Why don't you put the default value in yaml, and override the property in the environment variable? Ex: https://stackoverflow.com/questions/37052857/spring-overriding-one-application-property-from-command-line – pringi Mar 28 '22 at 12:33
  • @Arnaud, using such construction I got a compile error in the YAML file, after colon – Afrodeziak Mar 28 '22 at 12:33
  • In your environment, export CUSTOM_URI="key:value" and in you yaml, custom:map: '[key]':value – pringi Mar 28 '22 at 12:36
  • That makes sense @pringi, the only problem, is that Java cannot parse that: No converter found capable of converting from type String to Map. This construction ENV_VAR="key:value" cannot be converted to Map – Afrodeziak Mar 28 '22 at 12:46

0 Answers0