kelseyhightower / envconfig

Golang library for managing configuration data from environment variables
MIT License
5.01k stars 377 forks source link

Adding Check() function to test arbitrary key/values. #127

Closed rossmcf closed 5 years ago

rossmcf commented 6 years ago

This PR adds a function called Check(), which allows users to validate keys/values from other sources. This is intended to support the development of other tools, that might check — for example — environment variables defined in docker-compose files.

This resolves #126.

teepark commented 5 years ago

Thanks for the diff @rossmcf, it's not a bad high-level idea.

This does more than just "check" though, it will actually populate spec from the map (the only difference from Process is the "environment" source). There might be a nasty surprise for someone who constructs their program like this:

  1. Check against a sample map to make sure the spec is in good shape
  2. Process with the same spec to actually populate it from the environment

Now if the environment was missing an optional field which had been present in the sample map, they've got a sample test value populated instead of, say, the default tag value.

If we don't expect users to put this in their production programs (maybe just in a test), it's not any different from them iterating over a map and calling os.Setenv themselves, then checking with Process as-is.