-
-
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
chore: return errors using channels and not embedded in result type #10527
Open
gammazero
wants to merge
6
commits into
master
Choose a base branch
from
use-error-channels
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
940dde4
Return errors using channles and not embedded in result type
gammazero 09815c1
Add LsIter to iterate items from UnixfsAPI interfaces
gammazero 58f7a25
wip
gammazero 10fee6d
APIs take chan argument and return error
gammazero 8ac3d5b
fix sharowed variable error
gammazero 22f399d
boxo with Ls2
gammazero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,10 +144,12 @@ type lsOutput struct { | |
Objects []lsObject | ||
} | ||
|
||
func (api *UnixfsAPI) Ls(ctx context.Context, p path.Path, opts ...caopts.UnixfsLsOption) (<-chan iface.DirEntry, error) { | ||
func (api *UnixfsAPI) Ls(ctx context.Context, p path.Path, out chan<- iface.DirEntry, opts ...caopts.UnixfsLsOption) error { | ||
defer close(out) | ||
|
||
options, err := caopts.UnixfsLsOptions(opts...) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
resp, err := api.core().Request("ls", p.String()). | ||
|
@@ -156,86 +158,66 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p path.Path, opts ...caopts.Unixfs | |
Option("stream", true). | ||
Send(ctx) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
if resp.Error != nil { | ||
return nil, resp.Error | ||
return err | ||
} | ||
defer resp.Close() | ||
|
||
dec := json.NewDecoder(resp.Output) | ||
out := make(chan iface.DirEntry) | ||
|
||
go func() { | ||
defer resp.Close() | ||
defer close(out) | ||
|
||
for { | ||
var link lsOutput | ||
if err := dec.Decode(&link); err != nil { | ||
if err == io.EOF { | ||
return | ||
} | ||
select { | ||
case out <- iface.DirEntry{Err: err}: | ||
case <-ctx.Done(): | ||
} | ||
return | ||
for { | ||
var link lsOutput | ||
if err = dec.Decode(&link); err != nil { | ||
if err != io.EOF { | ||
return err | ||
} | ||
break | ||
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. idem |
||
} | ||
|
||
if len(link.Objects) != 1 { | ||
select { | ||
case out <- iface.DirEntry{Err: errors.New("unexpected Objects len")}: | ||
case <-ctx.Done(): | ||
} | ||
return | ||
} | ||
if len(link.Objects) != 1 { | ||
return errors.New("unexpected Objects len") | ||
} | ||
|
||
if len(link.Objects[0].Links) != 1 { | ||
select { | ||
case out <- iface.DirEntry{Err: errors.New("unexpected Links len")}: | ||
case <-ctx.Done(): | ||
} | ||
return | ||
} | ||
if len(link.Objects[0].Links) != 1 { | ||
return errors.New("unexpected Links len") | ||
} | ||
|
||
l0 := link.Objects[0].Links[0] | ||
l0 := link.Objects[0].Links[0] | ||
|
||
c, err := cid.Decode(l0.Hash) | ||
if err != nil { | ||
select { | ||
case out <- iface.DirEntry{Err: err}: | ||
case <-ctx.Done(): | ||
} | ||
return | ||
} | ||
c, err := cid.Decode(l0.Hash) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var ftype iface.FileType | ||
switch l0.Type { | ||
case unixfs.TRaw, unixfs.TFile: | ||
ftype = iface.TFile | ||
case unixfs.THAMTShard, unixfs.TDirectory, unixfs.TMetadata: | ||
ftype = iface.TDirectory | ||
case unixfs.TSymlink: | ||
ftype = iface.TSymlink | ||
} | ||
var ftype iface.FileType | ||
switch l0.Type { | ||
case unixfs.TRaw, unixfs.TFile: | ||
ftype = iface.TFile | ||
case unixfs.THAMTShard, unixfs.TDirectory, unixfs.TMetadata: | ||
ftype = iface.TDirectory | ||
case unixfs.TSymlink: | ||
ftype = iface.TSymlink | ||
} | ||
|
||
select { | ||
case out <- iface.DirEntry{ | ||
Name: l0.Name, | ||
Cid: c, | ||
Size: l0.Size, | ||
Type: ftype, | ||
Target: l0.Target, | ||
|
||
Mode: l0.Mode, | ||
ModTime: l0.ModTime, | ||
}: | ||
case <-ctx.Done(): | ||
} | ||
select { | ||
case out <- iface.DirEntry{ | ||
Name: l0.Name, | ||
Cid: c, | ||
Size: l0.Size, | ||
Type: ftype, | ||
Target: l0.Target, | ||
|
||
Mode: l0.Mode, | ||
ModTime: l0.ModTime, | ||
}: | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
}() | ||
} | ||
|
||
return out, nil | ||
return nil | ||
} | ||
|
||
func (api *UnixfsAPI) core() *HttpApi { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
Just a nit. No need to scroll down to the end of the loop to make sure we are fully done.