Documentation ¶
Overview ¶
Package onion is a layer based, pluggable config manager for golang.
Layers ¶
Each config object can has more than one config layer. currently there is 3 layer type is supported.
Default layer ¶
This layer is special layer to set default for configs. usage is simple :
l := onion.NewDefaultLayer() l.SetDefault("my.daughter.name", "bita")
This layer must be added before all other layer, and defaults must be added before adding it to onion ¶
File layer ¶
File layer is the basic one.
l := onion.NewFileLayer("/path/to/the/file.ext")
the onion package only support for json extension by itself, and there is toml and yaml loader available as sub package for this one.
Also writing a new loader is very easy, just implement the FileLoader interface and call the RegisterLoader function with your loader object
Folder layer ¶
Folder layer is much like file layer but it get a folder and search for the first file with tha specific name and supported extension
l := onion.NewFolderLayer("/path/to/folder", "filename")
the file name part is WHITOUT extension. library check for supported loader extension in that folder and return the first one.
ENV layer ¶
The other layer is env layer. this layer accept a whitelist of env variables and use them as value .
l := onion.NewEnvLayer("PORT", "STATIC_ROOT", "NEXT")
this layer currently dose not support nested variables.
YOUR layer ¶
Just implement the onion.Layer interface!
Getting from config ¶
After adding layers to config, its easy to get the config values.
o := onion.New() o.AddLayer(l1) o.AddLayer(l2) o.GetString("key", "default") o.GetBool("anotherkey", true) o.GetInt("worker.count", 10) // Nested value
library also support for mapping data to a structure. define your structure :
type MyStruct struct { Key1 string Key2 int Key3 bool `onion:"boolkey"` // struct tag is supported to change the name Other struct { Nested string } } o := onion.New() // Add layers..... c := MyStruct{} o.GetStruct("prefix", &c)
the the c.Key1 is equal to o.GetStringDefault("prefix.key1", c.Key1) , note that the value before calling this function is used as default value, when the type is not matched or the value is not exists, the the default is returned For changing the key name, struct tag is supported. for example in the above example c.Key3 is equal to o.GetBoolDefault("prefix.boolkey", c.Key3)
Also nested struct (and embeded ones) are supported too.
Index ¶
- func RegisterLoader(l FileLoader)
- type DefaultLayer
- type FileLoader
- type Layer
- type Onion
- func (o *Onion) AddLayer(l Layer) error
- func (o *Onion) Get(key string) (interface{}, bool)
- func (o *Onion) GetBool(key string) bool
- func (o *Onion) GetBoolDefault(key string, def bool) bool
- func (o *Onion) GetDelimiter() string
- func (o *Onion) GetDuration(key string) time.Duration
- func (o *Onion) GetDurationDefault(key string, def time.Duration) time.Duration
- func (o *Onion) GetInt(key string) int
- func (o *Onion) GetInt64(key string) int64
- func (o *Onion) GetInt64Default(key string, def int64) int64
- func (o *Onion) GetIntDefault(key string, def int) int
- func (o *Onion) GetString(key string) string
- func (o *Onion) GetStringDefault(key string, def string) string
- func (o *Onion) GetStringSlice(key string) []string
- func (o *Onion) GetStruct(prefix string, s interface{})
- func (o *Onion) SetDelimiter(d string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterLoader ¶
func RegisterLoader(l FileLoader)
RegisterLoader must be called to register a type loader, this function is only available with file and folder loaders.
Types ¶
type DefaultLayer ¶
type DefaultLayer interface { Layer // SetDefault set a default value for a key SetDefault(string, interface{}) error // GetDelimiter is used to get current delimiter for this layer. since // this layer needs to work with keys, the delimiter is needed GetDelimiter() string // SetDelimiter is used to set delimiter on this layer SetDelimiter(d string) }
DefaultLayer is a layer to handle defalt value for layer.
func NewDefaultLayer ¶
func NewDefaultLayer() DefaultLayer
NewDefaultLayer is used to return a default layer. shoud load this layer before any other layer, and before ading it, must add default value before adding this layer to onion.
type FileLoader ¶
type FileLoader interface { // Must return the list of supported ext for this loader interface SupportedEXT() []string // Convert is for translating the file data into config structure. Convert(io.Reader) (map[string]interface{}, error) }
FileLoader is an interface to handle load config from a file
type Layer ¶
Layer is an interface to handle the load phase.
func NewEnvLayer ¶
NewEnvLayer create a environment loader. this loader accept a whitelist of allowed variables TODO : find a way to map env variable with different name
func NewFileLayer ¶
NewFileLayer initialize a new file layer. its for a single file, and the file ext is the key for loader to load a correct loader. if you want to scan a directory, use the folder loader.
func NewFolderLayer ¶
NewFolderLayer return a new folder layer, this layer search in a folder for all supported file, and when it hit the first loadable file then simply return it the config name must not contain file extension
type Onion ¶
type Onion struct {
// contains filtered or unexported fields
}
Onion is a layer base configuration system
func (*Onion) AddLayer ¶
AddLayer add a new layer to the end of config layers. last layer is loaded after all other layer
func (*Onion) GetBoolDefault ¶
GetBoolDefault return bool value from Onion. if the value is not exists or if tha value is not boolean, return the default
func (*Onion) GetDelimiter ¶
GetDelimiter return the delimiter for nested key
func (*Onion) GetDuration ¶
GetDuration is for getting duration from config, it cast both int and string to duration
func (*Onion) GetDurationDefault ¶
GetDurationDefault is a function to get duration from config. it support both string duration (like 1h3m2s) and integer duration
func (*Onion) GetInt ¶
GetInt return an int value, if the value is not there, then it return zero value
func (*Onion) GetInt64Default ¶
GetInt64Default return an int64 value from Onion, if the value is not exists or if the value is not int64 then return the default
func (*Onion) GetIntDefault ¶
GetIntDefault return an int value from Onion, if the value is not exists or its not an integer , default is returned
func (*Onion) GetStringDefault ¶
GetStringDefault get a string from Onion. if the value is not exists or if tha value is not string, return the default
func (*Onion) GetStringSlice ¶
GetStringSlice try to get a slice from the config
func (*Onion) GetStruct ¶
GetStruct fill an structure base on the config nested set, this function use reflection, and its not good (in my opinion) for frequent call. but its best if you need the config to loaded in structure and use that structure after that.
func (*Onion) SetDelimiter ¶
SetDelimiter set the current delimiter
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package flagslayer is used to handle flags as a configuration layer this package is compatible with standard flags library
|
Package flagslayer is used to handle flags as a configuration layer this package is compatible with standard flags library |
Package tomlloader is used to handle toml file in Onion file/folder layer.
|
Package tomlloader is used to handle toml file in Onion file/folder layer. |
Package yamlloader is used to handle yaml file in Onion file/folder layer.
|
Package yamlloader is used to handle yaml file in Onion file/folder layer. |