1.二级节点直接解释字符串优化

2.根节点不能解释成字符串的bug Fixed
This commit is contained in:
liangzy 2020-08-10 09:59:29 +08:00
parent 93682ad23c
commit 8d7803d99d
3 changed files with 16 additions and 3 deletions

View File

@ -85,11 +85,9 @@ 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]
}

View File

@ -36,4 +36,8 @@ func TestNew(t *testing.T) {
var s string
gr.Item("db.default.host", &s)
fmt.Println("item形式解释符串:", s)
// 根解释字符串
fmt.Println("根解释字符串", gr.String("db"))
fmt.Println("二级解释字符串", gr.String("db.default"))
}

13
yaml.go
View File

@ -16,10 +16,21 @@ type ConfigerChannelYAML struct {
func (conf *ConfigerChannelYAML) String(item string) string {
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 {
return str
} else {
if out, err := yaml.Marshal(target); err == nil {
return bytes.NewBuffer(out).String()
}
}
return ""
}