map.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package paladin
  2. import (
  3. "strings"
  4. "sync/atomic"
  5. )
  6. // keyNamed key naming to lower case.
  7. func keyNamed(key string) string {
  8. return strings.ToLower(key)
  9. }
  10. // Map is config map, key(filename) -> value(file).
  11. type Map struct {
  12. values atomic.Value
  13. }
  14. // Store sets the value of the Value to values map.
  15. func (m *Map) Store(values map[string]*Value) {
  16. dst := make(map[string]*Value, len(values))
  17. for k, v := range values {
  18. dst[keyNamed(k)] = v
  19. }
  20. m.values.Store(dst)
  21. }
  22. // Load returns the value set by the most recent Store.
  23. func (m *Map) Load() map[string]*Value {
  24. return m.values.Load().(map[string]*Value)
  25. }
  26. // Exist check if values map exist a key.
  27. func (m *Map) Exist(key string) bool {
  28. _, ok := m.Load()[keyNamed(key)]
  29. return ok
  30. }
  31. // Get return get value by key.
  32. func (m *Map) Get(key string) *Value {
  33. v, ok := m.Load()[keyNamed(key)]
  34. if ok {
  35. return v
  36. }
  37. return &Value{}
  38. }
  39. // Keys return map keys.
  40. func (m *Map) Keys() []string {
  41. values := m.Load()
  42. keys := make([]string, 0, len(values))
  43. for key := range values {
  44. keys = append(keys, key)
  45. }
  46. return keys
  47. }