# README
Config Reader
Go package for reading cofig file by JSON, XML, YAML.
Installation
go get github.com/iTrellis/common/config
imports
import gopkg.in/yaml.v3
Usage
Config
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")
Feature
// 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
}
More Example
[See More Example]
Reader Repo
// 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
}
Readers
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))
# Functions
DeepCopy 深度拷贝.
NewAdapterConfig return default config adapter name is file's path.
NewConfig return Config by file's path, judge path's suffix, supported .json, .yml, .yaml.
NewConfigOptions 从操作函数解析Config.
NewJSONReader return a json reader.
NewReader return a reader by ReaderType.
NewSuffixReader return a suffix reader supportted: .json, .xml, .yaml, .yml.
NewXMLReader return xml config reader.
NewYAMLReader return a yaml reader.
OptionENVAllowed 允许获取系统环境变量.
OptionENVPrefix 设置环境变量已自定义字符串开始.
OptionFile 解析配置文件Option函数.
OptionString 字符串解析配置Option函数.
OptionStruct 结构体解析配置Option函数.
ParseJSONConfig 解析Json配置.
ParseXMLConfig 解析yaml的配置信息.
ParseYAMLConfig 解析yaml的配置信息.
ReaderOptionFilename set reader filename.
ReadJSONFile 读取Json文件数据到Models.
ReadXMLFile 读取yaml文件的配置信息.
ReadYAMLFile 读取yaml文件的配置信息.
# Constants
ReaderTypeJSON json reader type.
ReaderTypeSuffix judge by file suffix.
ReaderTypeXML xml reader type.
ReaderTypeYAML yaml reader type.
# Variables
Errors.
Errors.
Errors.
Errors.
Errors.
# Type aliases
OptionFunc 处理函数.
Options initial params.
ReaderOptionFunc declare reader option function.
ReaderType define reader type.