From 5bdbd256fbd325a7565397b4b86b1b4dcbe5064b Mon Sep 17 00:00:00 2001 From: gammazero <11790789+gammazero@users.noreply.github.com> Date: Sun, 15 Feb 2026 20:47:51 -1000 Subject: [PATCH] refactor: modernize code - interface{} to any, use min/max functions, etc. - update dependencies --- batch_test.go | 13 +++++-------- daghelpers_test.go | 8 +++----- format.go | 2 +- format_test.go | 4 ++-- go.mod | 4 ++-- go.sum | 8 ++++---- navipld.go | 5 +---- 7 files changed, 18 insertions(+), 26 deletions(-) diff --git a/batch_test.go b/batch_test.go index 28db71d..c91365f 100644 --- a/batch_test.go +++ b/batch_test.go @@ -79,12 +79,11 @@ func (d *testDag) RemoveMany(ctx context.Context, cids []cid.Cid) error { var _ DAGService = new(testDag) func TestBatch(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() d := newTestDag() b := NewBatch(ctx, d) - for i := 0; i < 1000; i++ { + for range 1000 { // It would be great if we could use *many* different nodes here // but we can't add any dependencies and I don't feel like adding // any more testing code. @@ -113,11 +112,10 @@ func TestBatch(t *testing.T) { func TestBufferedDAG(t *testing.T) { ds := newTestDag() - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() var bdag DAGService = NewBufferedDAG(ctx, ds) - for i := 0; i < 1000; i++ { + for range 1000 { n := new(EmptyNode) if err := bdag.Add(ctx, n); err != nil { t.Fatal(err) @@ -132,8 +130,7 @@ func TestBufferedDAG(t *testing.T) { } func TestBatchOptions(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() wantMaxSize := 8 << 10 wantMaxNodes := 500 diff --git a/daghelpers_test.go b/daghelpers_test.go index e2d9b0a..0d743a1 100644 --- a/daghelpers_test.go +++ b/daghelpers_test.go @@ -1,7 +1,6 @@ package format import ( - "context" "testing" "github.com/ipfs/go-cid" @@ -28,7 +27,7 @@ func InitNode(d []byte) *TestNode { } } -func (n *TestNode) Resolve([]string) (interface{}, []string, error) { +func (n *TestNode) Resolve([]string) (any, []string, error) { return nil, nil, ErrEmptyNode } @@ -56,7 +55,7 @@ func (n *TestNode) Links() []*Link { return n.links } -func (n *TestNode) Loggable() map[string]interface{} { +func (n *TestNode) Loggable() map[string]any { return nil } @@ -102,8 +101,7 @@ func (n *TestNode) AddRawLink(name string, l *Link) error { } func TestCopy(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() from := newTestDag() root := InitNode([]byte("level0")) diff --git a/format.go b/format.go index 990e1ce..3884c37 100644 --- a/format.go +++ b/format.go @@ -12,7 +12,7 @@ import ( type Resolver interface { // Resolve resolves a path through this node, stopping at any link boundary // and returning the object found as well as the remaining path to traverse - Resolve(path []string) (interface{}, []string, error) + Resolve(path []string) (any, []string, error) // Tree lists all paths within the object under 'path', and up to the given depth. // To list the entire object (similar to `find .`) pass "" and -1 diff --git a/format_test.go b/format_test.go index 732d660..827aa89 100644 --- a/format_test.go +++ b/format_test.go @@ -12,7 +12,7 @@ type EmptyNode struct{} var ErrEmptyNode error = errors.New("dummy node") -func (n *EmptyNode) Resolve([]string) (interface{}, []string, error) { +func (n *EmptyNode) Resolve([]string) (any, []string, error) { return nil, nil, ErrEmptyNode } @@ -46,7 +46,7 @@ func (n *EmptyNode) Links() []*Link { return nil } -func (n *EmptyNode) Loggable() map[string]interface{} { +func (n *EmptyNode) Loggable() map[string]any { return nil } diff --git a/go.mod b/go.mod index 056791d..3fb586e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.24.0 require ( github.com/ipfs/go-block-format v0.2.3 - github.com/ipfs/go-cid v0.5.0 + github.com/ipfs/go-cid v0.6.0 github.com/multiformats/go-multihash v0.2.3 ) @@ -16,7 +16,7 @@ require ( github.com/multiformats/go-base32 v0.1.0 // indirect github.com/multiformats/go-base36 v0.2.0 // indirect github.com/multiformats/go-multibase v0.2.0 // indirect - github.com/multiformats/go-varint v0.0.7 // indirect + github.com/multiformats/go-varint v0.1.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect golang.org/x/crypto v0.41.0 // indirect golang.org/x/sys v0.35.0 // indirect diff --git a/go.sum b/go.sum index c6bd666..215cc94 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/ipfs/boxo v0.34.0 h1:pMP9bAsTs4xVh8R0ZmxIWviV7kjDa60U24QrlGgHb1g= github.com/ipfs/boxo v0.34.0/go.mod h1:kzdH/ewDybtO3+M8MCVkpwnIIc/d2VISX95DFrY4vQA= github.com/ipfs/go-block-format v0.2.3 h1:mpCuDaNXJ4wrBJLrtEaGFGXkferrw5eqVvzaHhtFKQk= github.com/ipfs/go-block-format v0.2.3/go.mod h1:WJaQmPAKhD3LspLixqlqNFxiZ3BZ3xgqxxoSR/76pnA= -github.com/ipfs/go-cid v0.5.0 h1:goEKKhaGm0ul11IHA7I6p1GmKz8kEYniqFopaB5Otwg= -github.com/ipfs/go-cid v0.5.0/go.mod h1:0L7vmeNXpQpUS9vt+yEARkJ8rOg43DF3iPgn4GIN0mk= +github.com/ipfs/go-cid v0.6.0 h1:DlOReBV1xhHBhhfy/gBNNTSyfOM6rLiIx9J7A4DGf30= +github.com/ipfs/go-cid v0.6.0/go.mod h1:NC4kS1LZjzfhK40UGmpXv5/qD2kcMzACYJNntCUiDhQ= github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM= @@ -18,8 +18,8 @@ github.com/multiformats/go-multibase v0.2.0 h1:isdYCVLvksgWlMW9OZRYJEa9pZETFivnc github.com/multiformats/go-multibase v0.2.0/go.mod h1:bFBZX4lKCA/2lyOFSAoKH5SS6oPyjtnzK/XTFDPkNuk= github.com/multiformats/go-multihash v0.2.3 h1:7Lyc8XfX/IY2jWb/gI7JP+o7JEq9hOa7BFvVU9RSh+U= github.com/multiformats/go-multihash v0.2.3/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM= -github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/nEGOHFS8= -github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU= +github.com/multiformats/go-varint v0.1.0 h1:i2wqFp4sdl3IcIxfAonHQV9qU5OsZ4Ts9IOoETFs5dI= +github.com/multiformats/go-varint v0.1.0/go.mod h1:5KVAVXegtfmNQQm/lCY+ATvDzvJJhSkUlGQV9wgObdI= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4= diff --git a/navipld.go b/navipld.go index eed3e51..489cc53 100644 --- a/navipld.go +++ b/navipld.go @@ -87,10 +87,7 @@ const preloadSize = 10 // Preload at most `preloadSize` child nodes from `beg` through promises // created using this `ctx`. func (nn *NavigableIPLDNode) preload(ctx context.Context, beg uint) { - end := beg + preloadSize - if end >= uint(len(nn.childCIDs)) { - end = uint(len(nn.childCIDs)) - } + end := min(beg+preloadSize, uint(len(nn.childCIDs))) copy(nn.childPromises[beg:], GetNodes(ctx, nn.nodeGetter, nn.childCIDs[beg:end])) }