sksamuel / hoplite

A boilerplate-free Kotlin config library for loading configuration files as data classes
Apache License 2.0
923 stars 74 forks source link

Loading arrays from the Environment #402

Open Perondas opened 9 months ago

Perondas commented 9 months ago

I am trying to load an arrayof strings from the environemnt. I cannot however figure out the correct syntax for it.

For example:

serversettings.cors = ["http://localhost","https://some.domain"]

with

 EnvironmentVariablesPropertySource(useUnderscoresAsSeparator = true, allowUppercaseNames = true)

When I supply the environment variable:

SERVERSETTINGS__CORS='["http://localhost","https://some.domain"]'

It passes the entire string with the square brackets into the first position of the list.

Does Hoplite support some syntax for passing arrays?

rocketraman commented 8 months ago

See also for reference the way Spring does it:

Environment variables can also be used when binding to object lists. To bind to a List, the element number should be surrounded with underscores in the variable name.

For example, the configuration property my.service[0].other would use an environment variable named MY_SERVICE_0_OTHER.

That being said, I think that is a less well known feature than Spring's basic env var mapping, so Hoplite could reasonably use a different approach.