forked from omriharel/deej
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider_map.go
92 lines (70 loc) · 1.84 KB
/
slider_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package deej
import (
"fmt"
"strconv"
"sync"
"github.com/thoas/go-funk"
)
type sliderMap struct {
m map[int][]string
lock sync.Locker
}
func newSliderMap() *sliderMap {
return &sliderMap{
m: make(map[int][]string),
lock: &sync.Mutex{},
}
}
func sliderMapFromConfigs(userMapping map[string][]string, internalMapping map[string][]string) *sliderMap {
resultMap := newSliderMap()
// copy targets from user config, ignoring empty values
for sliderIdxString, targets := range userMapping {
sliderIdx, _ := strconv.Atoi(sliderIdxString)
resultMap.set(sliderIdx, funk.FilterString(targets, func(s string) bool {
return s != ""
}))
}
// add targets from internal configs, ignoring duplicate or empty values
for sliderIdxString, targets := range internalMapping {
sliderIdx, _ := strconv.Atoi(sliderIdxString)
existingTargets, ok := resultMap.get(sliderIdx)
if !ok {
existingTargets = []string{}
}
filteredTargets := funk.FilterString(targets, func(s string) bool {
return (!funk.ContainsString(existingTargets, s)) && s != ""
})
existingTargets = append(existingTargets, filteredTargets...)
resultMap.set(sliderIdx, existingTargets)
}
return resultMap
}
func (m *sliderMap) iterate(f func(int, []string)) {
m.lock.Lock()
defer m.lock.Unlock()
for key, value := range m.m {
f(key, value)
}
}
func (m *sliderMap) get(key int) ([]string, bool) {
m.lock.Lock()
defer m.lock.Unlock()
value, ok := m.m[key]
return value, ok
}
func (m *sliderMap) set(key int, value []string) {
m.lock.Lock()
defer m.lock.Unlock()
m.m[key] = value
}
func (m *sliderMap) String() string {
m.lock.Lock()
defer m.lock.Unlock()
sliderCount := 0
targetCount := 0
for _, value := range m.m {
sliderCount++
targetCount += len(value)
}
return fmt.Sprintf("<%d sliders mapped to %d targets>", sliderCount, targetCount)
}