common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package properties
  2. import (
  3. "bufio"
  4. "bytes"
  5. "io"
  6. "strings"
  7. "sync"
  8. "git.shuncheng.lu/bigthing/gocommon/pkg/internal/util"
  9. )
  10. type Properties interface {
  11. // changeMap 切片两个值,第一个是老得值,第二个是新的值,并发安全
  12. DiffProperties(pro Properties) (deleteMap, addMap map[string]string, changeMap map[string][]string)
  13. SetString(key string, value string)
  14. GetString(key string, defaultValue ...string) (value string)
  15. GetBool(key string, defaultValue ...bool) (value bool)
  16. GetInt64(key string, defaultValue ...int64) (value int64)
  17. GetUint64(key string, defaultValue ...uint64) (value uint64)
  18. GetFloat64(key string, defaultValue ...float64) (value float64)
  19. GetAll() map[string]string
  20. }
  21. type properties struct {
  22. config map[string]string
  23. sync.RWMutex
  24. }
  25. func NewProperties() Properties {
  26. return &properties{
  27. config: map[string]string{},
  28. }
  29. }
  30. func ReadFromBytes(content []byte) (Properties, error) {
  31. config, err := readStr(content)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &properties{
  36. config: config,
  37. }, nil
  38. }
  39. func ReadFromString(content string) (Properties, error) {
  40. config, err := readStr([]byte(content))
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &properties{
  45. config: config,
  46. }, nil
  47. }
  48. func ReadFromReader(reader io.Reader) (Properties, error) {
  49. config, err := readReader(reader)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &properties{
  54. config: config,
  55. }, nil
  56. }
  57. func (p *properties) GetString(key string, defaultValue ...string) string {
  58. p.RLock()
  59. defer p.RUnlock()
  60. value, isExist := p.config[key]
  61. if !isExist || value == "" {
  62. if defaultValue == nil || len(defaultValue) == 0 {
  63. return ""
  64. }
  65. return defaultValue[0]
  66. }
  67. return value
  68. }
  69. func (p *properties) SetString(key string, value string) {
  70. p.Lock()
  71. defer p.Unlock()
  72. p.config[key] = value
  73. }
  74. func (p *properties) GetInt64(key string, defaultValue ...int64) (value int64) {
  75. p.RLock()
  76. defer p.RUnlock()
  77. return util.String2Int64(p.config[key], defaultValue...)
  78. }
  79. func (p *properties) GetUint64(key string, defaultValue ...uint64) (value uint64) {
  80. p.RLock()
  81. defer p.RUnlock()
  82. return util.String2Uint64(p.config[key], defaultValue...)
  83. }
  84. func (p *properties) GetFloat64(key string, defaultValue ...float64) (value float64) {
  85. p.RLock()
  86. defer p.RUnlock()
  87. return util.String2Float64(p.config[key], defaultValue...)
  88. }
  89. func (p *properties) GetBool(key string, defaultValue ...bool) (value bool) {
  90. p.RLock()
  91. defer p.RUnlock()
  92. return util.String2Bool(p.config[key], defaultValue...)
  93. }
  94. func (p *properties) GetAll() map[string]string {
  95. p.RLock()
  96. defer p.RUnlock()
  97. result := make(map[string]string, len(p.config))
  98. for key, value := range p.config {
  99. result[key] = value
  100. }
  101. return result
  102. }
  103. func readStr(str []byte) (map[string]string, error) {
  104. if str == nil || len(str) == 0 {
  105. return map[string]string{}, nil
  106. }
  107. buffer := bytes.Buffer{}
  108. _, err := buffer.Write(str)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return readReader(&buffer)
  113. }
  114. func readReader(reader io.Reader) (map[string]string, error) {
  115. if reader == nil {
  116. return map[string]string{}, nil
  117. }
  118. r := bufio.NewReader(reader)
  119. config := make(map[string]string, 0)
  120. for {
  121. b, _, err := r.ReadLine()
  122. if err != nil {
  123. if err == io.EOF {
  124. break
  125. }
  126. return nil, err
  127. }
  128. s := strings.TrimSpace(string(b))
  129. if s == "" {
  130. continue
  131. }
  132. if s[0] == '#' { // 过滤注释
  133. continue
  134. }
  135. // 获取key=value
  136. index := strings.Index(s, "=")
  137. if index < 0 {
  138. continue
  139. }
  140. key := strings.TrimSpace(s[:index])
  141. if len(key) == 0 {
  142. continue
  143. }
  144. value := strings.TrimSpace(s[index+1:])
  145. if len(value) == 0 {
  146. continue
  147. }
  148. config[key] = value
  149. }
  150. return config, nil
  151. }
  152. func (p *properties) DiffProperties(pro Properties) (deleteMap, addMap map[string]string, changeMap map[string][]string) {
  153. oldConfig := p.GetAll()
  154. newConfig := pro.GetAll()
  155. return util.DiffMap(oldConfig, newConfig)
  156. }