Documentation
¶
Overview ¶
golang application config manage implement. support yaml,toml,json format.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/config
Here using the yaml format as an example(yml_other.yml):
name: app2
debug: false
baseKey: value2
map1:
key: val2
key2: val20
arr1:
- val1
- val21
usage please see examples:
Example (ExportConfig) ¶
// Notice: before dump please set driver encoder
SetEncoder(Yaml, yaml.Encoder)
buf := new(bytes.Buffer)
_, err := DumpTo(buf, Yaml)
if err != nil {
panic(err)
}
fmt.Printf("export config:\n%s", buf.String())
Output: arr1: - val1 - val21 baseKey: value2 debug: false ... ...
Example (UseToml) ¶
SetOptions(&Options{
ParseEnv: true,
})
SetDriver(Toml, toml.Decoder, toml.Encoder)
err := LoadFiles("testdata/toml_base.toml")
if err != nil {
panic(err)
}
// fmt.Printf("config data: \n %#v\n", Data())
// load more files
err = LoadFiles("testdata/toml_other.toml")
// can also
// LoadFiles("testdata/toml_base.toml", "testdata/toml_other.toml")
if err != nil {
panic(err)
}
// load from string
LoadSources(Toml, []byte(tomlStr))
// fmt.Printf("config data: \n %#v\n", Data())
fmt.Print("get config example:\n")
name, ok := GetString("name")
fmt.Printf("- get string\n ok: %v, val: %v\n", ok, name)
arr1, ok := GetStringArr("arr1")
fmt.Printf("- get array\n ok: %v, val: %#v\n", ok, arr1)
val0, ok := GetString("arr1.0")
fmt.Printf("- get sub-value by path 'arr.index'\n ok: %v, val: %v\n", ok, val0)
map1, ok := GetStringMap("map1")
fmt.Printf("- get map\n ok: %v, val: %#v\n", ok, map1)
val0, ok = GetString("map1.name")
fmt.Printf("- get sub-value by path 'map.key'\n ok: %v, val: %v\n", ok, val0)
// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", DefString("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", DefString("envKey1", ""))
Output: get config example: - get string ok: true, val: app2 - get array ok: true, val: []string{"alpha", "omega"} - get sub-value by path 'arr.index' ok: true, val: alpha - get map ok: true, val: map[string]string{"name":"Tom Preston-Werner", "org":"GitHub"} - get sub-value by path 'map.key' ok: true, val: Tom Preston-Werner get env 'envKey' val: /bin/zsh get env 'envKey1' val: defValue
Example (UseYaml) ¶
// add yaml decoder
SetDecoder(Yaml, yaml.Decoder)
err := LoadFiles("testdata/yml_other.yml")
if err != nil {
panic(err)
}
// load from string
LoadSources(Yaml, []byte(yamlStr))
fmt.Print("get config example:\n")
name, ok := GetString("name")
fmt.Printf("- get string\n ok: %v, val: %v\n", ok, name)
arr1, ok := GetStringArr("arr1")
fmt.Printf("- get array\n ok: %v, val: %#v\n", ok, arr1)
val0, ok := GetString("arr1.0")
fmt.Printf("- get sub-value by path 'arr.index'\n ok: %v, val: %#v\n", ok, val0)
map1, ok := GetStringMap("map1")
fmt.Printf("- get map\n ok: %v, val: %#v\n", ok, map1)
val0, ok = GetString("map1.key")
fmt.Printf("- get sub-value by path 'map.key'\n ok: %v, val: %#v\n", ok, val0)
// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", DefString("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", DefString("envKey1", ""))
Output: get config example: - get string ok: true, val: app2 - get array ok: true, val: []string{"val1", "val21"} - get sub-value by path 'arr.index' ok: true, val: "val1" - get map ok: true, val: map[string]string{"key":"val2", "key2":"val20"} - get sub-value by path 'map.key' ok: true, val: "val2" get env 'envKey' val: /bin/zsh get env 'envKey1' val: defValue
Index ¶
- Constants
- func Data() map[string]interface{}
- func DefBool(key string, def bool) bool
- func DefInt(key string, def int) int
- func DefString(key string, def string) string
- func DumpTo(out io.Writer, format string) (n int64, err error)
- func Get(key string, findByPath ...bool) (value interface{}, ok bool)
- func GetBool(key string) (value bool, ok bool)
- func GetInt(key string) (value int, ok bool)
- func GetString(key string) (value string, ok bool)
- func GetStringArr(key string) (arr []string, ok bool)
- func GetStringMap(key string) (mp map[string]string, ok bool)
- func LoadData(dataSource ...interface{}) (err error)
- func LoadFiles(sourceFiles ...string) (err error)
- func LoadSources(format string, sourceCode ...[]byte) (err error)
- func Set()
- func SetDecoder(format string, decoder Decoder)
- func SetDecoders(decoders map[string]Decoder)
- func SetDriver(format string, decoder Decoder, encoder Encoder)
- func SetEncoder(format string, encoder Encoder)
- func SetEncoders(encoders map[string]Encoder)
- func SetOptions(opts *Options)
- func WriteTo(out io.Writer) (n int64, err error)
- type Config
- func (c *Config) ClearAll()
- func (c *Config) ClearCaches()
- func (c *Config) ClearData()
- func (c *Config) Data() map[string]interface{}
- func (c *Config) DefBool(key string, def bool) bool
- func (c *Config) DefInt(key string, def int) int
- func (c *Config) DefString(key string, def string) string
- func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error)
- func (c *Config) Get(key string, findByPath ...bool) (value interface{}, ok bool)
- func (c *Config) GetBool(key string) (value bool, ok bool)
- func (c *Config) GetInt(key string) (value int, ok bool)
- func (c *Config) GetString(key string) (value string, ok bool)
- func (c *Config) GetStringArr(key string) (arr []string, ok bool)
- func (c *Config) GetStringMap(key string) (mp map[string]string, ok bool)
- func (c *Config) GetStructure(key string, v interface{}) (err error)
- func (c *Config) HasDecoder(format string) bool
- func (c *Config) HasEncoder(format string) bool
- func (c *Config) LoadData(dataSources ...interface{}) (err error)
- func (c *Config) LoadExists(sourceFiles ...string) (err error)
- func (c *Config) LoadFiles(sourceFiles ...string) (err error)
- func (c *Config) LoadSources(format string, sourceCodes ...[]byte) (err error)
- func (c *Config) MapStructure(key string, v interface{}) (err error)
- func (c *Config) Name() string
- func (c *Config) Readonly(readonly bool)
- func (c *Config) Set(key string, val interface{}) (err error)
- func (c *Config) SetDecoder(format string, decoder Decoder)
- func (c *Config) SetDecoders(decoders map[string]Decoder)
- func (c *Config) SetDriver(format string, decoder Decoder, encoder Encoder)
- func (c *Config) SetEncoder(format string, encoder Encoder)
- func (c *Config) SetEncoders(encoders map[string]Encoder)
- func (c *Config) SetOptions(opts *Options)
- func (c *Config) WriteTo(out io.Writer) (n int64, err error)
- type Decoder
- type Encoder
- type Options
Examples ¶
Constants ¶
const ( Json = "json" Yml = "yml" Yaml = "yaml" Toml = "toml" )
supported config format
const Version = "1.0.1"
package version
Variables ¶
This section is empty.
Functions ¶
func GetStringMap ¶
GetStringMap get config data as a map[string]string
func SetDecoder ¶
SetDecoder add/set a format decoder
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config
func (*Config) DefBool ¶
DefBool get a bool value, if not found return default value
Example ¶
val, ok := GetBool("debug")
fmt.Printf("get 'debug', ok: %v, val: %v\n", ok, val)
val1 := DefBool("debug", false)
fmt.Printf("get 'debug' with default, val: %v\n", val1)
Output: get 'debug', ok: true, val: true get 'debug' with default, val: true
func (*Config) DumpTo ¶ added in v1.0.2
DumpTo use the format(json,yaml,toml) dump config data to a writer
func (*Config) Get ¶
Get config value by key string, support get sub-value by key path(eg. 'map.key'), ok is true, find value from config ok is false, not found or error
func (*Config) GetBool ¶
GetBool Looks up a value for a key in this section and attempts to parse that value as a boolean, along with a boolean result similar to a map lookup. of following( case insensitive):
- true
- yes
- false
- no
- 1
- 0
The `ok` boolean will be false in the event that the value could not be parsed as a bool
func (*Config) GetStringArr ¶
GetStringArr get config data as a slice/array
func (*Config) GetStringMap ¶
GetStringMap get config data as a map[string]string
func (*Config) GetStructure ¶
GetStructure get config data and map to a structure. usage:
dbInfo := Db{}
config.GetStructure("db", &dbInfo)
func (*Config) HasEncoder ¶ added in v1.0.2
HasEncoder
func (*Config) LoadExists ¶
LoadExists load and parse config files, but will ignore not exists file.
func (*Config) LoadSources ¶
LoadSources load data from byte content. usage:
config.LoadSources(config.Yml, []byte(` name: blog arr: key: val
`))
func (*Config) MapStructure ¶
MapStructure alias method of the 'GetStructure'
func (*Config) SetDecoders ¶ added in v1.0.2
SetDecoders
func (*Config) SetEncoder ¶ added in v1.0.2
SetEncoder
func (*Config) SetEncoders ¶ added in v1.0.2
SetEncoders