podman
68 строк · 1.7 Кб
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
15//go:build go1.9
16// +build go1.9
17
18package swag
19
20import (
21"sort"
22"sync"
23)
24
25// indexOfInitialisms is a thread-safe implementation of the sorted index of initialisms.
26// Since go1.9, this may be implemented with sync.Map.
27type indexOfInitialisms struct {
28sortMutex *sync.Mutex
29index *sync.Map
30}
31
32func newIndexOfInitialisms() *indexOfInitialisms {
33return &indexOfInitialisms{
34sortMutex: new(sync.Mutex),
35index: new(sync.Map),
36}
37}
38
39func (m *indexOfInitialisms) load(initial map[string]bool) *indexOfInitialisms {
40m.sortMutex.Lock()
41defer m.sortMutex.Unlock()
42for k, v := range initial {
43m.index.Store(k, v)
44}
45return m
46}
47
48func (m *indexOfInitialisms) isInitialism(key string) bool {
49_, ok := m.index.Load(key)
50return ok
51}
52
53func (m *indexOfInitialisms) add(key string) *indexOfInitialisms {
54m.index.Store(key, true)
55return m
56}
57
58func (m *indexOfInitialisms) sorted() (result []string) {
59m.sortMutex.Lock()
60defer m.sortMutex.Unlock()
61m.index.Range(func(key, value interface{}) bool {
62k := key.(string)
63result = append(result, k)
64return true
65})
66sort.Sort(sort.Reverse(byInitialism(result)))
67return
68}
69