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_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")
chunks := strings.SplitN(item, ".", 2)
if len(chunks) < 1 {
panic("Item string error chunk len must more than 1")
if len(chunks) < 2 {
panic("Item string error")
}
if strings.TrimSpace(env) == "" {
@ -82,16 +82,15 @@ 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,43 +1,12 @@
package grconfig
import (
"fmt"
"log"
"lot.gaore.com/library/grconfig"
"testing"
)
func TestNew(t *testing.T) {
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"))
gr := grconfig.New("conf/")
log.Println(gr.String("test.a"))
}

18
yaml.go
View File

@ -16,31 +16,15 @@ type ConfigerChannelYAML struct {
func (conf *ConfigerChannelYAML) String(item string) string {
chunks := strings.SplitN(item, ".", 2)
var target interface{}
if len(chunks) > 1 {
target = recursiveParse(chunks[1], conf.tmp)
} else {
target = conf.tmp
}
target := recursiveParse(chunks[1], 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")