default_storage.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package config
  2. import (
  3. "encoding/json"
  4. "sync"
  5. )
  6. // defaultStorage implements config.Storage, providing in-memory config.
  7. // Indexed by section, then key.
  8. type defaultStorage struct {
  9. mu sync.RWMutex
  10. sections map[string]map[string]string
  11. }
  12. func newDefaultStorage() *defaultStorage {
  13. return &defaultStorage{
  14. sections: map[string]map[string]string{},
  15. }
  16. }
  17. // GetSectionList returns a slice of strings with names for all the sections.
  18. func (s *defaultStorage) GetSectionList() []string {
  19. s.mu.RLock()
  20. defer s.mu.RUnlock()
  21. sections := make([]string, 0, len(s.sections))
  22. for section := range s.sections {
  23. sections = append(sections, section)
  24. }
  25. return sections
  26. }
  27. // HasSection returns true if section exists in the config.
  28. func (s *defaultStorage) HasSection(section string) bool {
  29. s.mu.RLock()
  30. defer s.mu.RUnlock()
  31. _, hasSection := s.sections[section]
  32. return hasSection
  33. }
  34. // DeleteSection deletes the specified section.
  35. func (s *defaultStorage) DeleteSection(section string) {
  36. s.mu.Lock()
  37. defer s.mu.Unlock()
  38. delete(s.sections, section)
  39. }
  40. // GetKeyList returns the keys in this section.
  41. func (s *defaultStorage) GetKeyList(section string) []string {
  42. s.mu.RLock()
  43. defer s.mu.RUnlock()
  44. theSection := s.sections[section]
  45. keys := make([]string, 0, len(theSection))
  46. for key := range theSection {
  47. keys = append(keys, key)
  48. }
  49. return keys
  50. }
  51. // GetValue returns the key in section with a found flag.
  52. func (s *defaultStorage) GetValue(section string, key string) (value string, found bool) {
  53. s.mu.RLock()
  54. defer s.mu.RUnlock()
  55. theSection, hasSection := s.sections[section]
  56. if !hasSection {
  57. return "", false
  58. }
  59. value, hasValue := theSection[key]
  60. return value, hasValue
  61. }
  62. func (s *defaultStorage) SetValue(section string, key string, value string) {
  63. s.mu.Lock()
  64. defer s.mu.Unlock()
  65. theSection, hasSection := s.sections[section]
  66. if !hasSection {
  67. theSection = map[string]string{}
  68. s.sections[section] = theSection
  69. }
  70. theSection[key] = value
  71. }
  72. func (s *defaultStorage) DeleteKey(section string, key string) bool {
  73. s.mu.Lock()
  74. defer s.mu.Unlock()
  75. theSection, hasSection := s.sections[section]
  76. if !hasSection {
  77. return false
  78. }
  79. _, hasKey := theSection[key]
  80. if !hasKey {
  81. return false
  82. }
  83. delete(s.sections[section], key)
  84. return true
  85. }
  86. func (s *defaultStorage) Load() error {
  87. return nil
  88. }
  89. func (s *defaultStorage) Save() error {
  90. return nil
  91. }
  92. // Serialize the config into a string
  93. func (s *defaultStorage) Serialize() (string, error) {
  94. s.mu.RLock()
  95. defer s.mu.RUnlock()
  96. j, err := json.Marshal(s.sections)
  97. return string(j), err
  98. }
  99. // Check the interface is satisfied
  100. var _ Storage = newDefaultStorage()