util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package xstream
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. )
  11. type Conf map[string]interface{}
  12. var confs = make(map[string]Conf)
  13. func GetConfAsString(file, key string) (string, error) {
  14. val, err := getConfValue(file, key)
  15. if err != nil {
  16. return "", err
  17. }
  18. if v, ok := val.(string); ok {
  19. return v, nil
  20. } else if val == nil {
  21. return "", nil
  22. } else {
  23. return "", fmt.Errorf("The value %s is not type of string for key %s", val, key)
  24. }
  25. }
  26. func GetConfAsInt(file, key string) (int, error) {
  27. val, err := getConfValue(file, key)
  28. if err != nil {
  29. return 0, err
  30. }
  31. if v, ok := val.(float64); ok {
  32. return int(v), nil
  33. } else {
  34. return 0, fmt.Errorf("The value {0} is not type of int for key {1}")
  35. }
  36. }
  37. func GetConfAsFloat(file, key string) (float64, error) {
  38. val, err := getConfValue(file, key)
  39. if err != nil {
  40. return 0, err
  41. }
  42. if v, ok := val.(float64); ok {
  43. return v, nil
  44. } else {
  45. return 0, fmt.Errorf("The value {0} is not type of float for key {1}")
  46. }
  47. }
  48. func GetConfAsBool(file, key string) (bool, error) {
  49. val, err := getConfValue(file, key)
  50. if err != nil {
  51. return false, err
  52. }
  53. if v, ok := val.(bool); ok {
  54. return v, nil
  55. } else {
  56. return false, fmt.Errorf("The value {0} is not type of bool for key {1}")
  57. }
  58. }
  59. func getConfValue(file, key string) (interface{}, error) {
  60. if conf, ok := confs[file]; !ok {
  61. if c, e := initConf(file); e != nil {
  62. return nil, e
  63. } else {
  64. confs[file] = c
  65. return getValue(c, key)
  66. }
  67. } else {
  68. return getValue(conf, key)
  69. }
  70. }
  71. func initConf(file string) (Conf, error) {
  72. conf := make(Conf)
  73. fp, _ := filepath.Abs(file)
  74. if f, err1 := os.Open(fp); err1 == nil {
  75. defer f.Close()
  76. byteValue, _ := ioutil.ReadAll(f)
  77. if err2 := json.Unmarshal([]byte(byteValue), &conf); err2 != nil {
  78. return nil, err2
  79. }
  80. common.Log.Printf("Successfully to load the configuration file %s", fp)
  81. } else {
  82. //Try as absolute path
  83. if f, err1 := os.Open(file); err1 == nil {
  84. byteValue, _ := ioutil.ReadAll(f)
  85. if err2 := json.Unmarshal([]byte(byteValue), &conf); err2 != nil {
  86. return nil, err2
  87. }
  88. common.Log.Printf("Successfully to load the configuration file %s", file)
  89. } else {
  90. return nil, fmt.Errorf("Cannot load configuration file %s", file)
  91. }
  92. }
  93. return conf, nil
  94. }
  95. func getValue(conf Conf, key string) (interface{}, error) {
  96. keys := strings.Split(key, ".")
  97. if len(keys) == 1 {
  98. return conf[key], nil
  99. }
  100. nkey := strings.Join(keys[1:], ".")
  101. ckey := strings.Join(keys[0:1], "")
  102. if c, ok := conf[ckey].(map[string]interface{}); ok {
  103. return getValue(c, nkey)
  104. } else {
  105. return nil, fmt.Errorf("%s does not exsit for key %s.", conf, key)
  106. }
  107. }