Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default values for environment injection #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

scowcron
Copy link

To date, rust-config supports injecting environment variables into a configuration with the following syntax:

// Configuration file
log = {
    level = $"LOG_LEVEL"::str;
}

if LOG_LEVEL is not set, then the configuration will set it to a value of "", or an empty string. The potential for further syntax to specify a default value would, in my opinion, greatly increase the utility of this functionality. For example, being able to specify a default log level of "warning" and change that with an environment to "debug" would be a handy thing to have.

This patch supports that functionality with the following syntax:

// Configuration file
log = {
    level = $"LOG_LEVEL"::str("warning");
}

Now, when the program is invoked without the LOG_LEVEL environment variable set

./program

the program will read a value of "warning", but if the environment variable is set

LOG_LEVEL="debug" ./program

the program will read a value of "debug" instead.

This default value is strictly optional, and simply omitting the "(default)" section is permitted and the library will fall back to it's built in defaults.

in the case of $"ENV_VAR_NAME"::bool, we now have the option of
doing $"ENV_VAR_BOOL"::bool(true), where if the environment variable
is not set, the value will be assigned true instead of false.
@workanator
Copy link
Contributor

We were discussing that in the issue #14 but conversation stuck.
IMO the syntax becomes very complex and application should have hardcoded default values for missing env variables.

@scowcron
Copy link
Author

application should have hardcoded default values for missing env variables.

Here's the thing: this is a configuration language, not a programming language. The application shouldn't even be aware that the configuration is reading it's values from the environment. If it does care, then it should read from the environment directly and handle it as appropriate instead of relying on the config library.

From what I can tell, #14 is mostly a discussion of what the behavior should do if there is NO default value specified, though the idea of allowing a default is mentioned in the form of something like COALESCE(...). I personally am not a fan of the syntax given, but it does raise the point of being able to fall through multiple environment variables before hitting a default (or not). The syntax I've proposed doesn't allow for that as written. It could just check for another value of the same type instead of specifying that it had to be a locally defined value, but that could get ugly and complicated really fast and I don't suggest doing it this way:

$"LOG_LEVEL"::str($"LOGGING"::str("debug"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants