From 7eb39c4423503756b8a4587f5c76977b22b63b85 Mon Sep 17 00:00:00 2001 From: inhere Date: Thu, 19 Jul 2018 11:40:03 +0800 Subject: [PATCH] add new driver INI, add some demos, some logic update. --- README.md | 4 +-- config.go | 3 +- config_load.go | 4 +++ doc.go | 2 +- examples/ini-parse-packages.md | 41 ------------------------ examples/ini.go | 57 ++++++++++++++++++++++++++++++++++ ini/ini.go | 46 +++++++++++++++++++++++++++ testdata/ini_base.ini | 11 +++++++ testdata/ini_other.ini | 8 +++++ 9 files changed, 131 insertions(+), 45 deletions(-) delete mode 100644 examples/ini-parse-packages.md create mode 100644 examples/ini.go create mode 100644 ini/ini.go create mode 100644 testdata/ini_base.ini create mode 100644 testdata/ini_other.ini diff --git a/README.md b/README.md index 3539c3b..28e0890 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ [![GoDoc](https://godoc.org/github.com/gookit/config?status.svg)](https://godoc.org/github.com/gookit/config) -golang application config manage implement. +golang application config manage tool library. -- support multi format: `JSON`(default), `YAML`, `TOML`, `HCL` +- support multi format: `JSON`(default), `INI`, `YAML`, `TOML`, `HCL` - support multi file/data load - support data override merge - support get sub value by path, like `map.key` `arr.2` diff --git a/config.go b/config.go index 959effe..13ffd98 100644 --- a/config.go +++ b/config.go @@ -12,9 +12,10 @@ const Version = "1.0.3" // supported config format const ( - Json = "json" + Ini = "ini" Hcl = "hcl" Yml = "yml" + Json = "json" Yaml = "yaml" Toml = "toml" ) diff --git a/config_load.go b/config_load.go index 44ba0a6..87b5428 100644 --- a/config_load.go +++ b/config_load.go @@ -126,6 +126,10 @@ func (c *Config) parseSourceCode(format string, blob []byte) (err error) { var decoder Decoder switch format { + case Hcl: + decoder, ok = c.decoders[Hcl] + case Ini: + decoder, ok = c.decoders[Ini] case Json: decoder, ok = c.decoders[Json] case Yaml, Yml: diff --git a/doc.go b/doc.go index f1dd8e6..c75ce0f 100644 --- a/doc.go +++ b/doc.go @@ -1,5 +1,5 @@ /* -golang application config manage implement. support YAML,TOML,JSON,HCL format. +golang application config manage implement. support YAML,TOML,JSON,INI,HCL format. Source code and other details for the project are available at GitHub: diff --git a/examples/ini-parse-packages.md b/examples/ini-parse-packages.md deleted file mode 100644 index 49977e0..0000000 --- a/examples/ini-parse-packages.md +++ /dev/null @@ -1,41 +0,0 @@ -# ini config read - -```go -package main - -import ( - newini "github.com/ochinchina/go-ini" - kini "github.com/knq/ini" - dini "github.com/dombenson/go-ini" -) - - // "gopkg.in/ini.v1" 多个数据源合并后,获取section数据有误 - - Cfg, err := ini.Load("conf/app.ini", envFile) - // 只读时提升性能 - Cfg.BlockMode = false - - // newini "github.com/ochinchina/go-ini" - config := newini.Load("conf/app.ini", envFile) - rds, err := config.GetSection("redis") - fmt.Printf("merged: %s \n rds server: %s \n", config.String(), rds.Key("server").String()) - - // kini "github.com/knq/ini" 只能单个文件 - cfg1, err := kini.LoadFile("conf/app.ini") - fmt.Printf("merged: %s \n rds server: %s \n", cfg1.String(), cfg1.GetSection("redis").Get("server")) - - // dini "github.com/dombenson/go-ini" - cfg2, err := dini.LoadFile("conf/app.ini") - cfg3, err := dini.LoadFile(envFile) - - cfg2.WriteTo(os.Stdout) - - fmt.Printf("rds server: %+v \n", cfg2.Values("redis")) - - // cfg3 copy to cfg2 通过copy,可以合并多个文件 - cfg3.Copy(cfg2) - - cfg2.WriteTo(os.Stdout) - fmt.Printf("log conf: %+v \n", cfg2.Values("log")) - -``` diff --git a/examples/ini.go b/examples/ini.go new file mode 100644 index 0000000..8c2dc7a --- /dev/null +++ b/examples/ini.go @@ -0,0 +1,57 @@ +// These are some sample code for YAML,TOML,JSON,INI,HCL +package main + +import ( + "github.com/gookit/config" + "github.com/gookit/config/ini" + "fmt" +) + +// go run ./examples/ini.go +func main() { + config.SetOptions(&config.Options{ + ParseEnv: true, + }) + + // add Decoder and Encoder + config.AddDriver(config.Ini, ini.Driver) + // Or + // config.DecoderEncoder(config.Ini, ini.Decoder, ini.Encoder) + + err := config.LoadFiles("testdata/ini_base.ini") + if err != nil { + panic(err) + } + + fmt.Printf("config data: \n %#v\n", config.Data()) + + err = config.LoadFiles("testdata/ini_other.ini") + // config.LoadFiles("testdata/ini_base.ini", "testdata/ini_other.ini") + if err != nil { + panic(err) + } + + fmt.Printf("config data: \n %#v\n", config.Data()) + fmt.Print("get config example:\n") + + name, ok := config.GetString("name") + fmt.Printf("- get string\n ok: %v, val: %v\n", ok, name) + + // NOTICE: ini is not support array + + map1, ok := config.GetStringMap("map1") + fmt.Printf("- get map\n ok: %v, val: %#v\n", ok, map1) + + val0, ok := config.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", config.DefString("envKey", "")) + fmt.Printf("get env 'envKey1' val: %s\n", config.DefString("envKey1", "")) + + // set value + config.Set("name", "new name") + name, ok = config.GetString("name") + fmt.Printf("- set string\n ok: %v, val: %v\n", ok, name) + +} diff --git a/ini/ini.go b/ini/ini.go new file mode 100644 index 0000000..ecc35e7 --- /dev/null +++ b/ini/ini.go @@ -0,0 +1,46 @@ +/* +This is driver use INI format content as config source + +about ini parse, please see https://github.com/gookit/ini/parser + */ +package ini + +import ( + "github.com/gookit/config" + "github.com/gookit/ini/parser" + "errors" +) + +// Decoder the ini content decoder +var Decoder config.Decoder = func(blob []byte, ptr interface{}) (err error) { + return parser.Decode(blob, ptr) +} + +// Encoder encode data to ini content +var Encoder config.Encoder = func(ptr interface{}) (out []byte, err error) { + err = errors.New("INI: is not support encode data to INI") + return +} + +// Driver +var Driver = &iniDriver{config.Ini} + +// iniDriver for ini format content +type iniDriver struct { + name string +} + +// Name +func (d *iniDriver) Name() string { + return d.name +} + +// GetDecoder for ini +func (d *iniDriver) GetDecoder() config.Decoder { + return Decoder +} + +// GetEncoder for ini +func (d *iniDriver) GetEncoder() config.Encoder { + return Encoder +} diff --git a/testdata/ini_base.ini b/testdata/ini_base.ini new file mode 100644 index 0000000..7d050d5 --- /dev/null +++ b/testdata/ini_base.ini @@ -0,0 +1,11 @@ +name = app +debug = false +baseKey = value +age = 123 +envKey = ${SHELL} +envKey1 = ${NotExist|defValue} + +[map1] +key = val +key1 = val1 +key2 = val2 diff --git a/testdata/ini_other.ini b/testdata/ini_other.ini new file mode 100644 index 0000000..8ef0ff4 --- /dev/null +++ b/testdata/ini_other.ini @@ -0,0 +1,8 @@ +name = app2 +debug = false +age = 12 +baseKey = value2 + +[map1] +key = val2 +key2 = val20