diff --git a/README.md b/README.md index 09518b8..8b99400 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,39 @@ # grconfig -111 \ No newline at end of file +使用方法直接看 `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) +``` \ No newline at end of file diff --git a/conf/db.yaml b/conf/db.yaml new file mode 100644 index 0000000..048cf61 --- /dev/null +++ b/conf/db.yaml @@ -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_" \ No newline at end of file diff --git a/conf/dev/db.yaml b/conf/dev/db.yaml new file mode 100644 index 0000000..048cf61 --- /dev/null +++ b/conf/dev/db.yaml @@ -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_" \ No newline at end of file diff --git a/conf/test.yaml b/conf/test.yaml deleted file mode 100644 index 1e6c2cc..0000000 --- a/conf/test.yaml +++ /dev/null @@ -1 +0,0 @@ -Aa: "hello world" \ No newline at end of file diff --git a/grconfig.go b/grconfig.go index 14fdc4f..091dfcb 100644 --- a/grconfig.go +++ b/grconfig.go @@ -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,6 +82,10 @@ 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 { @@ -90,7 +94,6 @@ func recursiveParse(item string, v interface{}) interface{} { v = tmp[n] } } else { - if tmp, ok := v.(map[interface{}]interface{}); reflect.TypeOf(v).Kind() == reflect.Map && ok { v = tmp[chunks[0]] } diff --git a/grconfig_test.go b/grconfig_test.go index 2b6c240..5d172ba 100644 --- a/grconfig_test.go +++ b/grconfig_test.go @@ -1,12 +1,39 @@ 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) } diff --git a/yaml.go b/yaml.go index 0ba931f..1db79b9 100644 --- a/yaml.go +++ b/yaml.go @@ -25,6 +25,11 @@ func (conf *ConfigerChannelYAML) String(item string) string { 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")