forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetstore.go
61 lines (51 loc) · 1.05 KB
/
setstore.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 automod
import (
"context"
"encoding/json"
"io"
"os"
)
type SetStore interface {
InSet(ctx context.Context, name, val string) (bool, error)
}
// TODO: this implementation isn't race-safe (yet)!
type MemSetStore struct {
Sets map[string]map[string]bool
}
func NewMemSetStore() MemSetStore {
return MemSetStore{
Sets: make(map[string]map[string]bool),
}
}
func (s MemSetStore) InSet(ctx context.Context, name, val string) (bool, error) {
set, ok := s.Sets[name]
if !ok {
// NOTE: currently returns false when entire set isn't found
return false, nil
}
_, ok = set[val]
return ok, nil
}
func (s *MemSetStore) LoadFromFileJSON(p string) error {
f, err := os.Open(p)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
raw, err := io.ReadAll(f)
if err != nil {
return err
}
var rules map[string][]string
if err := json.Unmarshal(raw, &rules); err != nil {
return err
}
for name, l := range rules {
m := make(map[string]bool, len(l))
for _, val := range l {
m[val] = true
}
s.Sets[name] = m
}
return nil
}