https://kotlinlang.org logo
Title
p

Peter

01/24/2022, 10:45 PM
hi @sam - running into an interesting situation, is this the expected behaviour?
given
redis {
  host = ${MY_REDIS_HOST}
  port = ${MY_REDIS_PORT}
}
if the following is in the environment:
MY_REDIS_PORT=6378 redis.port=6379
using
data class RedisConfig(host: String, port: Int)
i’ll end up with
RedisConfig("..", 6379)
i was expecting to end up with
RedisConfig("..", 6378)
s

sam

01/24/2022, 10:47 PM
that's an interesting one
it's because it's using the env as a property source (for all values)
I guess the preprocessor one should override it
hmm it's tricky to fix
the env var property source like "overlays" on the files
you could disable that
p

Peter

01/24/2022, 10:49 PM
i have no idea why
redis.port
in the environment is taking precedence over
MY_REDIS_PORT
in the environment if i specified
MY_REDIS_PORT
in my HOCON config file?
s

sam

01/24/2022, 10:49 PM
because the env vars form like a "hidden" file
that's applied over the top
p

Peter

01/24/2022, 10:51 PM
by why does
redis.port
even apply to set the value in HOCON? i know that syntax is supported for reading values from the parsed HOCON configuration
s

sam

01/24/2022, 10:52 PM
its not related to hocon
you have an env var called 'redis.port' right
p

Peter

01/24/2022, 10:52 PM
right
s

sam

01/24/2022, 10:52 PM
so that forms a "file" which is like { redis { port : 1234 } }
which then is the first match when the decoder says "give me a value for redis.port"
p

Peter

01/24/2022, 10:53 PM
ahhh
s

sam

01/24/2022, 10:53 PM
if I'm right, it's not even getting to your hocon file, and therefore the hocon override, because it's already present in the env
The 
EnvironmentVariablesPropertySource
SystemPropertiesPropertySource
 and 
UserSettingsPropertySource
 sources are automatically registered, with precedence in that order
EnvironmentVariablesPropertySource The 
EnvironmentVariablesPropertySource
 reads config from environment variables. It does not map cases so 
HOSTNAME
 does not provide a value for a field with the name 
hostname
. For nested config, use a period to seperate keys, for example 
topic.name
 would override 
name
 located in a 
topic
 parent. Alternatively, in some environments a 
.
 is not supported in ENV names, so you can also use double underscore 
__
. Eg 
topic__name
 would override name in a Topic object. Optionally you can also create a 
EnvironmentVariablesPropertySource
 with 
allowUppercaseNames
 set to 
true
 to allows for uppercase-only names.
what you can do is create your config loader without the default sources, then it won't use that EnvironmentVariablesPropertySource
withDefaultSources(false) on the builder
p

Peter

01/24/2022, 10:55 PM
will it still substitute
host = ${MY_REDIS_HOST}
?
with the value of
MY_REDIS_HOST
s

sam

01/24/2022, 10:56 PM
yes because that is an override that is applied by the envvar preprocessor
so its another path
now I think about it the EnvironmentVariablesPropertySource might not make a ton of sense
it's just useful if you want to set ENV vars without putting them in files
p

Peter

01/24/2022, 10:56 PM
gotcha - i understand now
s

sam

01/24/2022, 10:57 PM
like you could do
redis.port
an env
and you wouldn't need to put it in the hocon at all
which is exactly how you found this to start with
p

Peter

01/24/2022, 10:57 PM
EnvironmentVariablesPropertySource
vs
EnvVarPreprocessor
… i missed the distinction there initially 😅
s

sam

01/24/2022, 10:58 PM
hope that helps
p

Peter

01/24/2022, 10:58 PM
thanks much!
👍🏻 1