pre_go19.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build !go1.9
  15. // +build !go1.9
  16. package swag
  17. import (
  18. "sort"
  19. "sync"
  20. )
  21. // indexOfInitialisms is a thread-safe implementation of the sorted index of initialisms.
  22. // Before go1.9, this may be implemented with a mutex on the map.
  23. type indexOfInitialisms struct {
  24. getMutex *sync.Mutex
  25. index map[string]bool
  26. }
  27. func newIndexOfInitialisms() *indexOfInitialisms {
  28. return &indexOfInitialisms{
  29. getMutex: new(sync.Mutex),
  30. index: make(map[string]bool, 50),
  31. }
  32. }
  33. func (m *indexOfInitialisms) load(initial map[string]bool) *indexOfInitialisms {
  34. m.getMutex.Lock()
  35. defer m.getMutex.Unlock()
  36. for k, v := range initial {
  37. m.index[k] = v
  38. }
  39. return m
  40. }
  41. func (m *indexOfInitialisms) isInitialism(key string) bool {
  42. m.getMutex.Lock()
  43. defer m.getMutex.Unlock()
  44. _, ok := m.index[key]
  45. return ok
  46. }
  47. func (m *indexOfInitialisms) add(key string) *indexOfInitialisms {
  48. m.getMutex.Lock()
  49. defer m.getMutex.Unlock()
  50. m.index[key] = true
  51. return m
  52. }
  53. func (m *indexOfInitialisms) sorted() (result []string) {
  54. m.getMutex.Lock()
  55. defer m.getMutex.Unlock()
  56. for k := range m.index {
  57. result = append(result, k)
  58. }
  59. sort.Sort(sort.Reverse(byInitialism(result)))
  60. return
  61. }