coreapi: use chan for returning results in Unixfs.Ls
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Feb 1, 2019 at 19:48 UTC
11ee7503deb90c6ea72cac465fc7e21ef4bb81c5
3 files changed
+16
-13
core/coreapi/interface/tests/unixfs.go
+10
-8
@@ -754,18 +754,20 @@ func (tp *provider) TestLs(t *testing.T) {
754
t.Error(err)
755
}
756
757
- if len(links) != 1 {
758
- t.Fatalf("expected 1 link, got %d", len(links))
757
+ link := <- links
758
+ if link.Size != 23 {
759
+ t.Fatalf("expected size = 23, got %d", link.Size)
760
}
760
- if links[0].Size != 23 {
761
- t.Fatalf("expected size = 23, got %d", links[0].Size)
761
+ if link.Name != "name-of-file" {
762
+ t.Fatalf("expected name = name-of-file, got %s", link.Name)
763
}
763
- if links[0].Name != "name-of-file" {
764
- t.Fatalf("expected name = name-of-file, got %s", links[0].Name)
764
+ if link.Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
765
+ t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", link.Cid)
766
}
766
- if links[0].Cid.String() != "QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr" {
767
- t.Fatalf("expected cid = QmX3qQVKxDGz3URVC3861Z3CKtQKGBn6ffXRBBWGMFz9Lr, got %s", links[0].Cid)
767
+ if _, ok := <-links; ok {
768
+ t.Errorf("didn't expect a second link")
769
}
770
+
771
}
772
773
func (tp *provider) TestEntriesExpired(t *testing.T) {
core/coreapi/interface/unixfs.go
+1
-1
@@ -31,5 +31,5 @@ type UnixfsAPI interface {
31
Get(context.Context, Path) (files.Node, error)
32
33
// Ls returns the list of links in a directory
34
- Ls(context.Context, Path) ([]*ipld.Link, error)
34
+ Ls(context.Context, Path) (<-chan *ipld.Link, error)
35
}
core/coreapi/unixfs.go
+5
-4
@@ -143,7 +143,7 @@ func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (files.Node, er
143
144
// Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format:
145
// `<link base58 hash> <link size in bytes> <link name>`
146
-func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*ipld.Link, error) {
146
+func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) (<-chan *ipld.Link, error) {
147
dagnode, err := api.core().ResolveNode(ctx, p)
148
if err != nil {
149
return nil, err
@@ -164,10 +164,11 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*ipld.Link, e
164
return nil, err
165
}
166
167
- links := make([]*ipld.Link, len(ndlinks))
168
- for i, l := range ndlinks {
169
- links[i] = &ipld.Link{Name: l.Name, Size: l.Size, Cid: l.Cid}
167
+ links := make(chan *ipld.Link, len(ndlinks))
168
+ for _, l := range ndlinks {
169
+ links <- &ipld.Link{Name: l.Name, Size: l.Size, Cid: l.Cid}
170
}
171
+ close(links)
172
return links, nil
173
}
174