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 Float64(key string) (float64, error)
- func GenerateBlank(e Encoder) ([]byte, error)
- func Get(key string) (interface{}, error)
- func Int(key string) (int, error)
- func MustParse()
- func Parse() error
- func Parsed() bool
- func RegisterFileDecoder(name string, source FileDecoder, fileExtensions ...string)
- func RegisterFileEncoder(name string, encoder Encoder, fileExtensions ...string)
- func RegisterFromStruct(structPtr interface{}, fieldToKeyFunc ...NameToKeyFunc) error
- func RegisterKey(name, description string, defaultVal interface{}, validators ...Validator)
- func RequireKey(name, description string, validators ...Validator)
- func SetConfigr(c *Configr)
- func String(key string) (string, error)
- func ToLowerCamelCase(s string) string
- func Unmarshal(destination interface{}) error
- func UnmarshalKey(key string, destination 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) Read(key string) (interface{}, error)
- func (c *Configr) RegisterFromStruct(structPtr interface{}, fieldToKeyFunc ...NameToKeyFunc) error
- 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)
- func (c *Configr) Unmarshal(destination interface{}) error
- func (c *Configr) UnmarshalKey(key string, destination interface{}) error
- type Encoder
- type EncoderAdapter
- type ErrRequiredKeysMissing
- type File
- type FileDecoder
- type FileDecoderAdapter
- type InvalidTypeError
- type KeySplitter
- type Manager
- type NameToKeyFunc
- type Source
- type SourceAdapter
- type ValidationError
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ( ErrKeyNotFound = errors.New("configr: Key not found") ErrParseHasntBeenCalled = errors.New("configr: Trying to get values before calling Parse()") ErrNoRegisteredValues = errors.New("configr: No registered values to generate") )
var ( RegisteredFileEncoders = make(map[string]Encoder) RegisteredFileDecoders = make(map[string]FileDecoder) ExtensionToDecoderName = make(map[string]string) ExtensionToEncoderName = make(map[string]string) ErrUnknownEncoding = errors.New("configr: Unable to determine file encoding, please set manually") )
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 RegisterFileDecoder ¶ added in v0.3.0
func RegisterFileDecoder(name string, source FileDecoder, fileExtensions ...string)
func RegisterFileEncoder ¶ added in v0.3.0
func RegisterFromStruct ¶ added in v0.6.0
func RegisterFromStruct(structPtr interface{}, fieldToKeyFunc ...NameToKeyFunc) error
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 ToLowerCamelCase ¶ added in v0.6.0
func Unmarshal ¶
func Unmarshal(destination interface{}) error
Unmarshals all parsed values into struct, uses `configr` struct tag for alternative property name. e.g.
type Example struct { property1 string `configr:"myproperty1"` }
func UnmarshalKey ¶ added in v0.6.0
UnmarshalKey unmarshals a subtree of parsed values from Key into a struct. Key follows the same rules as Get.
Types ¶
type Configr ¶
type Configr struct {
// contains filtered or unexported fields
}
func GetConfigr ¶
func GetConfigr() *Configr
func (*Configr) Read ¶ added in v0.2.0
Satisfies github.com/yourheropaul/inj:DatasourceReader interface, not intended for regular configr usage. Use `configr.Get(string)` instead.
func (*Configr) RegisterFromStruct ¶ added in v0.6.0
func (c *Configr) RegisterFromStruct(structPtr interface{}, fieldToKeyFunc ...NameToKeyFunc) error
func (*Configr) RegisterKey ¶
func (*Configr) RequireKey ¶
func (*Configr) SetDescriptionWrapper ¶
func (*Configr) SetIsCaseSensitive ¶
func (*Configr) SetKeyPathDelimeter ¶
func (*Configr) UnmarshalKey ¶ added in v0.6.0
type Encoder ¶
Encoder would be used to encode registered and required values (along with their defaults or descriptions) into bytes.
type EncoderAdapter ¶ added in v0.2.0
EncoderAdapter allows you to convert a func:
func(interface{}) ([]byte, error)
into a type that satisfies the Encoder interface
func (EncoderAdapter) Marshal ¶ added in v0.2.0
func (f EncoderAdapter) Marshal(v interface{}) ([]byte, error)
type ErrRequiredKeysMissing ¶
type ErrRequiredKeysMissing []string
func (ErrRequiredKeysMissing) Error ¶
func (e ErrRequiredKeysMissing) Error() string
type File ¶ added in v0.3.0
type File struct {
// contains filtered or unexported fields
}
func (*File) SetEncodingName ¶ added in v0.3.0
type FileDecoder ¶ added in v0.3.0
type FileDecoderAdapter ¶ added in v0.3.0
func (FileDecoderAdapter) Unmarshal ¶ added in v0.3.0
func (f FileDecoderAdapter) Unmarshal(b []byte, v interface{}) error
type InvalidTypeError ¶ added in v0.6.0
func (InvalidTypeError) Error ¶ added in v0.6.0
func (e InvalidTypeError) Error() string
type KeySplitter ¶ added in v0.4.0
KeySplitter is a function that takes a key path and splits it into its sub-parts:
In: "person.height.inches" Out: []string("person", "height", "inches")
func NewKeySplitter ¶ added in v0.5.0
func NewKeySplitter(delimeter string) KeySplitter
type NameToKeyFunc ¶ added in v0.6.0
Converts a Struct Name or Field into an appropriate key name:
In: "FromAddress" Out: "fromAddress"
type Source ¶
type Source interface {
Unmarshal([]string, KeySplitter) (map[string]interface{}, error)
}
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. First arg is a slice of all expected keys.
type SourceAdapter ¶
type SourceAdapter func([]string, KeySplitter) (map[string]interface{}, error)
SourceAdapter allows you to convert a func:
func() (map[string]interface{}, error)
into a type that satisfies the Source interface
func (SourceAdapter) Unmarshal ¶
func (f SourceAdapter) Unmarshal(keys []string, keySplitterFn KeySplitter) (map[string]interface{}, error)
type ValidationError ¶ added in v0.5.0
func NewValidationError ¶ added in v0.5.0
func NewValidationError(key string, err error) ValidationError
func (ValidationError) Error ¶ added in v0.5.0
func (v ValidationError) Error() string