/
collector.go
210 lines (178 loc) · 3.93 KB
/
collector.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package relay
import "sync"
//StringEachfunc defines the type of the Mappable.Each rule
type StringEachfunc func(interface{}, string, func())
//Collector defines a typ of map string
type Collector map[string]interface{}
//NewCollector returns a new collector instance
func NewCollector() Collector {
return make(Collector)
}
//ToMap makes a new clone of this map[string]interface{}
func (c Collector) ToMap() map[string]interface{} {
return c.Clone()
}
//Clone makes a new clone of this collector
func (c Collector) Clone() Collector {
col := make(Collector)
col.Copy(c)
return col
}
//Remove deletes a key:value pair
func (c Collector) Remove(k string) {
if c.Has(k) {
delete(c, k)
}
}
//Keys return the keys of the Collector
func (c Collector) Keys() []string {
var keys []string
c.Each(func(_ interface{}, k string, _ func()) {
keys = append(keys, k)
})
return keys
}
//Get returns the value with the key
func (c Collector) Get(k string) interface{} {
return c[k]
}
//Has returns if a key exists
func (c Collector) Has(k string) bool {
_, ok := c[k]
return ok
}
//HasMatch checks if key and value exists and are matching
func (c Collector) HasMatch(k string, v interface{}) bool {
if c.Has(k) {
return c.Get(k) == v
}
return false
}
//Set puts a specific key:value into the collector
func (c Collector) Set(k string, v interface{}) {
c[k] = v
}
//Copy copies the map into the collector
func (c Collector) Copy(m map[string]interface{}) {
for v, k := range m {
c.Set(v, k)
}
}
//Each iterates through all items in the collector
func (c Collector) Each(fx StringEachfunc) {
var state bool
for k, v := range c {
if state {
break
}
fx(v, k, func() {
state = true
})
}
}
//Clear clears the collector
func (c Collector) Clear() {
for k := range c {
delete(c, k)
}
}
// SyncCollector provides a mutex controlled map
type SyncCollector struct {
c Collector
rw sync.RWMutex
}
//NewSyncCollector returns a new collector instance
func NewSyncCollector() *SyncCollector {
so := SyncCollector{c: make(Collector)}
return &so
}
//Clone makes a new clone of this collector
func (c *SyncCollector) Clone() *SyncCollector {
var co Collector
c.rw.RLock()
co = c.c.Clone()
c.rw.RUnlock()
so := SyncCollector{c: co}
return &so
}
//ToMap makes a new clone of this map[string]interface{}
func (c *SyncCollector) ToMap() map[string]interface{} {
var co Collector
c.rw.RLock()
co = c.c.Clone()
c.rw.RUnlock()
return co
}
//Remove deletes a key:value pair
func (c *SyncCollector) Remove(k string) {
c.rw.Lock()
c.c.Remove(k)
c.rw.Unlock()
}
//Set puts a specific key:value into the collector
func (c *SyncCollector) Set(k string, v interface{}) {
c.rw.Lock()
c.c.Set(k, v)
c.rw.Unlock()
}
//Copy copies the map into the collector
func (c *SyncCollector) Copy(m map[string]interface{}) {
for v, k := range m {
c.Set(v, k)
}
}
//Each iterates through all items in the collector
func (c *SyncCollector) Each(fx StringEachfunc) {
var state bool
c.rw.RLock()
for k, v := range c.c {
if state {
break
}
fx(v, k, func() {
state = true
})
}
c.rw.RUnlock()
}
//Keys return the keys of the Collector
func (c *SyncCollector) Keys() []string {
var keys []string
c.Each(func(_ interface{}, k string, _ func()) {
keys = append(keys, k)
})
return keys
}
//Get returns the value with the key
func (c *SyncCollector) Get(k string) interface{} {
var v interface{}
c.rw.RLock()
v = c.c.Get(k)
c.rw.RUnlock()
return v
}
//Has returns if a key exists
func (c *SyncCollector) Has(k string) bool {
var ok bool
c.rw.RLock()
_, ok = c.c[k]
c.rw.RUnlock()
return ok
}
//HasMatch checks if key and value exists and are matching
func (c *SyncCollector) HasMatch(k string, v interface{}) bool {
// c.rw.RLock()
// defer c.rw.RUnlock()
if c.Has(k) {
return c.Get(k) == v
}
return false
}
//Clear clears the collector
func (c *SyncCollector) Clear() {
for k := range c.c {
c.rw.Lock()
delete(c.c, k)
c.rw.Unlock()
}
}