Compare commits

...

1 Commits

Author SHA1 Message Date
liangzy
8d7803d99d 1.二级节点直接解释字符串优化
2.根节点不能解释成字符串的bug Fixed
2020-08-10 09:59:29 +08:00
3 changed files with 16 additions and 3 deletions

View File

@ -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]
} }

View File

@ -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
View File

@ -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 ""
} }