pre_go19.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // +build !go1.9
  2. package swag
  3. import (
  4. "sort"
  5. "sync"
  6. )
  7. // indexOfInitialisms is a thread-safe implementation of the sorted index of initialisms.
  8. // Before go1.9, this may be implemented with a mutex on the map.
  9. type indexOfInitialisms struct {
  10. getMutex *sync.Mutex
  11. index map[string]bool
  12. }
  13. func newIndexOfInitialisms() *indexOfInitialisms {
  14. return &indexOfInitialisms{
  15. getMutex: new(sync.Mutex),
  16. index: make(map[string]bool, 50),
  17. }
  18. }
  19. func (m *indexOfInitialisms) load(initial map[string]bool) *indexOfInitialisms {
  20. m.getMutex.Lock()
  21. defer m.getMutex.Unlock()
  22. for k, v := range initial {
  23. m.index[k] = v
  24. }
  25. return m
  26. }
  27. func (m *indexOfInitialisms) isInitialism(key string) bool {
  28. m.getMutex.Lock()
  29. defer m.getMutex.Unlock()
  30. _, ok := m.index[key]
  31. return ok
  32. }
  33. func (m *indexOfInitialisms) add(key string) *indexOfInitialisms {
  34. m.getMutex.Lock()
  35. defer m.getMutex.Unlock()
  36. m.index[key] = true
  37. return m
  38. }
  39. func (m *indexOfInitialisms) sorted() (result []string) {
  40. m.getMutex.Lock()
  41. defer m.getMutex.Unlock()
  42. for k := range m.index {
  43. result = append(result, k)
  44. }
  45. sort.Sort(sort.Reverse(byLength(result)))
  46. return
  47. }