Support overlaying environment values onto existing values. #173
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Use Case
Our common application configuration requires each application to define a small set of descriptive information about the service in order to make the application deployable, discoverable, etc.
envconfig.Process
along withdefault
struct tag definitions allow for both global default value configuration as well as environment-specific configuration. However, they do not support defining application-specific defaults for common configuration concerns (application name, e.g.) while preserving global default values and required field behaviors. It's not possible to usedefault
value and setting the same information in each environment is redundant and prone to errors when not setting it correctly in each environment.envconfig.Overlay
enables per-application defaults by replacing only values defined in a configurationstruct
instance with environment variables and not the default values (if they are defined).Configuration values will adhere to following order of precedence with these changes:
Configuring a simple web server
Assume we have the provided configuration struct:
with the following environment variables defined:
We can define the server info before overlaying values from the environment as follows:
Then, we run
envconfig.Overlay()
and expectinfo
to be represented in json as: