Skip to content

Commit b1d76a0

Browse files
committed
use a single generic as suggested by @Wondertan
1 parent 410665a commit b1d76a0

File tree

10 files changed

+234
-212
lines changed

10 files changed

+234
-212
lines changed

cbor_gen.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var _ = xerrors.Errorf
1818

1919
var lengthBufNode = []byte{130}
2020

21-
func (t *Node[V, T]) MarshalCBOR(w io.Writer) error {
21+
func (t *Node[T]) MarshalCBOR(w io.Writer) error {
2222
if t == nil {
2323
_, err := w.Write(cbg.CborNull)
2424
return err
@@ -61,8 +61,8 @@ func (t *Node[V, T]) MarshalCBOR(w io.Writer) error {
6161
return nil
6262
}
6363

64-
func (t *Node[V, T]) UnmarshalCBOR(r io.Reader) (err error) {
65-
*t = Node[V, T]{}
64+
func (t *Node[T]) UnmarshalCBOR(r io.Reader) (err error) {
65+
*t = Node[T]{}
6666

6767
cr := cbg.NewCborReader(r)
6868

@@ -124,12 +124,12 @@ func (t *Node[V, T]) UnmarshalCBOR(r io.Reader) (err error) {
124124
}
125125

126126
if extra > 0 {
127-
t.Pointers = make([]*Pointer[V, T], extra)
127+
t.Pointers = make([]*Pointer[T], extra)
128128
}
129129

130130
for i := 0; i < int(extra); i++ {
131131

132-
var v Pointer[V, T]
132+
var v Pointer[T]
133133
if err := v.UnmarshalCBOR(cr); err != nil {
134134
return err
135135
}
@@ -142,7 +142,7 @@ func (t *Node[V, T]) UnmarshalCBOR(r io.Reader) (err error) {
142142

143143
var lengthBufKV = []byte{130}
144144

145-
func (t *KV[V, T]) MarshalCBOR(w io.Writer) error {
145+
func (t *KV[T]) MarshalCBOR(w io.Writer) error {
146146
if t == nil {
147147
_, err := w.Write(cbg.CborNull)
148148
return err
@@ -174,8 +174,8 @@ func (t *KV[V, T]) MarshalCBOR(w io.Writer) error {
174174
return nil
175175
}
176176

177-
func (t *KV[V, T]) UnmarshalCBOR(r io.Reader) (err error) {
178-
*t = KV[V, T]{}
177+
func (t *KV[T]) UnmarshalCBOR(r io.Reader) (err error) {
178+
*t = KV[T]{}
179179

180180
cr := cbg.NewCborReader(r)
181181

@@ -223,7 +223,8 @@ func (t *KV[V, T]) UnmarshalCBOR(r io.Reader) (err error) {
223223

224224
{
225225

226-
var value T = new(V)
226+
var value T
227+
value = value.New()
227228
if err := value.UnmarshalCBOR(cr); err != nil {
228229
return xerrors.Errorf("failed to read field: %w", err)
229230
}

diff.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,30 @@ const (
2121

2222
// Change represents a change to a DAG and contains a reference to the old and
2323
// new CIDs.
24-
type Change[V any] struct {
24+
type Change[T HamtValue[T]] struct {
2525
Type ChangeType
2626
Key string
27-
Before *V
28-
After *V
27+
Before T
28+
After T
2929
}
3030

31-
func (ch Change[V]) String() string {
31+
func (ch Change[T]) String() string {
3232
b, _ := json.Marshal(ch)
3333
return string(b)
3434
}
3535

3636
// 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) {
3838
if prev.Equals(cur) {
3939
return nil, nil
4040
}
4141

42-
prevHamt, err := LoadNode[V, T](ctx, prevBs, prev, opts...)
42+
prevHamt, err := LoadNode[T](ctx, prevBs, prev, opts...)
4343
if err != nil {
4444
return nil, err
4545
}
4646

47-
curHamt, err := LoadNode[V, T](ctx, curBs, cur, opts...)
47+
curHamt, err := LoadNode[T](ctx, curBs, cur, opts...)
4848
if err != nil {
4949
return nil, err
5050
}
@@ -55,7 +55,7 @@ func Diff[V any, T HamtValue[V]](ctx context.Context, prevBs, curBs cbor.IpldSto
5555
return diffNode(ctx, prevHamt, curHamt, 0)
5656
}
5757

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) {
5959
// which Bitfield contains the most bits. We will start a loop from this index, calling Bitfield.Bit(idx)
6060
// on an out of range index will return zero.
6161
bp := cur.Bitfield.BitLen()
@@ -64,7 +64,7 @@ func diffNode[V any, T HamtValue[V]](ctx context.Context, pre, cur *Node[V, T],
6464
}
6565

6666
// the changes between cur and prev
67-
var changes []*Change[V]
67+
var changes []*Change[T]
6868

6969
// loop over each bit in the bitfields
7070
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],
136136
changes = append(changes, rm...)
137137
} else {
138138
for _, p := range pointer.KVs {
139-
changes = append(changes, &Change[V]{
139+
changes = append(changes, &Change[T]{
140140
Type: Remove,
141141
Key: string(p.Key),
142142
Before: p.Value,
143-
After: nil,
143+
After: zero[T](),
144144
})
145145
}
146146
}
@@ -160,10 +160,10 @@ func diffNode[V any, T HamtValue[V]](ctx context.Context, pre, cur *Node[V, T],
160160
changes = append(changes, add...)
161161
} else {
162162
for _, p := range pointer.KVs {
163-
changes = append(changes, &Change[V]{
163+
changes = append(changes, &Change[T]{
164164
Type: Add,
165165
Key: string(p.Key),
166-
Before: nil,
166+
Before: zero[T](),
167167
After: p.Value,
168168
})
169169
}
@@ -174,10 +174,10 @@ func diffNode[V any, T HamtValue[V]](ctx context.Context, pre, cur *Node[V, T],
174174
return changes, nil
175175
}
176176

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] {
178178
preMap := make(map[string]T, len(pre))
179179
curMap := make(map[string]T, len(cur))
180-
var changes []*Change[V]
180+
var changes []*Change[T]
181181

182182
for _, kv := range pre {
183183
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]
188188
// find removed keys: keys in pre and not in cur
189189
for key, value := range preMap {
190190
if _, ok := curMap[key]; !ok {
191-
changes = append(changes, &Change[V]{
191+
changes = append(changes, &Change[T]{
192192
Type: Remove,
193193
Key: key,
194194
Before: value,
195-
After: nil,
195+
After: zero[T](),
196196
})
197197
}
198198
}
199199
// find added keys: keys in cur and not in pre
200200
// find modified values: keys in cur and pre with different values
201201
for key, curVal := range curMap {
202202
if preVal, ok := preMap[key]; !ok {
203-
changes = append(changes, &Change[V]{
203+
changes = append(changes, &Change[T]{
204204
Type: Add,
205205
Key: key,
206-
Before: nil,
206+
Before: zero[T](),
207207
After: curVal,
208208
})
209209
} else {
210210
if !preVal.Equal(curVal) {
211-
changes = append(changes, &Change[V]{
211+
changes = append(changes, &Change[T]{
212212
Type: Modify,
213213
Key: key,
214214
Before: preVal,
@@ -220,13 +220,13 @@ func diffKVs[V any, T HamtValue[V]](pre, cur []*KV[V, T], idx int) []*Change[V]
220220
return changes
221221
}
222222

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]
225225
if err := node.ForEach(ctx, func(k string, val T) error {
226-
changes = append(changes, &Change[V]{
226+
changes = append(changes, &Change[T]{
227227
Type: Add,
228228
Key: k,
229-
Before: nil,
229+
Before: zero[T](),
230230
After: val,
231231
})
232232

@@ -237,14 +237,14 @@ func addAll[V any, T HamtValue[V]](ctx context.Context, node *Node[V, T], idx in
237237
return changes, nil
238238
}
239239

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]
242242
if err := node.ForEach(ctx, func(k string, val T) error {
243-
changes = append(changes, &Change[V]{
243+
changes = append(changes, &Change[T]{
244244
Type: Remove,
245245
Key: k,
246246
Before: val,
247-
After: nil,
247+
After: zero[T](),
248248
})
249249

250250
return nil

0 commit comments

Comments
 (0)