1.二级节点直接解释字符串优化
2.根节点不能解释成字符串的bug Fixed
This commit is contained in:
parent
93682ad23c
commit
8d7803d99d
@ -85,11 +85,9 @@ func recursiveParse(item string, v interface{}) interface{} {
|
|||||||
if item == "" {
|
if item == "" {
|
||||||
return v
|
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]
|
||||||
}
|
}
|
||||||
|
@ -36,4 +36,8 @@ func TestNew(t *testing.T) {
|
|||||||
var s string
|
var s string
|
||||||
gr.Item("db.default.host", &s)
|
gr.Item("db.default.host", &s)
|
||||||
fmt.Println("item形式解释符串:", s)
|
fmt.Println("item形式解释符串:", s)
|
||||||
|
|
||||||
|
// 根解释字符串
|
||||||
|
fmt.Println("根解释字符串", gr.String("db"))
|
||||||
|
fmt.Println("二级解释字符串", gr.String("db.default"))
|
||||||
}
|
}
|
||||||
|
13
yaml.go
13
yaml.go
@ -16,10 +16,21 @@ 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)
|
||||||
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 {
|
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 ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user