-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobject.go
192 lines (156 loc) · 3.6 KB
/
object.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"errors"
"reflect"
"gopkg.in/mgo.v2/bson"
)
var (
ErrUnknownRevision = errors.New("unknown revision")
)
type Change struct {
Before interface{} `json:"before"`
After interface{} `json:"after"`
}
type Revision struct {
Version int `json:"version"`
Time int64 `json:"time"`
Additions map[string]interface{} `bson:",omitempty" json:"additions,omitempty"`
Removals map[string]interface{} `bson:",omitempty" json:"removals,omitempty"`
Changes map[string]Change `bson:",omitempty" json:"changes,omitempty"`
}
type Object struct {
ID bson.ObjectId `bson:"_id" json:"_id,omitempty"`
Key string `json:"key"`
Value map[string]interface{} `json:"value"`
Version int `json:"version"`
Time int64 `json:"time"`
History []*Revision `json:"history,omitempty" yaml:",omitempty"`
}
func applyRevision(o *Object, r *Revision) {
var (
key string
val interface{}
chg Change
)
o.Version = r.Version
o.Time = r.Time
if r.Additions != nil {
for key, val = range r.Additions {
o.Value[key] = val
}
}
if r.Removals != nil {
for key, val = range r.Removals {
delete(o.Value, key)
}
}
if r.Changes != nil {
for key, chg = range r.Changes {
o.Value[key] = chg.After
}
}
}
// AtVersion reverts the objects to the specified version.
func (o *Object) AtVersion(v int) *Object {
if v == 0 {
return nil
}
n := Object{
ID: o.ID,
Key: o.Key,
Value: make(map[string]interface{}),
}
for i, rev := range o.History {
if rev.Version > v {
n.History = o.History[:i]
break
}
applyRevision(&n, rev)
}
return &n
}
// AtTime reverts the object to the state as of the specified time.
func (o *Object) AtTime(t int64) *Object {
n := Object{
ID: o.ID,
Key: o.Key,
Value: make(map[string]interface{}),
}
for i, rev := range o.History {
if rev.Time > t {
n.History = o.History[:i]
break
}
applyRevision(&n, rev)
}
// The time is earlier than the first revision of this object.
if n.Version == 0 {
return nil
}
return &n
}
// Diff returns the set of changes representing the different between two
// documents. Compares the before (`b`) and after (`a`) state of the document.
// Currently this only diffs the top-level keys and does not recurse into
// sub-documents.
func Diff(b, a map[string]interface{}) *Revision {
if (a == nil || len(a) == 0) && (b == nil || len(b) == 0) {
return nil
}
// No existing document to compare, a is an addition.
if b == nil || len(b) == 0 {
return &Revision{
Additions: a,
}
}
// Next state is nil, b is a removal.
if a == nil || len(a) == 0 {
return &Revision{
Removals: b,
}
}
var (
ok bool
ak, bk string
av, bv interface{}
adds = make(map[string]interface{})
removes = make(map[string]interface{})
changes = make(map[string]Change)
)
// Additions and changes.
for ak, av = range a {
// Key does not exist in b, mark as addition.
if bv, ok = b[ak]; !ok {
adds[ak] = av
// Compare a and b values.
} else if !reflect.DeepEqual(av, bv) {
changes[ak] = Change{
Before: bv,
After: av,
}
}
}
// Removals.
for bk, bv = range b {
// Keys in b that no longer exist in a.
if _, ok = a[bk]; !ok {
removes[bk] = bv
}
}
// No difference.
if len(adds) == 0 && len(removes) == 0 && len(changes) == 0 {
return nil
}
// Build the diff.
r := Revision{}
if len(adds) > 0 {
r.Additions = adds
}
if len(removes) > 0 {
r.Removals = removes
}
if len(changes) > 0 {
r.Changes = changes
}
return &r
}