-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChannelStatusSlice.go
92 lines (82 loc) · 1.91 KB
/
ChannelStatusSlice.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
package model
type ChannelStatusSlice struct {
Slice []ChannelStatus
Filters channelStatusFilters
currentFilters []ChannelStatusFilter
}
//adds a filter to be used by iterators
func (Ω *ChannelStatusSlice) Filter(filter ChannelStatusFilter) *ChannelStatusSlice {
Ω.currentFilters = append(Ω.currentFilters, filter)
return Ω
}
//clears all filters used in iterators
func (Ω *ChannelStatusSlice) ClearFilters() {
Ω.currentFilters = []ChannelStatusFilter{}
}
func shouldFilterOut(v ChannelStatus, filters []ChannelStatusFilter) bool {
for _, f := range filters {
if !f(v) {
return true
}
}
return false
}
func (Ω *ChannelStatusSlice) Iterator() (chan ChannelStatus, chan bool) {
o := make(chan ChannelStatus, 1)
ctrl := make(chan bool, 1)
go func(filters []ChannelStatusFilter) {
for _, v := range Ω.Slice {
select {
case done := <-ctrl:
if done {
close(o)
close(ctrl)
}
default:
if !shouldFilterOut(v, filters) {
o <- v
}
}
}
close(o)
}(Ω.currentFilters)
return o, ctrl
}
func (Ω ChannelStatusSlice) ChannelIdIterator() (chan ChannelId, chan bool) {
o := make(chan ChannelId, 1)
ctrl := make(chan bool, 1)
go func(filters []ChannelStatusFilter) {
for _, v := range Ω.Slice {
select {
case done := <-ctrl:
if done {
close(o)
close(ctrl)
}
default:
if !shouldFilterOut(v, filters) {
o <- ChannelId(v)
}
}
}
close(o)
}(Ω.currentFilters)
return o, ctrl
}
func (Ω *ChannelStatusSlice) ToMap() map[string]ChannelStatus {
return Ω.ToMapById()
}
func (Ω *ChannelStatusSlice) ToMapById() map[string]ChannelStatus {
m := make(map[string]ChannelStatus)
for _, cc := range Ω.Slice {
m[cc.ChannelId()] = cc
}
return m
}
func (Ω *ChannelStatusSlice) ToMapByName() map[string]ChannelStatus {
m := make(map[string]ChannelStatus)
for _, cc := range Ω.Slice {
m[cc.Nameø] = cc
}
return m
}