1.写文档
2.增加二级直接解释的功能
This commit is contained in:
parent
b5e374f640
commit
93682ad23c
38
README.md
38
README.md
@ -1,3 +1,39 @@
|
|||||||
# grconfig
|
# grconfig
|
||||||
|
|
||||||
111
|
使用方法直接看 `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
8
conf/db.yaml
Normal 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
8
conf/dev/db.yaml
Normal 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_"
|
@ -1 +0,0 @@
|
|||||||
Aa: "hello world"
|
|
@ -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) < 2 {
|
if len(chunks) < 1 {
|
||||||
panic("Item string error")
|
panic("Item string error chunk len must more than 1")
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.TrimSpace(env) == "" {
|
if strings.TrimSpace(env) == "" {
|
||||||
@ -82,6 +82,10 @@ 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 {
|
||||||
@ -90,7 +94,6 @@ func recursiveParse(item string, v interface{}) interface{} {
|
|||||||
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]]
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,39 @@
|
|||||||
package grconfig
|
package grconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"fmt"
|
||||||
"lot.gaore.com/library/grconfig"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
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)
|
||||||
}
|
}
|
||||||
|
5
yaml.go
5
yaml.go
@ -25,6 +25,11 @@ func (conf *ConfigerChannelYAML) String(item string) string {
|
|||||||
|
|
||||||
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")
|
||||||
|
Loading…
Reference in New Issue
Block a user