Documentation ¶
Overview ¶
Package config is a go config management implement. support YAML,TOML,JSON,INI,HCL format.
Source code and other details for the project are available at GitHub:
https://github.com/gookit/config
JSON format content example:
{ "name": "app", "debug": false, "baseKey": "value", "age": 123, "envKey": "${SHELL}", "envKey1": "${NotExist|defValue}", "map1": { "key": "val", "key1": "val1", "key2": "val2" }, "arr1": [ "val", "val1", "val2" ], "lang": { "dir": "res/lang", "defLang": "en", "allowed": { "en": "val", "zh-CN": "val2" } } }
Usage please see example(more example please see examples folder in the lib):
Example ¶
WithOptions(ParseEnv) // add Decoder and Encoder // use yaml github.com/gookit/config/yaml // AddDriver(Yaml, yaml.Driver) // use toml github.com/gookit/config/toml // AddDriver(Toml, toml.Driver) // use toml github.com/gookit/config/hcl // AddDriver(Hcl, hcl.Driver) // Or // config.DecoderEncoder(config.JSON, yaml.Decoder, yaml.Encoder) err := LoadFiles("testdata/json_base.json") if err != nil { panic(err) } // fmt.Printf("config data: \n %#v\n", Data()) err = LoadFiles("testdata/json_other.json") // LoadFiles("testdata/json_base.json", "testdata/json_other.json") if err != nil { panic(err) } // load from string LoadSources(JSON, []byte(jsonStr)) // fmt.Printf("config data: \n %#v\n", Data()) fmt.Print("get config example:\n") name, ok := String("name") fmt.Printf("- get string\n ok: %v, val: %v\n", ok, name) arr1, ok := Strings("arr1") fmt.Printf("- get array\n ok: %v, val: %#v\n", ok, arr1) val0, ok := String("arr1.0") fmt.Printf("- get sub-value by path 'arr.index'\n ok: %v, val: %#v\n", ok, val0) map1, ok := StringMap("map1") fmt.Printf("- get map\n ok: %v, val: %#v\n", ok, map1) val0, ok = String("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", "")) // set value Set("name", "new name") name, ok = String("name") fmt.Printf("- set string\n ok: %v, val: %v\n", ok, name) // if you want export config data // buf := new(bytes.Buffer) // _, err = config.DumpTo(buf, config.JSON) // if err != nil { // panic(err) // } // fmt.Printf("export config:\n%s", buf.String()) // Out: // get config example: // - get string // ok: true, val: app // - get array // ok: true, val: []string{"val", "val1", "val2"} // - get sub-value by path 'arr.index' // ok: true, val: "val" // - get map // ok: true, val: map[string]string{"key":"val", "key1":"val1", "key2":"val2"} // - get sub-value by path 'map.key' // ok: true, val: "val" // get env 'envKey' val: /bin/zsh // get env 'envKey1' val: defValue // - set string // ok: true, val: new name
Output:
Example (ExportConfig) ¶
// Notice: before dump please set driver encoder // SetEncoder(Yaml, yaml.Encoder) ClearAll() // load from string LoadStrings(JSON, `{ "name": "app", "age": 34 }`) buf := new(bytes.Buffer) _, err := DumpTo(buf, JSON) if err != nil { panic(err) } fmt.Printf("%s", buf.String())
Output: {"age":34,"name":"app"}
Index ¶
- Constants
- Variables
- func AddDriver(driver Driver)
- func Bool(key string) (value bool, ok bool)
- func ClearAll()
- func ClearJSONComments(str string) string
- func Data() map[string]interface{}
- func DefBool(key string, def bool) bool
- func DefFloat(key string, def float64) float64
- func DefInt(key string, def int) int
- func DefInt64(key string, def int64) int64
- func DefString(key string, def string) string
- func DumpTo(out io.Writer, format string) (n int64, err error)
- func EnableCache(opts *Options)
- func Float(key string) (value float64, ok bool)
- func Get(key string, findByPath ...bool) (value interface{}, ok bool)
- func Int(key string) (value int, ok bool)
- func Int64(key string) (value int64, ok bool)
- func IntMap(key string) (mp map[string]int, ok bool)
- func Ints(key string) (arr []int, ok bool)
- func LoadData(dataSource ...interface{}) (err error)
- func LoadExists(sourceFiles ...string) (err error)
- func LoadFiles(sourceFiles ...string) (err error)
- func LoadSources(format string, src []byte, more ...[]byte) (err error)
- func LoadStrings(format string, str string, more ...string) (err error)
- func ParseEnv(opts *Options)
- func Readonly(opts *Options)
- func Set(key string, val interface{}, setByPath ...bool) (err error)
- func SetDecoder(format string, decoder Decoder)
- func SetEncoder(format string, encoder Encoder)
- func String(key string) (value string, ok bool)
- func StringMap(key string) (mp map[string]string, ok bool)
- func Strings(key string) (arr []string, ok bool)
- func WithOptions(opts ...func(*Options))
- func WriteTo(out io.Writer) (n int64, err error)
- type Config
- func (c *Config) AddDriver(driver Driver)
- func (c *Config) Bool(key string) (value bool, ok bool)
- 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) DefFloat(key string, def float64) float64
- func (c *Config) DefInt(key string, def int) int
- func (c *Config) DefInt64(key string, def int64) int64
- func (c *Config) DefString(key string, def string) string
- func (c *Config) DelDriver(format string)
- func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error)
- func (c *Config) Float(key string) (value float64, ok bool)
- func (c *Config) Get(key string, findByPath ...bool) (value interface{}, ok bool)
- func (c *Config) HasDecoder(format string) bool
- func (c *Config) HasEncoder(format string) bool
- func (c *Config) Int(key string) (value int, ok bool)
- func (c *Config) Int64(key string) (value int64, ok bool)
- func (c *Config) IntMap(key string) (mp map[string]int, ok bool)
- func (c *Config) Ints(key string) (arr []int, ok 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, src []byte, more ...[]byte) (err error)
- func (c *Config) LoadStrings(format string, str string, more ...string) (err error)
- func (c *Config) MapStructure(key string, v interface{}) (err error)
- func (c *Config) MustBool(key string) bool
- func (c *Config) MustInt(key string) int
- func (c *Config) MustInt64(key string) int64
- func (c *Config) MustString(key string) string
- func (c *Config) Name() string
- func (c *Config) Options() *Options
- func (c *Config) Set(key string, val interface{}, setByPath ...bool) (err error)
- func (c *Config) SetDecoder(format string, decoder Decoder)
- func (c *Config) SetDecoders(decoders map[string]Decoder)
- func (c *Config) SetEncoder(format string, encoder Encoder)
- func (c *Config) SetEncoders(encoders map[string]Encoder)
- func (c *Config) String(key string) (value string, ok bool)
- func (c *Config) StringMap(key string) (mp map[string]string, ok bool)
- func (c *Config) Strings(key string) (arr []string, ok bool)
- func (c *Config) Structure(key string, v interface{}) (err error)
- func (c *Config) ToJSON() string
- func (c *Config) WithOptions(opts ...func(*Options))
- func (c *Config) WriteTo(out io.Writer) (n int64, err error)
- type Decoder
- type Driver
- type Encoder
- type Options
Examples ¶
Constants ¶
const ( Ini = "ini" Hcl = "hcl" Yml = "yml" JSON = "json" Yaml = "yaml" Toml = "toml" )
There are supported config format
const Version = "1.0.6"
Version is package version
Variables ¶
var JSONDriver = &jsonDriver{JSON}
JSONDriver instance fot json
Functions ¶
func AddDriver ¶ added in v1.0.3
func AddDriver(driver Driver)
AddDriver set a decoder and encoder driver for a format.
func ClearJSONComments ¶ added in v1.0.9
ClearJSONComments for a JSON string
func LoadData ¶
func LoadData(dataSource ...interface{}) (err error)
LoadData load one or multi data
func LoadExists ¶ added in v1.0.3
LoadExists load one or multi files, will ignore not exist
func LoadSources ¶
LoadSources load one or multi byte data
func LoadStrings ¶ added in v1.0.3
LoadStrings load one or multi string
func SetDecoder ¶
SetDecoder add/set a format decoder
func SetEncoder ¶ added in v1.0.2
SetEncoder set a encoder for the format
func WithOptions ¶ added in v1.0.6
func WithOptions(opts ...func(*Options))
WithOptions with options
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config structure definition
func NewWithOptions ¶ added in v1.0.6
NewWithOptions config instance
func (*Config) Bool ¶ added in v1.0.5
Bool 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) DefBool ¶
DefBool get a bool value, if not found return default value
Example ¶
// load from string LoadSources(JSON, []byte(jsonStr)) val, ok := Bool("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) DefFloat ¶ added in v1.0.10
DefFloat get a float value, if not found return default value
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) HasEncoder ¶ added in v1.0.2
HasEncoder has encoder
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) LoadStrings ¶ added in v1.0.3
LoadStrings load data from source string content.
func (*Config) MapStructure ¶
MapStructure alias method of the 'Structure'
func (*Config) MustString ¶ added in v1.0.5
MustString get a string value, if not found return empty string
func (*Config) SetDecoder ¶
SetDecoder set decoder
func (*Config) SetDecoders ¶ added in v1.0.2
SetDecoders set decoders
func (*Config) SetEncoder ¶ added in v1.0.2
SetEncoder set a encoder for the format
func (*Config) SetEncoders ¶ added in v1.0.2
SetEncoders set encoders
func (*Config) Structure ¶ added in v1.0.5
Structure get config data and map to a structure. usage:
dbInfo := Db{} config.Structure("db", &dbInfo)
func (*Config) WithOptions ¶ added in v1.0.6
WithOptions apply some options
type Options ¶
type Options struct { // parse env value. like: "${EnvName}" "${EnvName|default}" ParseEnv bool // config is readonly Readonly bool // enable config data cache EnableCache bool // default write format DumpFormat string // default input format ReadFormat string }
Options config options
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
These are some sample code for YAML,TOML,JSON,INI,HCL
|
These are some sample code for YAML,TOML,JSON,INI,HCL |
Package hcl is driver use HCL format content as config source about HCL, please see https://github.com/hashicorp/hcl
|
Package hcl is driver use HCL format content as config source about HCL, please see https://github.com/hashicorp/hcl |
Package ini is driver use INI format content as config source about ini parse, please see https://github.com/gookit/ini/parser
|
Package ini is driver use INI format content as config source about ini parse, please see https://github.com/gookit/ini/parser |
Package json use the https://github.com/json-iterator/go for parse json
|
Package json use the https://github.com/json-iterator/go for parse json |
Package toml is driver use TOML format content as config source Usage please see example:
|
Package toml is driver use TOML format content as config source Usage please see example: |
Package yaml is a driver use YAML format content as config source Usage please see example:
|
Package yaml is a driver use YAML format content as config source Usage please see example: |