-
Notifications
You must be signed in to change notification settings - Fork 48
/
change.go
93 lines (79 loc) · 1.64 KB
/
change.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
87
88
89
90
91
92
93
package agollo
import (
"bytes"
"fmt"
)
// ChangeType for a key
type ChangeType int
const (
// ADD a new value
ADD ChangeType = iota
// MODIFY a old value
MODIFY
// DELETE ...
DELETE
)
func (c ChangeType) String() string {
switch c {
case ADD:
return "ADD"
case MODIFY:
return "MODIFY"
case DELETE:
return "DELETE"
}
return "UNKNOW"
}
// ChangeEvent change event
type ChangeEvent struct {
Namespace string
Changes map[string]*Change
}
func (e *ChangeEvent) String() string {
var buf bytes.Buffer
buf.WriteString("\n[CHANGE]====\n")
_, _ = fmt.Fprintf(&buf, " [namespace]%s\n", e.Namespace)
for _, change := range e.Changes {
_, _ = fmt.Fprintf(&buf, "%s\n", change)
_, _ = fmt.Fprintf(&buf, "--------\n")
}
buf.WriteString("============\n")
return buf.String()
}
// Change represent a single key change
type Change struct {
Key string
OldValue string
NewValue string
ChangeType ChangeType
}
func (c *Change) String() string {
var buf bytes.Buffer
_, _ = fmt.Fprintf(&buf, " [KEY]%s\n", c.Key)
_, _ = fmt.Fprintf(&buf, " [%s]\n", c.ChangeType)
_, _ = fmt.Fprintf(&buf, " [OLD]%s\n", c.OldValue)
_, _ = fmt.Fprintf(&buf, " [NEW]%s\n", c.NewValue)
return buf.String()
}
func makeDeleteChange(key, value string) *Change {
return &Change{
Key: key,
ChangeType: DELETE,
OldValue: value,
}
}
func makeModifyChange(key, oldValue, newValue string) *Change {
return &Change{
Key: key,
ChangeType: MODIFY,
OldValue: oldValue,
NewValue: newValue,
}
}
func makeAddChange(key, value string) *Change {
return &Change{
Key: key,
ChangeType: ADD,
NewValue: value,
}
}