Documentation ¶
Overview ¶
Package config provides types and functions for managing application configuration. A set of command-line flags or environment variables to parse can be inferred from a struct's type. The values are then subject to validation and assigned to the struct fields.
Quick Start ¶
The easiest way to use the package is to define a struct type and pass it to Configure. Struct tags can be used to control how fields are scanned or validated.
type Auth struct { User string Password string } type Options struct { Auth *Auth URLs []*url.URL `scheme:"^(http|https)$"` Verbose int `min:"0" max:"8"` } opts := Options{Verbose: 1} if err := config.Configure(&opts); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } if opts.Auth != nil { fmt.Printf("Auth: %+v\n", *opts.Auth) } fmt.Printf("URLs: %s\nVerbose: %d\n", opts.URLs, opts.Verbose)
If the program was given the -h flag, the following help would be written to stderr:
Command Line Flags: -auth-password string -auth-user string -urls url -verbose int (default 1) Environment Variables: AUTH_PASSWORD=string AUTH_USER=string URLS=url VERBOSE=int (default 1) Help requested
Running the program with no command line flags and none of the environment variables set would output:
URLs: [] Verbose: 1
Note that the entire Auth field is left as nil; nil pointers are only set if a value is configured for the field. Running the program again with flags
-urls http://www.google.com/ -urls http://www.yahoo.com/ -auth-user user1
provides the following:
Auth: {User:user1 Password:} URLs: [http://www.google.com/ http://www.yahoo.com/] Verbose: 1
Finally, some of the values have validation tags for their struct fields. If the validations are not met, an error is given:
invalid value "10" for flag -verbose: Validating 10 failed: 10 is not less than or equal to 8
Supported Types ¶
The following types are supported by the package:
- bool
- float32, float64
- int, int8, int16, int32, int64
- net.IP, net.IPNet
- string
- time.Duration, time.Time
- uint, uint8, uint16, uint32, uint64
- url.URL
In addition, any types derived from pointers and slices to those types are also supported.
bool, float, int and uint values are parsed by strconv.ParseBool, strconv.ParseFloat, strconv.ParseInt and strconv.ParseUint. Trying to set a value that would overflow the type results in an error.
net.IP values are parsed using net.ParseIP. net.IPNet values are parsed using net.ParseCIDR. url.URL values are parsed using url.Parse. time.Duration values are parsed using time.ParseDuration.
time.Time values will be parsed using the following layouts until one is succesful or all have been tried:
- "2006-01-02T15:04:05Z07:00"
- "2006-01-02 15:04:05Z07:00"
- "2006-01-02T15:04:05"
- "2006-01-02 15:04:05"
- "2006-01-02T15:04Z07:00"
- "2006-01-02 15:04Z07:00"
- "2006-01-02T15:04"
- "2006-01-02 15:04"
- "2006-01-02T15"
- "2006-01-02 15"
- "2006-01-02"
- "2006-01"
Additional types can be supported either by implementing the Setter API on that type, or by implementing and registering a SetterCreator for that type. The latter option will enable the package to automatically wrap derived pointer and struct types.
Validation ¶
Struct tags can be used to impose validation on parsed values.
`min:"x"` or `ge:"x"` sets a minimum value for int, float, uint, time.Duration and time.Time values. The value must be appropriate for the type, and is parsed the same as values of that type. For example
Timeout time.Duration `min:"30s"`
indicates that the Timeout value must be greater than or equal to 30 seconds.
`max:"x"“ or `le:"x"` indicates a maximum value for the above types. Similarly, `gt:"x"` or `lt:"x"` will require values greater than or less than the values specified.
Values can be combined to set ranges. Note, however, that it's possible to set impossible validations this way, e.g., `gt:"10" lt:"5"`.
`regexp:"x"` sets a regular expression for validating string values. A match can occur anywhere in the string. To match the whole string, anchor the expression with "^$". Also note that the value in the struct tag will be subject to string unquoting; backslashes and double-quotes must be escaped with a backslash.
`scheme:"x"`, `host:"x"` or `path:"x"` tags can be applied to url.URL values. They specify regular expressions that are used to validate the corresponding parts of the URL.
The `is:"x"` tag can be used to specify required or disallowed classes of IP addresses for the net.IP or net.IPNet types. Valid values are:
- global unicast
- interface local multicast
- link local multicast
- link local unicast
- loopback
- multicast
- unspecified
The class name can be prefixed with an exclamation point to indicate that the class is disallowed. Multiple values can be combined by separating with commas. For example, `is:"!loopback,!link local unicast"` would allow addresses that are neither loopback nor link local unicast. If any disallowed class is matched, the validation will fail. The allowed classes are matched in an "or" fashion, however, and any one match will cause the validation to succeed.
The `net:"x"` tag can be used with net.IP or net.IPNet values to specify required or disallowed networks. Networks are specified in address/bits form, e.g., 169.254.0.0/16 or 2001:cdba:9abc:5678::/64. Disallowed networks are specified by prefixing with an exclamation point. Multiple values can be combined by separating with commas. The semantics are the same as for the 'is' tag: A match for any disallowed value fails validation; a match for any single allowed value causes validation to succeed.
The `version:"4"` or `version:"6"` tags can be used with net.IP and net.IPNet values to specify that the value must be an IPv4 or IPv6 address.
Other Struct Tags ¶
`config:"X"` can be used to override the name of a struct field, instead of using the reflected name. The name will still be subjected to parsing with SplitName. Setting `config:"-"` will cause the struct field to be skipped, i.e., it will not be scanned or configured.
`prefix:"X"` can be used to override how nested structs are handled. If no prefix is set, the child struct uses the name of its field in the parent struct. If a name is given, that name will be used to group settings in the child struct. (`config:"X"` can be used for the same purpose.) If `prefix:"-"` is given, the child struct will use the same prefix as its parent, and names parsed from the child's fields will be added to the parent's level as if the fields had been parsed from the parent. For example:
type Options struct { Timeout time.Duration Auth struct { User string Pass string } `prefix:"-"` } var opts Options config.Configure(&opts)
will result in command line flags being generated for -timeout, -user and -pass. Without the prefix tag, the names would have been -auth-user and -auth-pass.
`from:"X"` will cause the value to only be configured by the named loaders. For example
Interactive bool `from:"flag"`
will only let the Interactive value be set from a command line flag.
`append:"false"` or `append:"true"` (the default) can be used with slice types. When true, values are appended to existing values in a slice. When false, setting a new value will cause the slice to be overwritten. (Setting multiple values will still result in second and subsequent values being appended after the first new value).
`sep:"X" can be used with slice types. It indicates a string on which the value should be split on in order to populate a slice.
Index ¶
- Variables
- func Configure(strct interface{}) error
- func FriendlyTypeName(x interface{}) string
- func Load() error
- func PathCmp(a, b *Path) int
- func Scan(strct interface{}) error
- func SplitName(name string) []string
- func Var(value interface{}, tag reflect.StructTag, name ...string) error
- type Config
- func (c *Config) AddLoader(loader Loader)
- func (c *Config) Args() []string
- func (c *Config) Configure(strct interface{}) error
- func (c *Config) GetLoaders() Loaders
- func (c *Config) GetSetterRegistry() SetterRegistry
- func (c *Config) Load() error
- func (c *Config) Scan(strct interface{}) error
- func (c *Config) SetLoaders(loaders Loaders)
- func (c *Config) SetSetterRegistry(reg SetterRegistry)
- func (c *Config) Usage(w io.Writer)
- func (c *Config) Var(value interface{}, tag reflect.StructTag, name ...string) error
- type ConversionError
- type EnvLoader
- type Errors
- type FlagLoader
- type Loader
- type Loaders
- type NodePath
- func (np *NodePath) AddNodePath(c *NodePath) *NodePath
- func (np *NodePath) AddPath(c *Path) *Path
- func (np *NodePath) FindNodePath(elements ...string) *NodePath
- func (np *NodePath) FindPath(elements ...string) *Path
- func (np *NodePath) NewNodePath(name string) *NodePath
- func (np *NodePath) NewPath(name string) *Path
- type Path
- type Setter
- type SetterCreator
- type SetterRegistry
- type Setting
- type UnknownTypeError
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = new(Config)
DefaultConfig is a default, global Config.
Is is used by the package-level Scan, Var and Configure functions, or can be used directly by an application that doesn't require multiple configurations.
var DefaultSetterRegistry = SetterRegistry{}
DefaultSetterRegistry provides a default registry.
It supports SetterCreator instances for the built-in types, and is used by Config if no other registry is specified.
var ErrHelp = errors.New("Help requested")
ErrHelp is returned when help is requested via the -h command line flag.
Functions ¶
func FriendlyTypeName ¶
func FriendlyTypeName(x interface{}) string
FriendlyTypeName returns a descriptive name for a value's type.
It is primarily useful for generating placeholder names for paremeters in help output.
If types are element types, such as an array or pointer, they are dereferenced until a non-element type is found. If that type has a name, it is lowercased and returned. If the type is unnamed, the string "value" is returned.
func PathCmp ¶
PathCmp compares two Paths to determine ordering.
It returns -1 if a should order before b; 1 if a should order after b; or 0 if a and b are equivalent.
func SplitName ¶
SplitName splits a name into logical words.
It attempts to parse names into words based on Go conventions and how humans perceive them:
- A capital letter following a lowercase one always starts a new word.
- A series of capital letters are generally considered part of the same word. The last capital letter in the series will start a new word, except:
- When it is followed by a lowercase letter and a digit, e.g., "v2".
- When it is followed by a single lowercase letter and then the end of the string.
Examples:
"HTMLEntityID" -> {"HTML", "Entity", "ID"} "UUID" -> {"UUID"} "UUIDv2" -> {"UUID", "v2"} "UUIDs" -> {"UUIDs"} "IPv6Network" -> {"IP", "v6", "Network"}
The return values are useful for creating parameter names that are readily human-readable, such as for command line flags or environment variables.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config represents a configuration for an application.
It can be used in lieu of the package-level functions or DefaultConfig if an application needs to manage multiple, separate configurations.
The zero value is ready to use.
func (*Config) Args ¶
Args returns the command-line arguments left after parsing.
Returns nil if Load has not been called, or if no flag loader was included in the config.
func (*Config) Configure ¶
Configure scans the strct argument to generate a list of parameters and then loads them.
The strct parameter must be a non-nil pointer to a struct type. Any other value panics.
It is equivalent to calling c.Scan followed by c.Load. The return value, if not nil, will be of type Errors, and will contain the concatenated errors from both Scan and Load.
func (*Config) GetLoaders ¶
GetLoaders returns the current Loaders used by the Config.
It returns the last loaders set by SetLoaders, or a set of default loaders if none was set.
func (*Config) GetSetterRegistry ¶
func (c *Config) GetSetterRegistry() SetterRegistry
GetSetterRegistry returns the SetterRegistry used by the Config.
It returns the last value passed to c.SetSetterRegistry, or DefaultSetterRegistry if none is set.
func (*Config) Load ¶
Load calls loaders to parse and validate configuration.
This method is implicitly called by c.Configure. It is only necessary to call it if not calling c.Configure. This method will only do something useful if c.Scan or c.Var has been called to populate a list of values to set.
Any validation or load errors will result in a non-nil return status.
If one of the loaders is a *FlagLoader and the -h flag has not been overridden, calling Load with "-h" in the application's command line arguments will cause a list of settings and their descriptions to be printed to stderr.
func (*Config) Scan ¶
Scan uses reflection to populate configuration settings from a struct.
The strct parameter must be a non-nil pointer to a struct type. Scan will panic if any other value is passed.
This method is implicitly called by calling c.Configure. This method can be used if multiple struct types are to be scanned; if additional settings are to be added with c.Var before calling c.Load; or if unsupported type errors are to be ignored. Note that any names scanned from the struct must be unique. If any name is duplicated, Scan panics. This can only happen when calling Scan multiple times or mixing calls to Var and Scan.
If the return value is non-nil, it will be of type Errors. Each element will in turn be of type *UnknownTypeError, one for each scanned field for which a Setter could not be created.
Only exported fields can be set. Unexported fields will be ignored. See the package-level documentation for information about which field types are supported by default and the effects of various struct tags.
func (*Config) SetLoaders ¶
SetLoaders sets loaders to be used by the Config.
It makes a copy of the parameter, so modifying the parameter after the function returns is safe. Similarly, subsequently calling c.AddLoader will not modify the parameter passed to this method.
Ordering is important, as subsequent loaders can overwrite configuration values from earlier ones.
Passing a nil value will cause Config to use a default set of loaders.
func (*Config) SetSetterRegistry ¶
func (c *Config) SetSetterRegistry(reg SetterRegistry)
SetSetterRegistry sets a new SetterRegistry to be used by the Config.
This can be useful for providing custom SetterCreator implementations for new or existing types without changing the global DefaultSetterRegistry.
Passing nil will cause the Config to use the DefaultSetterRegistry.
func (*Config) Usage ¶
Usage dumps usage information to an io.Writer.
Usage writes a list of setting names and their descriptions to w. If w is nil, the list is dumped to os.Stderr.
func (*Config) Var ¶
Var adds a single value to be set by Config.
The value parameter must be a non-nil pointer. Passing any other value will panic. The tag parameter can be used to set most tags as documented in the package-level documentation, with the exception of 'config' and 'prefix' tags.
If value points to a struct, the function is equivalent to calling Scan with value nested at the level indicated by name. For example:
opts := struct { Level1: struct { Level2: struct { X int } } } Scan(&opts)
is equivalent to
Var(&opts.Level1.Level2, "", "Level1", "Level2")
The remaining parameters are variadic, but at least one must be provided. If only one value is provided, it is a non-prefixed name. If multiple values are provided, leading values are prefixes. For example,
DefaultConfig.Var(&version, "", "version")
will parse command-line flag -version and environment variable VERSION, while
DefaultConfig.Var(&version, "", "api", "version")
parses command-line flag -api-version and environment variable API_VERSION.
Any name, with or without prefix, must be unique to the configuration. If it duplicates a name already set with Var or parsed by Scan, Var panics.
type ConversionError ¶
ConversionError is returned for values which can not be convered by a Setter.
func (*ConversionError) Error ¶
func (ce *ConversionError) Error() string
type EnvLoader ¶
type EnvLoader struct {
// contains filtered or unexported fields
}
EnvLoader implements a Loader type to parse settings from environment variables.
Environment variable names are generated by expanding the path for each setting to a list of optional prefixes and ending with the setting's name:
opts := struct { HTMLParser struct { ElementIDs []string Name string } }{}
results in paths
["HTMLParser", "ElementIDs"] ["HTMLParser", "Name"]
Each element is then replaced with the result of passing it to SplitName:
["HTML", "Parser", "Element", "IDs"] ["HTML", "Parser", "Name"]
Any dashes or underscores in each word are removed, the words capitalized, and finally joined with underscores to produce the final names:
HTML_PARSER_ELEMENT_IDS HTML_PARSER_NAME
The zero value is ready to use.
func (*EnvLoader) Init ¶
Init initializes the loader with the given settings.
Init must be called before Load.
func (*EnvLoader) Load ¶
Load attempts to parse the configured settings from environment variables.
The returned error, if non-nil, will be of type Errors. Each element will be of type *ConversionError or *ValidationError.
type Errors ¶
type Errors []error
Errors represents a collection of one or more errors.
func (*Errors) Append ¶
Append adds an error to the list of errors.
If err is of type *Errors, each element in it will be appended individually instead of nesting the *Errors values.
func (*Errors) AsError ¶
AsError returns e as an error type.
If *e is empty, returns error(nil). This is a convenience to avoid a common pitfall:
func foo() error { var *errs Errors return &errs } if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) }
Values of an interface type only compare equal to nil if they have neither a type nor a value. In this case, the return value from foo() will have a nil value, but the type will be (*Errors), so err != nil is true.
type FlagLoader ¶
type FlagLoader struct {
// contains filtered or unexported fields
}
FlagLoader implements a Loader type to parse settings from command line flags.
Command line flag names are generated by expanding the path for each setting to a list of optional prefixes and ending with the setting's name:
opts := struct { HTMLParser struct { ElementIDs []string Name string } }{}
results in paths
["HTMLParser", "ElementIDs"] ["HTMLParser", "Name"]
Each element is then replaced with the result of passing it to SplitName:
["HTML", "Parser", "Element", "IDs"] ["HTML", "Parser", "Name"]
Any dashes in each word are removed, the words lowercased, and finally joined with dashes to produce the final names:
-html-parser-element-ids -html-parser-name
The zero value is ready to use.
func (*FlagLoader) Args ¶
func (fl *FlagLoader) Args() []string
Args returns any command-line arguments that are left after parsing arguments.
It returns nil if Load() has not been called.
func (*FlagLoader) Init ¶
func (fl *FlagLoader) Init(settings []Setting)
Init initializes the loader with the given settings.
Init must be called before Load.
func (*FlagLoader) Load ¶
func (fl *FlagLoader) Load() error
Load attempts to parse the configured settings from command line flags.
The returned error, if non-nil, will be either ErrHelp or an error value returned by (*flag.FlagSet).Parse.
func (*FlagLoader) Name ¶
func (*FlagLoader) Name() string
Name returns the name of the loader. It is always "flag".
func (*FlagLoader) Parse ¶
func (fl *FlagLoader) Parse(args []string) error
Parse loads arguments from the provided slice.
It is similar to fl.Load except that arguments are provided instead of being loaded from os.Args[1:]. It is useful primarily for testing.
The returned error, if non-nil, will be either ErrHelp or an error value returned by (*flag.FlagSet).Parse.
func (*FlagLoader) SetUsageFn ¶
func (fl *FlagLoader) SetUsageFn(fn func())
SetUsageFn sets a function to be executed to provide usage information.
If the -h flag is not overridden and is encountered on the command line when fl.Load is called, the function provided will be called.
This method is called by (*Config).Load to provide detailed usage information for all loaders.
func (*FlagLoader) Usage ¶
func (fl *FlagLoader) Usage() string
Usage returns a string with a list of command line flags and their descriptions.
type Loader ¶
type Loader interface { // Init is used to initialize a loader with a list of settings. Config // will always call it before Load. Implementations should support calling // it more than once. Init([]Setting) // Load will be called to parse and set variables. For each Setting passed // to Init, the implementation should look for an appropriate setting, and, // if found, call one of the 'Set*' methods on Setting.Setter. Load() error // Name should return a name for the loader. The name need not be unique. // However, the name will be used for matching against the `from:""` struct // tag. Name() string // Usage should return a string providing context-specific help for the // given loader. Config will always call Init before calling Usage, so // the implementation may use the Setting variables passed to Init to // provide setting-specific help. Usage() string }
Loader is an interface for parsing and configuring settings using Config.
type Loaders ¶
type Loaders []Loader
Loaders provides a slice type for managing multiple loaders.
func GetDefaultLoaders ¶
func GetDefaultLoaders() Loaders
GetDefaultLoaders returns an ordered list of default loaders.
It currently returns {*EnvLoader, *FlagLoader}. This implies that command line flags can override environment variables.
The returned values are always new values without other references, so can be modified without affecting existing references.
func (Loaders) Copy ¶
Copy makes a copy of l.
This is primarily useful in order to modify the list of loaders based on an existing template without modifying the original. Note that Loader implementations may be pointer types, so while the slice is copied, individual elements may point to shared data.
type NodePath ¶
type NodePath struct { Path // contains filtered or unexported fields }
NodePath represents an intermediate node in a configuration path.
It is associated with one parent NodePath, or none if it is a root. It is also associated with 0 or more child NodePath or Path elements.
func NewRootPath ¶
NewRootPath returns a *NodePath that represents the root of a path hierarchy.
If name is the empty string, the root is unnamed. This is equivalent to the zero value, which is also ready to use.
func (*NodePath) AddNodePath ¶
AddNodePath registers a child NodePath to np.
This makes the child locatable using np.FindNodePath.
func (*NodePath) AddPath ¶
AddPath registers a child Path to np.
This makes the child locatable using np.FindPath.
func (*NodePath) FindNodePath ¶
FindNodePath finds the given path.
The name of np is not considered, so the first parameter will be a child of np. For example, in the hierarchy
NodePath()->NodePath(usr)->NodePath(bin)->Path(python)
Calling FindPath("usr", "bin") on the root node will return NodePath(bin).
If the path is not found within np, or is not an intermediate node (*NodePath), FindNodePath returns nil.
func (*NodePath) FindPath ¶
FindPath finds the given path.
The name of np is not considered, so the first parameter will be a child of np. For example, in the hierarchy
NodePath()->NodePath(usr)->NodePath(bin)->Path(python)
Calling FindPath("usr", "bin", "python") on the root node will return Path(python).
If the path is not found within np, or is not a leaf node (*Path), FindPath returns nil.
func (*NodePath) NewNodePath ¶
NewNodePath returns a new NodePath but without adding it as a child of np.
The returned *NodePath will have np as its parent, but will not be locatable using np.FindNodePath.
type Path ¶
type Path struct {
// contains filtered or unexported fields
}
Path provides a leaf node in a hierarchy of configuration parameters.
When scanning nested structs, for example, there will be a hierarchy such as
TypeNameA +> TypeNameB -> TypeNameC | +-> TypeNameD -> TypeNameE
A Path can refer to the edge nodes, TypeNameC or TypeNameE in this case. Each will have an association with one parent node.
func (*Path) Elements ¶
Elements returns a list of names from the root node to p.
If the root *NodePath is unnamed, it will not be included in the list. Otherwise, the first item will be the name of the root node, and the last will be the name of p. E.g., for
NodePath()->NodePath(usr)->NodePath(bin)->Path(python)
Elements returns
[]string{"usr", "bin", "python"}
type Setter ¶
type Setter interface { // String should return a descriptive string for the current value // associated with the Setter. Note that this method may be called on a // zero value of the implementation type and should not panic in that case. String() string // Set is called to parse, validate and set the associated value from a // string. The implementation must return an error (such as // *ConversionError) if the value cannot be parsed from the given string, or // if string is not a valid type to represent the parameter. Set(string) error // SetInt is called to interpret, validate and set the associated value from // an int64. The implementation must return an error (such as // *ConversionError) if the value cannot be interpreted from the given // int64, or if int64 is not a valid type to represent the parameter. SetInt(int64) error // SetUint is called to interpret, validate and set the associated value // from a uint64. The implementation must return an error (such as // *ConversionError) if the value cannot be interpreted from the given // uint64, or if uint64 is not a valid type to represent the parameter. SetUint(uint64) error // SetFloat is called to interpret, validate and set the associated value // from a float64. The implementation must return an error (such as // *ConversionError) if the value cannot be interpreted from the given // float64, or if float64 is not a valid type to represent the parameter. SetFloat(float64) error // SetBool is called to interpret, validate and set the associated value // from a bool. The implementation must return an error (such as // *ConversionError) if the value cannot be interpreted from the given // bool, or if bool is not a valid type to represent the parameter. SetBool(bool) error // Get is called to get the current value set on the Setter. The type of // the returned value should be the same as the value being set, even if // the value is currently unset or zero. Get() interface{} }
Setter is an interface which is used to parse, validate and set values of a specific type.
Note that SetInt, SetUint, SetFloat and SetBool are not used for the default loaders, but may be used by custom Loader implementations or in future features.
type SetterCreator ¶
type SetterCreator interface { // Type must return the reflect.Type that the Setter returned from Setter // will support. Type() reflect.Type // Setter must return an implementation of Setter to set a value of the // type returned by Type(). The value passed to Setter will always be of // the type returned by Type(), and the Setter must be able to accept // and set that type. Setter(reflect.Value, reflect.StructTag) Setter }
SetterCreator is an interface type for creating a Setter for a given value.
type SetterRegistry ¶
type SetterRegistry map[reflect.Type]SetterCreator
SetterRegistry is a map of reflect.Type to SetterCreator.
It provides methods that can be used to find or create a Setter for a given value.
func (*SetterRegistry) Add ¶
func (sr *SetterRegistry) Add(sc SetterCreator)
Add adds a SetterCreator to the registry.
If there is already a SetterCreator registered for the same type returned by sc.Type(), it will be replaced.
func (*SetterRegistry) Copy ¶
func (sr *SetterRegistry) Copy() *SetterRegistry
Copy creates a copy of the registry.
It is useful for modifying the existing registry without affecting other references to it.
func (*SetterRegistry) GetSetter ¶
GetSetter returns a Setter that wraps/sets the given val.
If the value and type represented by val implements the Setter type, val.Interface() is simply returned.
If val.Type() has an existing entry in the registry, the registered SetterCreator will be used to create the Setter. Otherwise, if val.Type() is a slice or pointer type, it will be dereferenced until either a type is found in the registry, or a non-element type is found. If a Setter is returned for a pointer or slice type, it may be created using wrappers to handle the indirections, so may not be a value returned by one of the registered SetterCreators.
If no SetterCreator can be found or created, GetSetter returns nil.
func (*SetterRegistry) GetSetterCreator ¶
func (sr *SetterRegistry) GetSetterCreator(t reflect.Type) SetterCreator
GetSetterCreator returns the registered SetterCreator for the given type, or nil if none is registered.
type Setting ¶
type Setting struct { // Path is the given path within the configuration hierarchy. This should // be used by Loader implementations to create or identify the parameter. // it will never be nil when passed to Loader.Init(). Path *Path // Tag is the struct tag parsed by Config.Scan or passed to Config.Var. Tag reflect.StructTag // Setter is an implementation of Setter. When passed to Loader.Init(), it // will never be nil. Setter }
Setting represents one configuration value.
It represents a value parsed from either a struct field or passed to Config.Var.
type UnknownTypeError ¶
UnknownTypeError represents an error when no Setter can be created for a type.
func (*UnknownTypeError) Error ¶
func (ute *UnknownTypeError) Error() string
type ValidationError ¶
ValidationError is returned by a Setter when values fail validation.
func (*ValidationError) Error ¶
func (ve *ValidationError) Error() string