Documentation ¶
Overview ¶
Package envconf parses environment variables into structs. It supports multiple types, however the core type is always a string.
Translators are available which manipulate the string value before setting, e.g. `!base64:SGVsbG8gV29ybGQ=` will cast to a string as "Hello World".
There is no specific handling for bytes when using this method, it is handled as a string entirely, if you are expecting actual bytes use a type like Hex or Base64 which will directly translate the env var string to bytes
Standard conversion from string to int, bool etc work, as well as custom types which satisfy `SetterFromEnv` (on a pointer, like JSON)
Combining translators and custom types is perfectly fine. The string translations will happen first, then the output will be passed into FromEnvString
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultParser = Parser{ Translators: map[string]Translator{ "base64": TranslatorFunc(Base64Translator), }, }
DefaultParser is used by the Parse and Translate funcs for convenience
Functions ¶
func Base64Translator ¶
Base64Translator decodes a URL Base64 encoded string. Not safe for byte data, use the Base64 type instead
func SetFromString ¶
SetFromString attempts to translate a string to the given interface. Must be a pointer. Standard Types string, bool, int, int(8-64) float(32, 64) and []string. Custom types must have method FromEnvString(string) error
Types ¶
type Base64 ¶
type Base64 []byte
Base64 is a byte array which is loaded from a URL base64 string
func (*Base64) FromEnvString ¶
FromEnvString Satisfies SetterFromEnv
type Hex ¶
type Hex []byte
Hex is a byte array which is loaded from a Hex string
func (*Hex) FromEnvString ¶
FromEnvString Satisfies SetterFromEnv
type Parser ¶
type Parser struct {
Translators map[string]Translator
}
Parser holds a list of Translator functions
func (Parser) Parse ¶
Parse reads the tags of dest to set any fields which should be parsed from the environment. The `env` tag gives the name of the variable. If the environment variable evaluates to an empty string, the value of `default` is used, or an error is thrown if the `default` tag is omitted. To allow optional parameters, set default to an empty string
func (*Parser) RegisterTranslatorFunc ¶
RegisterTranslatorFunc adds a translator function to the list of translators. It replaces any existing function with the given name
type SetterFromEnv ¶
SetterFromEnv is used by SetFromString for custom types
type Translator ¶
Translator is responsible for taking a string and converting it to the output string, either by looking it up in an external service, decrypring, or anything else you can think of