Skip to content

Commit

Permalink
Merge pull request #2812 from ipfs/feature/refs-local-api
Browse files Browse the repository at this point in the history
Fix refs local marshalling
  • Loading branch information
whyrusleeping committed Jun 10, 2016
2 parents 62fb45b + 8be37ea commit 444a8dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
73 changes: 35 additions & 38 deletions core/commands/refs.go
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"bytes"
"errors"
"fmt"
"io"
"strings"

Expand Down Expand Up @@ -117,35 +116,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 +143,46 @@ 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 {
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
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

0 comments on commit 444a8dc

Please sign in to comment.