Browse Source

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

2.根节点不能解释成字符串的bug Fixed
tags/v1.1.1
liangzy 3 years ago
parent
commit
8d7803d99d
3 changed files with 16 additions and 3 deletions
  1. +0
    -2
      grconfig.go
  2. +4
    -0
      grconfig_test.go
  3. +12
    -1
      yaml.go

+ 0
- 2
grconfig.go 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]
}


+ 4
- 0
grconfig_test.go 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"))
}

+ 12
- 1
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 ""
}



Loading…
Cancel
Save