-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
51 lines (44 loc) · 866 Bytes
/
utils.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
package utils
import (
"gopkg.in/yaml.v3"
)
func Uniq[T comparable](values []T) []T {
var s = make(map[T]struct{}, len(values))
var out = make([]T, 0, len(values))
for _, v := range values {
if _, ok := s[v]; !ok {
s[v] = struct{}{}
out = append(out, v)
}
}
return out
}
type Set[T comparable] map[T]struct{}
func NewSet[T comparable](values ...T) Set[T] {
var s = make(Set[T], len(values))
for _, v := range values {
s[v] = struct{}{}
}
return s
}
func (s Set[T]) Has(values ...T) bool {
for _, v := range values {
if _, ok := s[v]; !ok {
return false
}
}
return true
}
func (s Set[T]) Add(values ...T) {
for _, v := range values {
s[v] = struct{}{}
}
}
func (s *Set[T]) UnmarshalYAML(value *yaml.Node) error {
var data []T
if err := value.Decode(&data); err != nil {
return err
}
*s = NewSet(data...)
return nil
}