Skip to content
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

Fix refs local marshalling #2812

Merged
merged 3 commits into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 36 additions & 37 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,8 @@ NOTE: List all references recursively by using the flag '-r'.
}
}()
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
outChan, ok := res.Output().(<-chan interface{})
if !ok {
return nil, u.ErrCast()
}

marshal := func(v interface{}) (io.Reader, error) {
obj, ok := v.(*RefWrapper)
if !ok {
fmt.Println("%#v", v)
return nil, u.ErrCast()
}

if obj.Err != "" {
return nil, errors.New(obj.Err)
}

return strings.NewReader(obj.Ref + "\n"), nil
}

return &cmds.ChannelMarshaler{
Channel: outChan,
Marshaler: marshal,
Res: res,
}, nil
},
},
Type: RefWrapper{},
Marshalers: refsMarshallerMap,
Type: RefWrapper{},
}

var RefsLocalCmd = &cmds.Command{
Expand All @@ -171,21 +144,47 @@ Displays the hashes of all local objects.
return
}

piper, pipew := io.Pipe()
out := make(chan interface{})
res.SetOutput((<-chan interface{})(out))

go func() {
defer pipew.Close()
defer close(out)

for k := range allKeys {
s := k.B58String() + "\n"
if _, err := pipew.Write([]byte(s)); err != nil {
log.Error("pipe write error: ", err)
return
}
out <- &RefWrapper{Ref: k.B58String()}
}
}()
},
Marshalers: refsMarshallerMap,
Type: RefWrapper{},
}

var refsMarshallerMap = cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
outChan, ok := res.Output().(<-chan interface{})
if !ok {
return nil, u.ErrCast()
}

marshal := func(v interface{}) (io.Reader, error) {
obj, ok := v.(*RefWrapper)
if !ok {
fmt.Println("%#v", v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should make this a log.Errorf or a log.Warningf

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it isn't needed, it might be a left over from someone debugging something.
It got here when I was rearranging the code.

return nil, u.ErrCast()
}

if obj.Err != "" {
return nil, errors.New(obj.Err)
}

return strings.NewReader(obj.Ref + "\n"), nil
}

res.SetOutput(piper)
return &cmds.ChannelMarshaler{
Channel: outChan,
Marshaler: marshal,
Res: res,
}, nil
},
}

Expand Down
5 changes: 5 additions & 0 deletions test/sharness/t0600-issues-and-regressions-online.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ test_expect_sucess "commands command with flag flags works via HTTP API - #2301"
curl "http://$API_ADDR/api/v0/commands?flags" | grep "verbose"
'

test_expect_sucess "ipfs refs local over HTTP API returns NDJOSN not flat - #2803" '
echo "Hello World" | ipfs add &&
curl "http://$API_ADDR/api/v0/refs/local" | grep "Ref" | grep "Err"
'

test_kill_ipfs_daemon

test_done
Expand Down