-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
listener_map.go
73 lines (64 loc) · 1.67 KB
/
listener_map.go
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
62
63
64
65
66
67
68
69
70
71
72
73
//go:generate mapgen -name "listener" -zero "nil" -go-type "Listener" -pkg "" -a "func(e) {}" -b "nil" -c "nil" -bb "nil" -destination "events"
// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
package events
import (
"sort"
"sync"
)
// listenerMap wraps sync.Map and uses the following types:
// key: string
// value: Listener
type listenerMap struct {
data sync.Map
}
// Delete the key from the map
func (m *listenerMap) Delete(key string) {
m.data.Delete(key)
}
// Load the key from the map.
// Returns Listener or bool.
// A false return indicates either the key was not found
// or the value is not of type Listener
func (m *listenerMap) Load(key string) (Listener, bool) {
i, ok := m.data.Load(key)
if !ok {
return nil, false
}
s, ok := i.(Listener)
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *listenerMap) LoadOrStore(key string, value Listener) (Listener, bool) {
i, _ := m.data.LoadOrStore(key, value)
s, ok := i.(Listener)
return s, ok
}
// Range over the Listener values in the map
func (m *listenerMap) Range(f func(key string, value Listener) bool) {
m.data.Range(func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
return false
}
value, ok := v.(Listener)
if !ok {
return false
}
return f(key, value)
})
}
// Store a Listener in the map
func (m *listenerMap) Store(key string, value Listener) {
m.data.Store(key, value)
}
// Keys returns a list of keys in the map
func (m *listenerMap) Keys() []string {
var keys []string
m.Range(func(key string, value Listener) bool {
keys = append(keys, key)
return true
})
sort.Strings(keys)
return keys
}