-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_config.go
More file actions
61 lines (49 loc) · 1.22 KB
/
api_config.go
File metadata and controls
61 lines (49 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// ==========================
// api_config.go
// ==========================
package main
import "sort"
func NewServiceConfigStore() *ServiceConfigStore {
return &ServiceConfigStore{
profiles: make(map[string]ServiceProfile),
}
}
func (s *ServiceConfigStore) Add(p ServiceProfile) {
s.mu.Lock()
defer s.mu.Unlock()
s.profiles[p.Name] = p
}
func (s *ServiceConfigStore) Get(name string) (ServiceProfile, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
p, ok := s.profiles[name]
return p, ok
}
// List returns a snapshot of all service profiles, sorted by name
func (s *ServiceConfigStore) List() []ServiceProfile {
s.mu.RLock()
defer s.mu.RUnlock()
out := make([]ServiceProfile, 0, len(s.profiles))
for _, p := range s.profiles {
out = append(out, p)
}
sort.Slice(out, func(i, j int) bool {
return out[i].Name < out[j].Name
})
return out
}
// ListRecommended returns only recommended services, sorted by name
func (s *ServiceConfigStore) ListRecommended() []ServiceProfile {
s.mu.RLock()
defer s.mu.RUnlock()
out := make([]ServiceProfile, 0)
for _, p := range s.profiles {
if p.Recommended {
out = append(out, p)
}
}
sort.Slice(out, func(i, j int) bool {
return out[i].Name < out[j].Name
})
return out
}