Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

7 changed files with 10 additions and 110 deletions

View File

@ -1,39 +1,2 @@
# grconfig # 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)
```

View File

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

View File

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

1
conf/test.yaml Normal file
View File

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

View File

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

View File

@ -1,43 +1,12 @@
package grconfig package grconfig
import ( import (
"fmt" "log"
"lot.gaore.com/library/grconfig"
"testing" "testing"
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
gr := grconfig.New("conf/")
type Dsn struct { log.Println(gr.String("test.a"))
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,31 +16,15 @@ type ConfigerChannelYAML struct {
func (conf *ConfigerChannelYAML) String(item string) string { func (conf *ConfigerChannelYAML) String(item string) string {
chunks := strings.SplitN(item, ".", 2) chunks := strings.SplitN(item, ".", 2)
var target interface{} target := recursiveParse(chunks[1], conf.tmp)
if len(chunks) > 1 {
target = recursiveParse(chunks[1], conf.tmp)
} else {
target = conf.tmp
}
if str, ok := target.(string); ok { if str, ok := target.(string); ok {
return str return str
} else {
if out, err := yaml.Marshal(target); err == nil {
return bytes.NewBuffer(out).String()
} }
}
return "" return ""
} }
func (conf *ConfigerChannelYAML) Item(item string, t interface{}) (err error) { func (conf *ConfigerChannelYAML) Item(item string, t interface{}) (err error) {
chunks := strings.SplitN(item, ".", 2) chunks := strings.SplitN(item, ".", 2)
if len(chunks) == 1 {
chunks = append(chunks, "")
}
target := recursiveParse(chunks[1], conf.tmp) target := recursiveParse(chunks[1], conf.tmp)
if target == nil { if target == nil {
err = errors.New("item doesn't exists") err = errors.New("item doesn't exists")