Documentation
¶
Index ¶
- Variables
- func Load[T any](defaultConfig *T, opts ...LoadOption) (*T, error)
- func WithEnvPrefix(prefix string) envOption
- func WithEnvSeparator(separator string) envOption
- func WithEnvStructTag(tag string) envOption
- type InvalidMapValueError
- type LoadConfig
- type LoadOption
- type Loader
- type UnsupportedTypeError
Constants ¶
This section is empty.
Variables ¶
var ( NotAMapError = errors.New("not a map") NotASliceError = errors.New("not a slice") ConfigTypeError = errors.New("config must be a pointer to a struct") )
var DefaultLoadOptions = []LoadOption{ UseEnv(), UseFlags(), }
DefaultLoadOptions is the default LoadOptions used by the Load function if no LoadOptions are passed into it.
Functions ¶
func Load ¶
func Load[T any](defaultConfig *T, opts ...LoadOption) (*T, error)
Load modifies the pointer it receives with configuration information from the sources specified in the LoadOptions. The Load function are passed to the Load function. The default LoadOptions are:
DefaultLoadOptions := []LoadOption{ qcl.UseEnv(qcl.WithPrefix(""), qcl.WithEnvSeparator(","), qcl.WithEnvStructTag("env")), qcl.UseFlags() }
If no LoadOptions are passed to the Load function, the default LoadOptions will be used.
Example:
qcl.Load(&defaultConfig)
is equivalent to:
qcl.Load(&defaultConfig, qcl.DefaultLoadOptions...)
If any LoadOption is passed to the Load function, the default LoadOptions will not be used. The Load function returns a pointer to the configuration struct, and an error.
func WithEnvPrefix ¶
func WithEnvPrefix(prefix string) envOption
WithEnvPrefix allows you to specify a prefix for environment variables. For example, if you specify "FOO" as the prefix, then an environment variable named "FOO_BAR" will be used to set a field named "Bar" in the config struct.
Example:
type Config struct { Bar string } WithEnvPrefix("FOO")
will set the value of Bar to the value of the environment variable "FOO_BAR".
The default is no prefix.
func WithEnvSeparator ¶
func WithEnvSeparator(separator string) envOption
WithEnvSeparator allows you to specify a custom separator for environment variables that are setting iterables.
Example:
WithEnvSeparator(";")
will allow an environment variable like
export FOO="bar;baz"
to set a field named "Foo" to a slice of strings with the values "bar" and "baz":
type Config struct { Foo []string // foo will be set to []string{"bar", "baz"} }
This also works for maps where the key/value pairs are separated by the separator.
Example:
export FOO="bar=baz;qux=quux"
will set a field named "Foo" to a map with the key/value pairs bar=baz, and qux=quux.
type Config struct { Foo map[string]string // foo will be set to map[string]string{"bar": "baz", "qux": "quux"} }
The default separator is a comma (,)
func WithEnvStructTag ¶
func WithEnvStructTag(tag string) envOption
WithEnvStructTag allows you to specify a custom struct tag to use for environment variable names. By default, the loader looks for the "env" struct tag, but if that's not found the field name itself is used as the environment variable name and in either case, it is split on word boundaries. For example, a field named "FooBar" will be set by the environment variable "FOO_BAR" by default.
Example:
WithEnvStructTag("mytag") type Config struct { FooBar string `mytag:"FOO"` // FooBar will be set by the environment variable "FOO" instead of the default "FOO_BAR" }
By default, the environment loader looks for a struct tag "env" and in the absence of a struct tag, will use the field name itself.
Types ¶
type InvalidMapValueError ¶
type InvalidMapValueError struct {
// contains filtered or unexported fields
}
InvalidMapValueError is returned when the number of keys and values in a map do not match.
func (InvalidMapValueError) Error ¶ added in v0.9.4
func (e InvalidMapValueError) Error() string
type LoadConfig ¶
type LoadConfig struct { Sources []string // Sources is a slice of the configuration sources. Loaders map[string]Loader // Loaders is a map of the configuration sources and their corresponding loaders. }
LoadConfig is the configuration struct for the Load function. It contains the configuration sources and the loaders for those sources. Since maps in go are not ordered, the order of the sources is kept in a separate slice. The Load function will iterate over the sources in the Sources slice and call the corresponding loader in the Loaders map.
type LoadOption ¶
type LoadOption func(*LoadConfig) // LoadOption is a function that configures the Load function's LoadConfig. The Load function accepts a variable number of LoadOptions.
func UseEnv ¶
func UseEnv(opts ...envOption) LoadOption
UseEnv allows you to load configuration from environment variables. The environment variables are expected to be in all caps and separated by underscores. For example, a field named "FooBar" will be set by the environment variable "FOO_BAR".
Example:
export FOO_BAR=baz type Config struct { FooBar string } var defaultConfig Config ocl.Load(defaultConfig, ocl.UseEnv())
will set the value of FooBar to the value of the environment variable "FOO_BAR".
func UseFlags ¶
func UseFlags() LoadOption
UseFlags enables configuration from command line flags. Currently, the flag loader is not configurable. It will use the struct field names as the flag names, but lowercased and spit on word boundaries with a dash. For example, the field name "FooBar" will be converted to "foo-bar". You can override the flag name by using the "flag" struct tag. Examples:
type Config struct { FooBar string // will look for -foo-bar flag } type Config struct { FooBar string `flag:"foo"` // will look for -foo flag } type Config struct { FooBar string `flag:"foo.bar"` // will look for -foo.bar flag }
By default, calling Load() without any LoadOptions will use the flag loader as well as the environment loader, with the flag loader taking precedence. If you want to use only the flag loader, you can call Load with just the UseFlags option:
Load(&config, UseFlags()) // will only use flags
type UnsupportedTypeError ¶
type UnsupportedTypeError struct {
// contains filtered or unexported fields
}
UnsupportedTypeError is returned when a field is not a supported type.
func (UnsupportedTypeError) Error ¶ added in v0.9.4
func (e UnsupportedTypeError) Error() string