@@ -21,30 +21,30 @@ const (
21
21
22
22
// Change represents a change to a DAG and contains a reference to the old and
23
23
// new CIDs.
24
- type Change [V any ] struct {
24
+ type Change [T HamtValue [ T ] ] struct {
25
25
Type ChangeType
26
26
Key string
27
- Before * V
28
- After * V
27
+ Before T
28
+ After T
29
29
}
30
30
31
- func (ch Change [V ]) String () string {
31
+ func (ch Change [T ]) String () string {
32
32
b , _ := json .Marshal (ch )
33
33
return string (b )
34
34
}
35
35
36
36
// Diff returns a set of changes that transform node 'prev' into node 'cur'. opts are applied to both prev and cur.
37
- func Diff [V any , T HamtValue [V ]](ctx context.Context , prevBs , curBs cbor.IpldStore , prev , cur cid.Cid , opts ... Option ) ([]* Change [V ], error ) {
37
+ func Diff [T HamtValue [T ]](ctx context.Context , prevBs , curBs cbor.IpldStore , prev , cur cid.Cid , opts ... Option ) ([]* Change [T ], error ) {
38
38
if prev .Equals (cur ) {
39
39
return nil , nil
40
40
}
41
41
42
- prevHamt , err := LoadNode [V , T ](ctx , prevBs , prev , opts ... )
42
+ prevHamt , err := LoadNode [T ](ctx , prevBs , prev , opts ... )
43
43
if err != nil {
44
44
return nil , err
45
45
}
46
46
47
- curHamt , err := LoadNode [V , T ](ctx , curBs , cur , opts ... )
47
+ curHamt , err := LoadNode [T ](ctx , curBs , cur , opts ... )
48
48
if err != nil {
49
49
return nil , err
50
50
}
@@ -55,7 +55,7 @@ func Diff[V any, T HamtValue[V]](ctx context.Context, prevBs, curBs cbor.IpldSto
55
55
return diffNode (ctx , prevHamt , curHamt , 0 )
56
56
}
57
57
58
- func diffNode [V any , T HamtValue [V ]](ctx context.Context , pre , cur * Node [V , T ], depth int ) ([]* Change [V ], error ) {
58
+ func diffNode [T HamtValue [T ]](ctx context.Context , pre , cur * Node [T ], depth int ) ([]* Change [T ], error ) {
59
59
// which Bitfield contains the most bits. We will start a loop from this index, calling Bitfield.Bit(idx)
60
60
// on an out of range index will return zero.
61
61
bp := cur .Bitfield .BitLen ()
@@ -64,7 +64,7 @@ func diffNode[V any, T HamtValue[V]](ctx context.Context, pre, cur *Node[V, T],
64
64
}
65
65
66
66
// the changes between cur and prev
67
- var changes []* Change [V ]
67
+ var changes []* Change [T ]
68
68
69
69
// loop over each bit in the bitfields
70
70
for idx := bp ; idx >= 0 ; idx -- {
@@ -136,11 +136,11 @@ func diffNode[V any, T HamtValue[V]](ctx context.Context, pre, cur *Node[V, T],
136
136
changes = append (changes , rm ... )
137
137
} else {
138
138
for _ , p := range pointer .KVs {
139
- changes = append (changes , & Change [V ]{
139
+ changes = append (changes , & Change [T ]{
140
140
Type : Remove ,
141
141
Key : string (p .Key ),
142
142
Before : p .Value ,
143
- After : nil ,
143
+ After : zero [ T ]() ,
144
144
})
145
145
}
146
146
}
@@ -160,10 +160,10 @@ func diffNode[V any, T HamtValue[V]](ctx context.Context, pre, cur *Node[V, T],
160
160
changes = append (changes , add ... )
161
161
} else {
162
162
for _ , p := range pointer .KVs {
163
- changes = append (changes , & Change [V ]{
163
+ changes = append (changes , & Change [T ]{
164
164
Type : Add ,
165
165
Key : string (p .Key ),
166
- Before : nil ,
166
+ Before : zero [ T ]() ,
167
167
After : p .Value ,
168
168
})
169
169
}
@@ -174,10 +174,10 @@ func diffNode[V any, T HamtValue[V]](ctx context.Context, pre, cur *Node[V, T],
174
174
return changes , nil
175
175
}
176
176
177
- func diffKVs [V any , T HamtValue [V ]](pre , cur []* KV [V , T ], idx int ) []* Change [V ] {
177
+ func diffKVs [T HamtValue [T ]](pre , cur []* KV [T ], idx int ) []* Change [T ] {
178
178
preMap := make (map [string ]T , len (pre ))
179
179
curMap := make (map [string ]T , len (cur ))
180
- var changes []* Change [V ]
180
+ var changes []* Change [T ]
181
181
182
182
for _ , kv := range pre {
183
183
preMap [string (kv .Key )] = kv .Value
@@ -188,27 +188,27 @@ func diffKVs[V any, T HamtValue[V]](pre, cur []*KV[V, T], idx int) []*Change[V]
188
188
// find removed keys: keys in pre and not in cur
189
189
for key , value := range preMap {
190
190
if _ , ok := curMap [key ]; ! ok {
191
- changes = append (changes , & Change [V ]{
191
+ changes = append (changes , & Change [T ]{
192
192
Type : Remove ,
193
193
Key : key ,
194
194
Before : value ,
195
- After : nil ,
195
+ After : zero [ T ]() ,
196
196
})
197
197
}
198
198
}
199
199
// find added keys: keys in cur and not in pre
200
200
// find modified values: keys in cur and pre with different values
201
201
for key , curVal := range curMap {
202
202
if preVal , ok := preMap [key ]; ! ok {
203
- changes = append (changes , & Change [V ]{
203
+ changes = append (changes , & Change [T ]{
204
204
Type : Add ,
205
205
Key : key ,
206
- Before : nil ,
206
+ Before : zero [ T ]() ,
207
207
After : curVal ,
208
208
})
209
209
} else {
210
210
if ! preVal .Equal (curVal ) {
211
- changes = append (changes , & Change [V ]{
211
+ changes = append (changes , & Change [T ]{
212
212
Type : Modify ,
213
213
Key : key ,
214
214
Before : preVal ,
@@ -220,13 +220,13 @@ func diffKVs[V any, T HamtValue[V]](pre, cur []*KV[V, T], idx int) []*Change[V]
220
220
return changes
221
221
}
222
222
223
- func addAll [V any , T HamtValue [V ]](ctx context.Context , node * Node [V , T ], idx int ) ([]* Change [V ], error ) {
224
- var changes []* Change [V ]
223
+ func addAll [T HamtValue [T ]](ctx context.Context , node * Node [T ], idx int ) ([]* Change [T ], error ) {
224
+ var changes []* Change [T ]
225
225
if err := node .ForEach (ctx , func (k string , val T ) error {
226
- changes = append (changes , & Change [V ]{
226
+ changes = append (changes , & Change [T ]{
227
227
Type : Add ,
228
228
Key : k ,
229
- Before : nil ,
229
+ Before : zero [ T ]() ,
230
230
After : val ,
231
231
})
232
232
@@ -237,14 +237,14 @@ func addAll[V any, T HamtValue[V]](ctx context.Context, node *Node[V, T], idx in
237
237
return changes , nil
238
238
}
239
239
240
- func removeAll [V any , T HamtValue [V ]](ctx context.Context , node * Node [V , T ], idx int ) ([]* Change [V ], error ) {
241
- var changes []* Change [V ]
240
+ func removeAll [T HamtValue [T ]](ctx context.Context , node * Node [T ], idx int ) ([]* Change [T ], error ) {
241
+ var changes []* Change [T ]
242
242
if err := node .ForEach (ctx , func (k string , val T ) error {
243
- changes = append (changes , & Change [V ]{
243
+ changes = append (changes , & Change [T ]{
244
244
Type : Remove ,
245
245
Key : k ,
246
246
Before : val ,
247
- After : nil ,
247
+ After : zero [ T ]() ,
248
248
})
249
249
250
250
return nil
0 commit comments