Documentation ¶
Overview ¶
Package envdecode is a package for populating structs from environment variables, using struct tags.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidTarget = errors.New("target must be non-nil pointer to struct that has at least one exported field with a valid env tag")
ErrInvalidTarget indicates that the target value passed to Decode is invalid. Target must be a non-nil pointer to a struct.
var ErrNoTargetFieldsAreSet = errors.New("none of the target fields were set from environment variables")
var FailureFunc = func(err error) { log.Fatalf("envdecode: an error was encountered while decoding: %v\n", err) }
FailureFunc is called when an error is encountered during a MustDecode operation. It prints the error and terminates the process.
This variable can be assigned to another function of the user-programmer's design, allowing for graceful recovery of the problem, such as loading from a backup configuration file.
Functions ¶
func Decode ¶
func Decode(target interface{}) error
Decode environment variables into the provided target. The target must be a non-nil pointer to a struct. Fields in the struct must be exported, and tagged with an "env" struct tag with a value containing the name of the environment variable. An error is returned if there are no exported members tagged.
Default values may be provided by appending ",default=value" to the struct tag. Required values may be marked by appending ",required" to the struct tag. It is an error to provide both "default" and "required". Strict values may be marked by appending ",strict" which will return an error on Decode if there is an error while parsing. If everything must be strict, consider using StrictDecode instead.
All primitive types are supported, including bool, floating point, signed and unsigned integers, and string. Boolean and numeric types are decoded using the standard strconv Parse functions for those types. Structs and pointers to structs are decoded recursively. time.Duration is supported via the time.ParseDuration() function and *url.URL is supported via the url.Parse() function. Slices are supported for all above mentioned primitive types. Semicolon is used as delimiter in environment variables.
Example ¶
type Example struct { // A string field, without any default String string `env:"EXAMPLE_STRING"` // A uint16 field, with a default value of 100 Uint16 uint16 `env:"EXAMPLE_UINT16,default=100"` } os.Setenv("EXAMPLE_STRING", "an example!") var e Example if err := Decode(&e); err != nil { panic(err) } // If TEST_STRING is set, e.String will contain its value fmt.Println(e.String) // If TEST_UINT16 is set, e.Uint16 will contain its value. // Otherwise, it will contain the default value, 100. fmt.Println(e.Uint16)
Output: an example! 100
func MustDecode ¶
func MustDecode(target interface{})
MustDecode calls Decode and terminates the process if any errors are encountered.
func MustStrictDecode ¶
func MustStrictDecode(target interface{})
MustStrictDecode calls StrictDecode and terminates the process if any errors are encountered.
func StrictDecode ¶
func StrictDecode(target interface{}) error
StrictDecode is similar to Decode except all fields will have an implicit ",strict" on all fields.
Types ¶
type ConfigInfo ¶
type ConfigInfo struct { Field string EnvVar string Value string DefaultValue string HasDefault bool Required bool UsesEnv bool }
func Export ¶
func Export(target interface{}) ([]*ConfigInfo, error)
Returns a list of final configuration metadata sorted by envvar name
type ConfigInfoSlice ¶
type ConfigInfoSlice []*ConfigInfo
func (ConfigInfoSlice) Len ¶
func (c ConfigInfoSlice) Len() int
func (ConfigInfoSlice) Less ¶
func (c ConfigInfoSlice) Less(i, j int) bool
func (ConfigInfoSlice) Swap ¶
func (c ConfigInfoSlice) Swap(i, j int)