-
Notifications
You must be signed in to change notification settings - Fork 0
/
observed_changes.go
62 lines (54 loc) · 1.23 KB
/
observed_changes.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
package bolted
import "github.com/draganm/bolted/dbpath"
type ChangeType int
const (
ChangeTypeNoChange ChangeType = iota
ChangeTypeMapCreated
ChangeTypeValueSet
ChangeTypeDeleted
)
// type ObservedChanges map[string]ChangeType
type ObservedChange struct {
Path dbpath.Path
Type ChangeType
}
type ObservedChanges []ObservedChange
func (o ObservedChanges) TypeOfChange(path dbpath.Path) ChangeType {
for _, oc := range o {
switch oc.Type {
case ChangeTypeDeleted:
if oc.Path.ToMatcher().Matches(path) {
return ChangeTypeDeleted
}
case ChangeTypeMapCreated, ChangeTypeValueSet:
if path.Equal(oc.Path) {
return oc.Type
}
}
}
return ChangeTypeNoChange
}
func (o ObservedChanges) Update(path dbpath.Path, t ChangeType) ObservedChanges {
switch t {
case ChangeTypeValueSet, ChangeTypeMapCreated:
for i, oc := range o {
if oc.Path.Equal(path) {
o[i].Type = t
return o
}
}
return append(o, ObservedChange{Path: path, Type: t})
case ChangeTypeDeleted:
m := path.ToMatcher().AppendAnySubpathMatcher()
oc := ObservedChanges{}
for _, c := range o {
if !m.Matches(c.Path) {
oc = append(oc, c)
}
}
oc = append(oc, ObservedChange{Path: path, Type: t})
return oc
default:
return o
}
}