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

Support overlaying environment values onto existing values. #173

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

Conversation

taothit
Copy link

@taothit taothit commented Jun 11, 2020

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 with default 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 use default 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 configuration struct 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:

  • (Highest) Environment Variable
  • Struct values
  • (Lowest) Struct tag values

Configuring a simple web server

Assume we have the provided configuration struct:

type ServerInfo struct {
    http             string `default:":8080"`
    appName          string `required:"true"`
    env              string `default:"local"`
    messageBrokerURL string `default:"localhost:8081"`
}

with the following environment variables defined:

SERVER_INFO_ENV=STAGE
SERVER_INFO_MESSAGE_BROKER_URL=http://message-broker-stage.bigbankcorp.internal/messages/recent

We can define the server info before overlaying values from the environment as follows:

base := ServerInfo{
  appName: "MessageDigester",
}

info := envconfig.Overlay("SERVER_INFO", &base)

Then, we run envconfig.Overlay() and expect info to be represented in json as:

{
  "http": ":8080",
  "appName": "MessageDigester",
  "env": "STAGE",
  "messageBrokerURL": "http://message-broker-stage.bigbankcorp.internal"
}

@strideynet
Copy link

👍 Need this

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