jdiazcano / cfg4k

Flexible and easy to use config library written in kotlin
Apache License 2.0
80 stars 6 forks source link

HOCON substitutions, override via file and via environment/system prop #54

Closed rocketraman closed 6 years ago

rocketraman commented 6 years ago

HOCON substitutions work well within a single file. However, when the substituted value needs to be loaded from another higher priority source e.g. another HOCON config file, system prop, or environment, the substitution fails.

At least for a HOCON-only solution, one can inject the HoconConfigLoader with the pre-resolved substitutions:

HoconConfigLoader {
      ConfigFactory
        .parseResources("application-local.conf")
        .withFallback(ConfigFactory.parseResources("application.conf"))
    })

However, when combining this with environment and/or system prop overloads, that solution is not sufficient as the substitutions are applied by typesafe config before the data is ever seen by cfg4k.

One can override the final key value, but not the individual substitution.

Example:

application-local.conf

environment {
  prefix = "dev-"
}

application.conf

environment {
  prefix = ""
}
foo = ${environment.prefix}bar

with configuration something like:

OverrideConfigProvider(
    ProxyConfigProvider(EnvironmentConfigLoader()),
    ProxyConfigProvider(SystemPropertyConfigLoader()),
    ProxyConfigProvider(HoconConfigLoader("application-local.conf"))
    ProxyConfigProvider(HoconConfigLoader("application.conf"))
)

My ideal scenario would be that the following would be true:

1) No system or env properties, set, then

foo = dev-bar

2) No application-local.conf present, or is empty:

foo = bar

3) With System property or equivalent env var -Denvironment.prefix=test-:

foo = test-bar

The general idea here being that one can specify some defaults for substitutions, but override them with earlier config providers in the same way that one can specify defaults for entire config values but override them with earlier config providers.

rocketraman commented 6 years ago

As per our discussion on Slack this is a dup of https://github.com/jdiazcano/cfg4k/issues/29. Closing!

jdiazcano commented 6 years ago

Adding more details:

The problem is that the substitutions are done by the HOCON library. Cfg4k isn't doing any kind of substitution and that's why a HOCON cannot be used to substitute the value from a preceding provider/loader.