-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Golint: fix golint warnings in merkledag submodule #4665
Changes from 1 commit
2d138b0
2743dd2
d04de00
69497a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,28 +14,34 @@ type ErrorService struct { | |
|
||
var _ ipld.DAGService = (*ErrorService)(nil) | ||
|
||
// Add returns an error. | ||
func (cs *ErrorService) Add(ctx context.Context, nd ipld.Node) error { | ||
return cs.Err | ||
} | ||
|
||
// AddMany returns an error. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
func (cs *ErrorService) AddMany(ctx context.Context, nds []ipld.Node) error { | ||
return cs.Err | ||
} | ||
|
||
// Get returns an error. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Etc. |
||
func (cs *ErrorService) Get(ctx context.Context, c *cid.Cid) (ipld.Node, error) { | ||
return nil, cs.Err | ||
} | ||
|
||
// GetMany many returns an error. | ||
func (cs *ErrorService) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *ipld.NodeOption { | ||
ch := make(chan *ipld.NodeOption) | ||
close(ch) | ||
return ch | ||
} | ||
|
||
// Remove returns an error. | ||
func (cs *ErrorService) Remove(ctx context.Context, c *cid.Cid) error { | ||
return cs.Err | ||
} | ||
|
||
// RemoveMany returns an error. | ||
func (cs *ErrorService) RemoveMany(ctx context.Context, cids []*cid.Cid) error { | ||
return cs.Err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// package merkledag implements the IPFS Merkle DAG datastructures. | ||
// Package merkledag implements the IPFS Merkle DAG data structures. | ||
package merkledag | ||
|
||
import ( | ||
|
@@ -23,8 +23,14 @@ func init() { | |
ipld.Register(cid.DagCBOR, ipldcbor.DecodeBlock) | ||
} | ||
|
||
// contextKey is a type to use as value for the ProgressTracker contexts. | ||
type contextKey string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would export this type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has no use outside. If someone ever has a use, it can be exported then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, your right. Sorry. |
||
|
||
const progressContextKey contextKey = "progress" | ||
|
||
// NewDAGService constructs a new DAGService (using the default implementation). | ||
func NewDAGService(bs bserv.BlockService) *dagService { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does golint really complain about this? I actually prefer to return concrete types whenever possible. It helps to deal with multiple potentially fulfilled interfaces. For example, if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Want me to export |
||
// Note that the default implementation is also an ipld.LinkGetter. | ||
func NewDAGService(bs bserv.BlockService) ipld.DAGService { | ||
return &dagService{Blocks: bs} | ||
} | ||
|
||
|
@@ -147,8 +153,8 @@ func (sg *sesGetter) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *ipld. | |
} | ||
|
||
// Session returns a NodeGetter using a new session for block fetches. | ||
func (ds *dagService) Session(ctx context.Context) ipld.NodeGetter { | ||
return &sesGetter{bserv.NewSession(ctx, ds.Blocks)} | ||
func (n *dagService) Session(ctx context.Context) ipld.NodeGetter { | ||
return &sesGetter{bserv.NewSession(ctx, n.Blocks)} | ||
} | ||
|
||
// FetchGraph fetches all nodes that are children of the given node | ||
|
@@ -159,7 +165,7 @@ func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error | |
ng = &sesGetter{bserv.NewSession(ctx, ds.Blocks)} | ||
} | ||
|
||
v, _ := ctx.Value("progress").(*ProgressTracker) | ||
v, _ := ctx.Value(progressContextKey).(*ProgressTracker) | ||
if v == nil { | ||
return EnumerateChildrenAsync(ctx, GetLinksDirect(ng), root, cid.NewSet().Visit) | ||
} | ||
|
@@ -168,9 +174,8 @@ func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error | |
if set.Visit(c) { | ||
v.Increment() | ||
return true | ||
} else { | ||
return false | ||
} | ||
return false | ||
} | ||
return EnumerateChildrenAsync(ctx, GetLinksDirect(ng), root, visit) | ||
} | ||
|
@@ -179,8 +184,8 @@ func FetchGraph(ctx context.Context, root *cid.Cid, serv ipld.DAGService) error | |
// returns the indexes of any links pointing to it | ||
func FindLinks(links []*cid.Cid, c *cid.Cid, start int) []int { | ||
var out []int | ||
for i, lnk_c := range links[start:] { | ||
if c.Equals(lnk_c) { | ||
for i, lnkC := range links[start:] { | ||
if c.Equals(lnkC) { | ||
out = append(out, i+start) | ||
} | ||
} | ||
|
@@ -265,21 +270,26 @@ func EnumerateChildren(ctx context.Context, getLinks GetLinks, root *cid.Cid, vi | |
return nil | ||
} | ||
|
||
// ProgressTracker is used to show progress when fetching nodes. | ||
type ProgressTracker struct { | ||
Total int | ||
lk sync.Mutex | ||
} | ||
|
||
// DeriveContext returns a new context with value "progress" derived from | ||
// the given one. | ||
func (p *ProgressTracker) DeriveContext(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, "progress", p) | ||
return context.WithValue(ctx, progressContextKey, p) | ||
} | ||
|
||
// Increment adds one to the total progress. | ||
func (p *ProgressTracker) Increment() { | ||
p.lk.Lock() | ||
defer p.lk.Unlock() | ||
p.Total++ | ||
} | ||
|
||
// Value returns the current progress. | ||
func (p *ProgressTracker) Value() int { | ||
p.lk.Lock() | ||
defer p.lk.Unlock() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ import ( | |
"testing" | ||
"time" | ||
|
||
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be grouped with |
||
|
||
bserv "github.com/ipfs/go-ipfs/blockservice" | ||
bstest "github.com/ipfs/go-ipfs/blockservice/test" | ||
offline "github.com/ipfs/go-ipfs/exchange/offline" | ||
|
@@ -22,7 +24,6 @@ import ( | |
mdpb "github.com/ipfs/go-ipfs/merkledag/pb" | ||
dstest "github.com/ipfs/go-ipfs/merkledag/test" | ||
uio "github.com/ipfs/go-ipfs/unixfs/io" | ||
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format" | ||
|
||
u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util" | ||
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" | ||
|
@@ -241,9 +242,10 @@ func TestFetchGraph(t *testing.T) { | |
// create an offline dagstore and ensure all blocks were fetched | ||
bs := bserv.New(bsis[1].Blockstore(), offline.Exchange(bsis[1].Blockstore())) | ||
|
||
offline_ds := NewDAGService(bs) | ||
// we know the default dagService implements LinkGetter | ||
offlineDS := NewDAGService(bs).(ipld.LinkGetter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is exactly the case for having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it is a case only used in tests (and tests in the same module). From an external user's point of view, it's not possible to know what *dagService can do because it's unexported. |
||
|
||
err = EnumerateChildren(context.Background(), offline_ds.GetLinks, root.Cid(), func(_ *cid.Cid) bool { return true }) | ||
err = EnumerateChildren(context.Background(), offlineDS.GetLinks, root.Cid(), func(_ *cid.Cid) bool { return true }) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -260,7 +262,9 @@ func TestEnumerateChildren(t *testing.T) { | |
} | ||
|
||
set := cid.NewSet() | ||
err = EnumerateChildren(context.Background(), ds.GetLinks, root.Cid(), set.Visit) | ||
lg := ds.(ipld.LinkGetter) | ||
|
||
err = EnumerateChildren(context.Background(), lg.GetLinks, root.Cid(), set.Visit) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -491,7 +495,7 @@ func TestCidRetention(t *testing.T) { | |
} | ||
|
||
func TestCidRawDoesnNeedData(t *testing.T) { | ||
srv := NewDAGService(dstest.Bserv()) | ||
srv := NewDAGService(dstest.Bserv()).(ipld.LinkGetter) | ||
nd := NewRawNode([]byte("somedata")) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this description confusing, rather than say "an error" something like "returns ErrorService.Err" or maybe just "returns the error" rather than "an error".