Documentation ¶
Overview ¶
Configr provides an abstraction above configuration sources, allowing you to use a single interface to get all your configuration values
Index ¶
- Variables
- func AddSource(p Source)
- func Bool(key string) (bool, error)
- func FilePath() string
- func Float64(key string) (float64, error)
- func GenerateBlank(e Encoder) ([]byte, error)
- func Get(key string) (interface{}, error)
- func Int(key string) (int, error)
- func Marshal(v interface{}) ([]byte, error)
- func MustParse()
- func Parse() error
- func Parsed() bool
- func RegisterKey(name, description string, defaultVal interface{}, validators ...Validator)
- func RequireKey(name, description string, validators ...Validator)
- func SetConfigr(c *Configr)
- func SetEncoding(encoding Encoding)
- func SetFilePath(path string)
- func String(key string) (string, error)
- func Unmarshal() (map[string]interface{}, error)
- type Config
- type Configr
- func (c *Configr) AddSource(p Source)
- func (c *Configr) Bool(key string) (bool, error)
- func (c *Configr) Float64(key string) (float64, error)
- func (c *Configr) GenerateBlank(e Encoder) ([]byte, error)
- func (c *Configr) Get(key string) (interface{}, error)
- func (c *Configr) Int(key string) (int, error)
- func (c *Configr) MustParse()
- func (c *Configr) Parse() error
- func (c *Configr) Parsed() bool
- func (c *Configr) RegisterKey(name, description string, defaultVal interface{}, validators ...Validator)
- func (c *Configr) RequireKey(name, description string, validators ...Validator)
- func (c *Configr) SetDescriptionWrapper(wrapper string)
- func (c *Configr) SetIsCaseSensitive(isCaseSensitive bool)
- func (c *Configr) SetKeyPathDelimeter(delimeter string)
- func (c *Configr) String(key string) (string, error)
- type Encoder
- type Encoding
- type ErrRequiredKeysMissing
- type FileSource
- type Manager
- type Source
- type SourceAdapter
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ErrKeyNotFound = errors.New("configr: Key not found")
var ErrNoRegisteredValues = errors.New("configr: No registered values to generate")
var ErrParseHasntBeenCalled = errors.New("configr: Trying to get values before calling Parse()")
var ErrUnknownEncoding = errors.New("configr: Unable to determine file encoding, please set manually")
var SupportedFileExtensions = []string{"json", "toml"}
Functions ¶
func AddSource ¶
func AddSource(p Source)
AddSource registers Sources with the Configr instance to Unmarshal() when Parse() is called. Sources are parsed in a FILO order, meaning the first source added is considered the highest priority, and any keys from lower priority sources that are present in a higher will be overwritten
func Float64 ¶
Float64 wraps Get() and will attempt to cast the resulting value to a float64 or error
func GenerateBlank ¶
GenerateBlank generates a 'blank' configuration using the passed Encoder, it will honour nested keys, use default values where possible and when not fall back to placing the description as the value.
func Get ¶
Get can only be called after a Parse() has been done. Keys support the nested notation format:
"user.age.month"
If a key is not found but has been registered with a default, the default will be returned
func MustParse ¶
func MustParse()
MustParse wraps Parse() and will panic if there are any resulting errors
func Parse ¶
func Parse() error
Parse calls Unmarshal on all registered sources, and caches the subsequent key/value's. Additional calls to Parse can be made to add additional config from sources.
Sources are called in a FILO order, meaning the first source added is considered the highest priority, any keys set from lower priority sources found in higher priority will be overwritten.
func RegisterKey ¶
RegisterKey registers a configuration key (name) along with a description of what the configuration key is for, a default value and optional validators
name supports nested notation in the form of '.' delimitered keys (unless changed) e.g.
"user.age.month"
func RequireKey ¶
RequireValue wraps the RegisterValue() call but upon parsing sources, if the configuration key (name) is not found, Parse() will return a ErrRequiredValuesMissing error
func SetConfigr ¶
func SetConfigr(c *Configr)
func SetEncoding ¶
func SetEncoding(encoding Encoding)
SetEncoding allows the caller to override the infered file encoding format
func SetFilePath ¶
func SetFilePath(path string)
SetFilePath sets the file path of the configuration file and try to determine the encoding of the file using its extension. See SupportedFileExtensions for a list of supported extensions
Types ¶
type Configr ¶
type Configr struct {
// contains filtered or unexported fields
}
func GetConfigr ¶
func GetConfigr() *Configr
func (*Configr) RegisterKey ¶
func (*Configr) RequireKey ¶
func (*Configr) SetDescriptionWrapper ¶
func (*Configr) SetIsCaseSensitive ¶
func (*Configr) SetKeyPathDelimeter ¶
type Encoder ¶
Encoder would be used to encode registered and required values (along with their defaults or descriptions) into bytes.
type ErrRequiredKeysMissing ¶
type ErrRequiredKeysMissing []string
func (ErrRequiredKeysMissing) Error ¶
func (e ErrRequiredKeysMissing) Error() string
type FileSource ¶
type FileSource struct {
// contains filtered or unexported fields
}
func NewFileSource ¶
func NewFileSource(filePath string) *FileSource
func (*FileSource) FilePath ¶
func (f *FileSource) FilePath() string
func (*FileSource) Marshal ¶
func (f *FileSource) Marshal(v interface{}) ([]byte, error)
func (*FileSource) SetEncoding ¶
func (f *FileSource) SetEncoding(encoding Encoding)
func (*FileSource) SetFilePath ¶
func (f *FileSource) SetFilePath(path string)
func (*FileSource) Unmarshal ¶
func (f *FileSource) Unmarshal() (map[string]interface{}, error)
type Source ¶
Source is a source of configuration keys and values, calling unmarshal should return a map[string]interface{} of all key/value pairs (nesting is supported) with multiple types.
type SourceAdapter ¶
func (SourceAdapter) Unmarshal ¶
func (f SourceAdapter) Unmarshal() (map[string]interface{}, error)