简单地读取不同环境的配置, 目前仅限于YAML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
814 B

  1. package grconfig
  2. import (
  3. "bytes"
  4. "errors"
  5. "gopkg.in/yaml.v2"
  6. "strings"
  7. )
  8. type ConfigerChannelYAML struct {
  9. tmp interface{}
  10. filename string
  11. adapter string
  12. }
  13. func (conf *ConfigerChannelYAML) String(item string) string {
  14. chunks := strings.SplitN(item, ".", 2)
  15. target := recursiveParse(chunks[1], conf.tmp)
  16. if str, ok := target.(string); ok {
  17. return str
  18. }
  19. return ""
  20. }
  21. func (conf *ConfigerChannelYAML) Item(item string, t interface{}) (err error) {
  22. chunks := strings.SplitN(item, ".", 2)
  23. target := recursiveParse(chunks[1], conf.tmp)
  24. if target == nil {
  25. err = errors.New("item doesn't exists")
  26. return
  27. }
  28. out, _ := yaml.Marshal(target)
  29. err = yaml.Unmarshal(out, t)
  30. return
  31. }
  32. func parseYAML(buf bytes.Buffer) interface{} {
  33. var v interface{}
  34. yaml.Unmarshal(buf.Bytes(), &v)
  35. return v
  36. }