https://kotlinlang.org logo
#hoplite
Title
# hoplite
s

sam

10/11/2023, 3:15 PM
I don't think so no, it needs to match case or your var be all lowercase
🤕 1
d

dave08

10/11/2023, 3:34 PM
One of the main reasons I'm using Hoplite is to be able to override any setting using env vars in a k8s deployment... it's a pity that not all configs can be mapped to env vars...
s

sam

10/11/2023, 3:34 PM
In general you can, it just gets more complicated if you haveAVariableLikeThis
d

dave08

10/11/2023, 3:34 PM
Which is the standard name format in kotlin
It wouldn't be nice to have data classes with fields like this_setting
s

sam

10/11/2023, 3:35 PM
right, but if you have ENVVAR should that map to envVar or enVvAr or enVvar or eNvvar like the combinations are endless
I'm checking the code to see what we support one sec
d

dave08

10/11/2023, 3:36 PM
How do you store these properties after having resolved them from any of the various property sources?
s

sam

10/11/2023, 3:36 PM
it's a single big tree
d

dave08

10/11/2023, 3:36 PM
And the names?
s

sam

10/11/2023, 3:37 PM
everything gets merged so we support the cascading lookups
d

dave08

10/11/2023, 3:37 PM
Are they lowercased?
s

sam

10/11/2023, 3:37 PM
no, when a field is looked up, param mappers are applied first, which generate "alt" names so you could add your own param mapper
which would just map accessKey to ACCESS_KEY
a bunch of this stuff is built in, I'll let you know in a sec
intellij is indexing so give me 4 hours 😂
😁 1
🙈 1
Copy code
SOME_CAMEL_SETTING
will be automatically applied to someCamelSetting
which is probably what you want
d

dave08

10/11/2023, 3:40 PM
There's no such param mapper already existing?
s

sam

10/11/2023, 3:40 PM
you just need to add
Copy code
EnvironmentVariablesPropertySource(true,true)
as a property source
and I think what you want will work
d

dave08

10/11/2023, 3:43 PM
I'm trying this in a Kotlin Notebook... but I'm not sure how to provide it with the env var... and setting a system property probably doesn't work the same way?
I need to make sure this works before putting out my service... 🕵🏼‍♂️
s

sam

10/11/2023, 3:44 PM
system properties are another different property service
you could just set the env on your local computer and reload intellij and then the notebook should pick it up
on linux that's easy, but I don't use a mac so no idea how a mac would do it
d

dave08

10/11/2023, 3:45 PM
I'm on linux, how would you do it? Just running intellij from the command line with the env var before it?
s

sam

10/11/2023, 3:46 PM
or do
export FOO=BAR
in a shell and then running intellijj from the same shell
export will make the env var available to all processes forked from where you run the export
👍🏼 1
d

dave08

10/11/2023, 3:51 PM
Copy code
S3_CONFIGS__ACCESS_TOKEN
doesn't work
Even though
System.getEnv()
returns it
I have this:
Copy code
val configs = ConfigLoaderBuilder.default().apply {
    addResourceSource("/application.yaml")
    val envs = listOf("local")
    envs.forEach {
        addResourceSource("/application-$it.yaml", optional = true)
    }
    addEnvironmentSource()
}.build().loadConfigOrThrow<Settings>()
And it just takes all my configs from
application.yaml
and
application-local.yaml
This:
Copy code
object MyUppercaseParamMapper : ParameterMapper {
  override fun map(param: KParameter, constructor: KFunction<Any>, kclass: KClass<*>): Set<String> =
    setOfNotNull(param.name?.fold("") { acc, c -> if (c.isUpperCase()) acc + "_" + c.uppercaseChar() else acc + c.uppercaseChar() })
}
Copy code
"s3Configs.accessToken".fold("") { acc, c -> if (c.isUpperCase()) acc + "_" + c.uppercaseChar() else acc + c.uppercaseChar() }
gives me:
Copy code
S3_CONFIGS.ACCESS_TOKEN
but it still doesn't work... do I also need to convert the
.
to
__
?
s

sam

10/11/2023, 6:08 PM
add EnvironmentVariablesPropertySource(true,true) explicitly rather than via addEnvironmentSource
d

dave08

10/12/2023, 5:57 AM
Why should that help?
Copy code
fun ConfigLoaderBuilder.addEnvironmentSource(
  useUnderscoresAsSeparator: Boolean = true,
  allowUppercaseNames: Boolean = true,
) = addPropertySource(
  EnvironmentVariablesPropertySource(useUnderscoresAsSeparator, allowUppercaseNames)
)
with no params the default IS
true, true
Ok... my bad... I thought the sources had to be ordered from least priority to top priority like in Micronaut... but in Hoplite it's the opposite! For some reason, just addEnvironmentSource() works, even though I don't see how it adds the _ between where the capital letters are... it just uppercases everthing... (unless it's a combo of snake case + that...?). Thanks for all the help! As usual, you always answer our (sometimes stupid...) questions so warmly, and keep updating all your great libraries ❤️!
🎉 1
3 Views