Compare commits

...

3 Commits

Author SHA1 Message Date
liangzy
8d7803d99d 1.二级节点直接解释字符串优化
2.根节点不能解释成字符串的bug Fixed
2020-08-10 09:59:29 +08:00
liangzy
93682ad23c 1.写文档
2.增加二级直接解释的功能
2020-06-05 17:01:43 +08:00
liangzy
b5e374f640 更新 'README.md' 2020-04-14 03:07:37 +00:00
7 changed files with 110 additions and 10 deletions

View File

@ -1,2 +1,39 @@
# grconfig
使用方法直接看 `grconfig_test.go`
```go
import "golib.gaore.com/GaoreGo/grconfig"
```
```go
type Dsn struct {
User string `yaml:"user"`
Pass string `yaml:"pass"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Name string `yaml:"name"`
Driver string `yaml:"driver"`
Prefix string `yaml:"prefix"`
}
var items = make(map[string]*Dsn)
// 一级
gr := New("conf/")
fmt.Println(gr.Item("db", &items))
fmt.Printf("%+v\n", items["default"])
// 二级
a := new(Dsn)
gr.Item("db.default", &a)
fmt.Printf("%+v\n", a)
// 解释字符串
fmt.Println("直接解释字符串", gr.String("db.default.host"))
// item形式解释符串
var s string
gr.Item("db.default.host", &s)
fmt.Println("item形式解释符串:", s)
```

8
conf/db.yaml Normal file
View File

@ -0,0 +1,8 @@
default:
user: "jenkins"
pass: "123412412"
host: "rm-w23232z93xv4dds00rl.mysql.rds.aliyuncs.com"
port: 3306
name: "gr_jenkins"
driver: "mysql"
prefix: "gr_"

8
conf/dev/db.yaml Normal file
View File

@ -0,0 +1,8 @@
default:
user: "jenkins"
pass: "123412412"
host: "rm-w23232z93xv4dds00rl.mysql.rds.aliyuncs.com"
port: 3306
name: "gr_jenkins"
driver: "mysql"
prefix: "gr_"

View File

@ -1 +0,0 @@
Aa: "hello world"

View File

@ -29,8 +29,8 @@ func (conf *Configer) getData(item string) (ch ConfigerChannelInterface, err err
env := os.Getenv("CENTER_RUNMODE")
chunks := strings.SplitN(item, ".", 2)
if len(chunks) < 2 {
panic("Item string error")
if len(chunks) < 1 {
panic("Item string error chunk len must more than 1")
}
if strings.TrimSpace(env) == "" {
@ -82,15 +82,16 @@ func (conf *Configer) String(item string) string {
func recursiveParse(item string, v interface{}) interface{} {
if item == "" {
return v
}
chunks := strings.SplitN(item, ".", 2)
if n, err := strconv.Atoi(chunks[0]); err == nil {
if tmp, ok := v.([]interface{}); reflect.TypeOf(v).Kind() == reflect.Slice && ok {
v = tmp[n]
}
} else {
if tmp, ok := v.(map[interface{}]interface{}); reflect.TypeOf(v).Kind() == reflect.Map && ok {
v = tmp[chunks[0]]
}

View File

@ -1,12 +1,43 @@
package grconfig
import (
"log"
"lot.gaore.com/library/grconfig"
"fmt"
"testing"
)
func TestNew(t *testing.T) {
gr := grconfig.New("conf/")
log.Println(gr.String("test.a"))
type Dsn struct {
User string `yaml:"user"`
Pass string `yaml:"pass"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Name string `yaml:"name"`
Driver string `yaml:"driver"`
Prefix string `yaml:"prefix"`
}
var items = make(map[string]*Dsn)
// 一级
gr := New("conf/")
fmt.Println(gr.Item("db", &items))
fmt.Printf("%+v\n", items["default"])
// 二级
a := new(Dsn)
gr.Item("db.default", &a)
fmt.Printf("%+v\n", a)
// 解释字符串
fmt.Println("直接解释字符串", gr.String("db.default.host"))
// item形式解释符串
var s string
gr.Item("db.default.host", &s)
fmt.Println("item形式解释符串:", s)
// 根解释字符串
fmt.Println("根解释字符串", gr.String("db"))
fmt.Println("二级解释字符串", gr.String("db.default"))
}

18
yaml.go
View File

@ -16,15 +16,31 @@ type ConfigerChannelYAML struct {
func (conf *ConfigerChannelYAML) String(item string) string {
chunks := strings.SplitN(item, ".", 2)
target := recursiveParse(chunks[1], conf.tmp)
var target interface{}
if len(chunks) > 1 {
target = recursiveParse(chunks[1], conf.tmp)
} else {
target = conf.tmp
}
if str, ok := target.(string); ok {
return str
} else {
if out, err := yaml.Marshal(target); err == nil {
return bytes.NewBuffer(out).String()
}
}
return ""
}
func (conf *ConfigerChannelYAML) Item(item string, t interface{}) (err error) {
chunks := strings.SplitN(item, ".", 2)
if len(chunks) == 1 {
chunks = append(chunks, "")
}
target := recursiveParse(chunks[1], conf.tmp)
if target == nil {
err = errors.New("item doesn't exists")