8d7803d99d
2.根节点不能解释成字符串的bug Fixed
62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package grconfig
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"gopkg.in/yaml.v2"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
type ConfigerChannelYAML struct {
|
|
tmp interface{}
|
|
filename string
|
|
adapter string
|
|
}
|
|
|
|
func (conf *ConfigerChannelYAML) String(item string) string {
|
|
chunks := strings.SplitN(item, ".", 2)
|
|
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 ""
|
|
}
|
|
|
|
func (conf *ConfigerChannelYAML) Item(item string, t interface{}) (err error) {
|
|
chunks := strings.SplitN(item, ".", 2)
|
|
|
|
if len(chunks) == 1 {
|
|
chunks = append(chunks, "")
|
|
}
|
|
|
|
target := recursiveParse(chunks[1], conf.tmp)
|
|
if target == nil {
|
|
err = errors.New("item doesn't exists")
|
|
return
|
|
}
|
|
out, _ := yaml.Marshal(target)
|
|
err = yaml.Unmarshal(out, t)
|
|
return
|
|
}
|
|
|
|
func parseYAML(buf bytes.Buffer) interface{} {
|
|
var v interface{}
|
|
if err := yaml.Unmarshal(buf.Bytes(), &v); err != nil {
|
|
log.Println(err)
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|