Documentation ¶
Index ¶
- func CamelCase(path []string) string
- func KebabCase(path []string) string
- func Load(dst interface{}, loaders ...Loader) error
- func LowerSnakeCase(path []string) string
- func PascalCase(path []string) string
- func SplitPathIntoWords(path []string) []string
- func StructWalk(dst interface{}, fieldMapper FieldMapper) error
- func UpperSnakeCase(path []string) string
- type EnvOption
- type FieldMapper
- type FileOption
- type FlagSetOption
- type Loader
- type LoaderFunc
- type UnmarshalFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CamelCase ¶
CamelCase will convert the provided struct-field path to camel-case. For example:
CamelCase([]string{"Document","HTMLParser"}) // returns "documentHTMLParser"
func KebabCase ¶
KebabCase will convert the provided struct-field path to kebab-case, e.g.:
KebabCase([]string{"Document","HTMLParser"}) // returns "document-html-parser"
func Load ¶
Load is the central function tying all the building blocks together. It allows the composition of the loader precedence. For a given pointer to struct dst, an empty instantiation of the same type is create for each loader. Each loader populates its copy and all copies are merged into dst in the specified order.
See the README for several examples.
func LowerSnakeCase ¶
LowerSnakeCase takes the path to a struct-field and returns it in lower-case snake-case. For example:
LowerSnakeCase([]string{"Document","HTMLParser"}) // returns "document_html_parser"
func PascalCase ¶
PascalCase will convert the provided struct-field path to pascal-case. For example:
PascalCase([]string{"Document","HTMLParser"}) // returns "DocumentHTMLParser"
As you can see above the input is already expected to be field names in pascal-case and the names will be joined.
func SplitPathIntoWords ¶
SplitPathIntoWords is a utility function used by some of the *Case-functions. It takes a field path and returns the list of individual words. For example:
SplitPathIntoWords([]string{"Document", "HTMLParser"}) // returns []string{"Document", "HTML", "Parser"}
func StructWalk ¶
func StructWalk(dst interface{}, fieldMapper FieldMapper) error
StructWalk walks/visits every field of a struct (including nested) and calls fieldMapper for every field. If an internal error is encountered or an error is returned by fieldMapper it is immediately returned and traversal stopped.
func UpperSnakeCase ¶
UpperSnakeCase takes the path to a struct-field and returns it in upper-case snake-case. For example:
UpperSnakeCase([]string{"Document","HTMLParser"}) // returns "DOCUMENT_HTML_PARSER"
Types ¶
type EnvOption ¶
type EnvOption interface {
// contains filtered or unexported methods
}
EnvOption configures how environment variables are used to populate a given structure.
func ComputeEnvKey ¶
ComputeEnvKey will remove the requirement to explicitly specify the env-key with a tag. For all fields not explicitly tagged, the name will be computed based on the path by the provided nameGetter function. For example:
ComputeEnvKey(UpperSnakeCase)
func OverrideEnvTag ¶
OverrideEnvTag will change the struct-tag used to retrieve the env-key and options. The main purpose of this option is to allow interoperability with libraries using the same tag.
However it can also be used to enable edge-cases where multiple sets of environment variables are used to populate the same field.
func WithPrefix ¶
WithPrefix will prefix environment variable names with the specified value unless noprefix option is set in the tag.
type FieldMapper ¶
type FieldMapper func(path []string, field reflect.StructField) (interface{}, error)
FieldMapper is a function that takes the path of a field in a nested structure and field itself, to return a value or an error.
type FileOption ¶
type FileOption interface {
// contains filtered or unexported methods
}
FileOption configures how given configuration files are used to populate a given structure.
func AppendFilePaths ¶
func AppendFilePaths(paths ...string) FileOption
AppendFilePaths appends paths to the list of paths used to locate configuration files.
See File for details on how configuration files are located.
func ExpandEnv ¶
func ExpandEnv(f ...bool) FileOption
ExpandEnv expands environment variables in loaded configuration files.
func IgnoreNotFound ¶
func IgnoreNotFound(f ...bool) FileOption
IgnoreNotFound surpresses File from returning fs.ErrNotExist errors effectively making the configuration file optional.
func MergeFiles ¶
func MergeFiles(f ...bool) FileOption
MergeFiles changes the default behavior of using the first file found to load configuration. Instead all files that are available will be loaded and unmarshalled into the configuration struct.
type FlagSetOption ¶
type FlagSetOption interface {
// contains filtered or unexported methods
}
FlagSetOption configures how a pflag.FlagSet is used to populate a given structure.
func ComputeFlagName ¶
func ComputeFlagName(nameGetter func([]string) string) FlagSetOption
ComputeFlagName will remove the requirement to explicitly specify the flag-name with a tag. For all fields not explicitly tagged, the name will be computed based on the path by the provided nameGetter function. For example:
ComputeFlagName(KebabCase)
func IncludeUnchanged ¶
func IncludeUnchanged(f ...bool) FlagSetOption
IncludeUnchanged will also process the values of unchanged flags. Effectively this means the flag defaults, if non zero, will be set as well.
func OverrideFlagTag ¶
func OverrideFlagTag(tag string) FlagSetOption
OverrideFlagTag will change the struct-tag used to retrieve the flag name. The main purpose of this option is to allow interoperability with libraries using the same tag.
However it can also be used to enable edge-cases where multiple flags are used to populate the same field.
type Loader ¶
type Loader interface {
Process(dst interface{}) error
}
Loader is the interface that needs to be implemented to be able to load configuration from a configuration source. See Env, File or FlagSet for the implementations provided by this library. They only have to implement a single method Process, which populates the passed-in configuration-struct dst and returns an error if problems occur.
func Env ¶
Env implements a Loader, that uses environment variables to retrieve configuration values.
Standalone usage example:
cfg := struct{ // Illustrating some ways to load bytes from env A []byte `env:"NOPREFIX_A,noprefix"` B []byte `env:"MYPREFIX_B,hex"` C []byte `env:",base64"` }{} err := Env(WithPrefix("MYPREFIX"), ComputeEnvKey(UpperSnakeCase)).Process(&cfg)
func File ¶
func File(filePath string, unmarshal UnmarshalFunc, opts ...FileOption) Loader
File implements a Loader, that uses a file or files to retrieve configuration values.
By default the provided filePath is used. However this behaviour can be changed using options. If File should look in multiple locations, additional paths can be appended using AppendFilePaths. File will check the existence of those files one by one and load the first found. If MergeFiles is specified, all files will be loaded and unmarshalled in the order specified by the search paths.
Simple standalone example:
err := File("/etc/myapp/config.json", json.Unmarshal, IgnoreNotFound()).Process(&cfg)
Advanced standalone example:
err := File("./config.json", json.Unmarshal, AppendFilePaths("/etc/myapp/myapp.json", path.Join(userHomeDir, ".config/myapp/myapp.json")), MergeFiles(), ).Process(&cfg)
func FlagSet ¶
func FlagSet(flags *pflag.FlagSet, opts ...FlagSetOption) Loader
FlagSet implements a Loader, that takes a pflag.FlagSet and uses those to retrieve configuration values.
Standalone usage example:
flags := pflag.NewFlagSet("test", pflag.ContinueOnError) flags.String("foo-bar", "hello", "") err := flags.Parse([]string{}) // ... cfg := struct{ FooBar string }{} loader := FlagSet(flags, IncludeUnchanged(), ComputeFlagName(KebabCase)) err = l.Process(&cfg) // cfg.FooBar will have the default value of the flag "hello"
type LoaderFunc ¶
type LoaderFunc func(dst interface{}) error
LoaderFunc implements the Loader interface for a individual functions.
func (LoaderFunc) Process ¶
func (fn LoaderFunc) Process(dst interface{}) error
Process calls the LoaderFunc underneath.
type UnmarshalFunc ¶
UnmarshalFunc is a function that File can use to unmarshal data into a struct. Compatible with the common signature provided by json.Unmarshal, yaml.Unmarshal and similar.