properties.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package conf
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "sync"
  7. "github.com/tal-tech/go-zero/core/iox"
  8. )
  9. // PropertyError represents a configuration error message.
  10. type PropertyError struct {
  11. error
  12. message string
  13. }
  14. // Properties interface provides the means to access configuration.
  15. type Properties interface {
  16. GetString(key string) string
  17. SetString(key, value string)
  18. GetInt(key string) int
  19. SetInt(key string, value int)
  20. ToString() string
  21. }
  22. // Properties config is a key/value pair based configuration structure.
  23. type mapBasedProperties struct {
  24. properties map[string]string
  25. lock sync.RWMutex
  26. }
  27. // Loads the properties into a properties configuration instance.
  28. // Returns an error that indicates if there was a problem loading the configuration.
  29. func LoadProperties(filename string) (Properties, error) {
  30. lines, err := iox.ReadTextLines(filename, iox.WithoutBlank(), iox.OmitWithPrefix("#"))
  31. if err != nil {
  32. return nil, err
  33. }
  34. raw := make(map[string]string)
  35. for i := range lines {
  36. pair := strings.Split(lines[i], "=")
  37. if len(pair) != 2 {
  38. // invalid property format
  39. return nil, &PropertyError{
  40. message: fmt.Sprintf("invalid property format: %s", pair),
  41. }
  42. }
  43. key := strings.TrimSpace(pair[0])
  44. value := strings.TrimSpace(pair[1])
  45. raw[key] = value
  46. }
  47. return &mapBasedProperties{
  48. properties: raw,
  49. }, nil
  50. }
  51. func (config *mapBasedProperties) GetString(key string) string {
  52. config.lock.RLock()
  53. ret := config.properties[key]
  54. config.lock.RUnlock()
  55. return ret
  56. }
  57. func (config *mapBasedProperties) SetString(key, value string) {
  58. config.lock.Lock()
  59. config.properties[key] = value
  60. config.lock.Unlock()
  61. }
  62. func (config *mapBasedProperties) GetInt(key string) int {
  63. config.lock.RLock()
  64. // default 0
  65. value, _ := strconv.Atoi(config.properties[key])
  66. config.lock.RUnlock()
  67. return value
  68. }
  69. func (config *mapBasedProperties) SetInt(key string, value int) {
  70. config.lock.Lock()
  71. config.properties[key] = strconv.Itoa(value)
  72. config.lock.Unlock()
  73. }
  74. // Dumps the configuration internal map into a string.
  75. func (config *mapBasedProperties) ToString() string {
  76. config.lock.RLock()
  77. ret := fmt.Sprintf("%s", config.properties)
  78. config.lock.RUnlock()
  79. return ret
  80. }
  81. // Returns the error message.
  82. func (configError *PropertyError) Error() string {
  83. return configError.message
  84. }
  85. // Builds a new properties configuration structure
  86. func NewProperties() Properties {
  87. return &mapBasedProperties{
  88. properties: make(map[string]string),
  89. }
  90. }