-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset.go
61 lines (53 loc) · 1 KB
/
set.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
package gmtr
import (
"encoding/json"
"strings"
)
type StringSet map[string]struct{}
func (ms StringSet) MarshalJSON() ([]byte, error) {
val := make([]string, 0, len(ms))
for k := range ms {
val = append(val, k)
}
return json.Marshal(val)
}
func (ms *StringSet) UnmarshalJSON(b []byte) error {
val := []string{}
err := json.Unmarshal(b, &val)
if err != nil {
return err
}
*ms = make(StringSet)
for _, v := range val {
ms.Add(v)
}
return nil
}
func (ms StringSet) Values() []string {
var result []string
for key, _ := range ms {
result = append(result, key)
}
return result
}
func (ms StringSet) String() string {
var values []string
for metric, _ := range ms {
values = append(values, string(metric))
}
return strings.Join(values, ",")
}
func (ms StringSet) Has(mk string) bool {
_, exists := ms[mk]
return exists
}
func (ms StringSet) Add(mk string) {
ms[mk] = struct{}{}
}
func (ms StringSet) Copy() StringSet {
d := StringSet{}
for key, _ := range ms {
d.Add(key)
}
return d
}