Achieve this repo, move it into github.com/iTellis/common
Go package for reading cofig file by JSON, XML, YAML.
go get github.com/iTrellis/common/config
import gopkg.in/yaml.v3
not supported "*.xml": now go encoding/xml is not supported map[string]interface{}
- dot separator to get values, and if return nil, you should set default value
- A: ${X.Y.Z} for finding out X.Y.Z's value and setting into A. See copy example:See config
- You can do like this: c.GetString("a.b.c") Or c.GetString("a.b.c", "default")
- You can write notes into the json file.
- Supported: .json, .yaml
c, e := NewConfig(name)
c.GetString("a.b.c")
// Config manager data functions
type Config interface {
// get a object
GetInterface(key string, defValue ...interface{}) (res interface{})
// get a string
GetString(key string, defValue ...string) (res string)
// get a bool
GetBoolean(key string, defValue ...bool) (b bool)
// get a int
GetInt(key string, defValue ...int) (res int)
// get a float
GetFloat(key string, defValue ...float64) (res float64)
// get list of objects
GetList(key string) (res []interface{})
// get list of strings
GetStringList(key string) []string
// get list of bools
GetBooleanList(key string) []bool
// get list of ints
GetIntList(key string) []int
// get list of float64s
GetFloatList(key string) []float64
// get time duration by (int)(uint), exp: 1s, 1day
GetTimeDuration(key string, defValue ...time.Duration) time.Duration
// get byte size by (int)(uint), exp: 1k, 1m
GetByteSize(key string) *big.Int
// get map value
GetMap(key string) Options
// get key's config
GetConfig(key string) Config
// get key's values if values can be Config, or panic
GetValuesConfig(key string) Config
// set key's value into config
SetKeyValue(key string, value interface{}) (err error)
// get all config
Dump() (bs []byte, err error)
// get all keys
GetKeys() []string
// deep copy configs
Copy() Config
}
[See More Example]
// Reader reader repo
type Reader interface {
// read file into model
Read(model interface{}) error
// dump configs' cache
Dump(model interface{}) ([]byte, error)
// parse data to model
ParseData(data []byte, model interface{}) error
}
r := NewReader(ReaderType, filename)
if err := r.Read(model); err != nil {
return
}
jReader := NewJSONReader() or NewJSONReader(ReaderOptionFilename(filename))
xReader := NewXMLReader() or NewXMLReader(ReaderOptionFilename(filename))
yReader := NewYAMLReader() or NewYAMLReader(ReaderOptionFilename(filename))
if not set filename with reader option function, you can't use Read function, it will return: no such file or directory
-
.json = NewJSONReader()
-
.xml = NewXMLReader()
-
.yaml | .yml = NewYAMLReader()
-
if you want to use a fuzzy reader by filename's suffix
sReader := NewSuffixReader(ReaderOptionFilename(filename))