-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutations.go
86 lines (75 loc) · 1.94 KB
/
mutations.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
package attorney
import (
"log/slog"
"github.com/freehandle/breeze/crypto"
)
type Mutations struct {
GrantPower map[crypto.Hash]struct{}
RevokePower map[crypto.Hash]struct{}
NewMembers map[crypto.Hash]struct{}
NewCaption map[crypto.Hash]struct{}
}
func NewMutations() *Mutations {
return &Mutations{
GrantPower: make(map[crypto.Hash]struct{}),
RevokePower: make(map[crypto.Hash]struct{}),
NewMembers: make(map[crypto.Hash]struct{}),
NewCaption: make(map[crypto.Hash]struct{}),
}
}
func (m *Mutations) HasGrantPower(hash crypto.Hash) bool {
if m.GrantPower == nil {
slog.Error("mutations.GrantPower is nil")
return false
}
_, ok := m.GrantPower[hash]
return ok
}
func (m *Mutations) HasRevokePower(hash crypto.Hash) bool {
if m.RevokePower == nil {
slog.Error("mutations.RevokePower is nil")
return false
}
_, ok := m.RevokePower[hash]
return ok
}
func (m *Mutations) HasMember(hash crypto.Hash) bool {
if m.NewMembers == nil {
slog.Error("mutations.NewMembers is nil")
return false
}
_, ok := m.NewMembers[hash]
return ok
}
func (m *Mutations) HasCaption(hash crypto.Hash) bool {
if m.NewCaption == nil {
slog.Error("mutations.NewCaption is nil")
return false
}
_, ok := m.NewCaption[hash]
return ok
}
func (m *Mutations) Merge(others ...*Mutations) *Mutations {
grouped := &Mutations{
GrantPower: make(map[crypto.Hash]struct{}),
RevokePower: make(map[crypto.Hash]struct{}),
NewMembers: make(map[crypto.Hash]struct{}),
NewCaption: make(map[crypto.Hash]struct{}),
}
for _, mutations := range others {
for hash := range mutations.GrantPower {
grouped.GrantPower[hash] = struct{}{}
}
for hash := range mutations.RevokePower {
grouped.RevokePower[hash] = struct{}{}
delete(grouped.GrantPower, hash)
}
for hash := range mutations.NewMembers {
grouped.NewMembers[hash] = struct{}{}
}
for hash := range mutations.NewCaption {
grouped.NewCaption[hash] = struct{}{}
}
}
return grouped
}