提交 444a8dc2 作者: Jeromy Johnson 提交者: GitHub

Merge pull request #2812 from ipfs/feature/refs-local-api

Fix refs local marshalling
...@@ -3,7 +3,6 @@ package commands ...@@ -3,7 +3,6 @@ package commands
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"io" "io"
"strings" "strings"
...@@ -117,34 +116,7 @@ NOTE: List all references recursively by using the flag '-r'. ...@@ -117,34 +116,7 @@ NOTE: List all references recursively by using the flag '-r'.
} }
}() }()
}, },
Marshalers: cmds.MarshalerMap{ Marshalers: refsMarshallerMap,
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{}, Type: RefWrapper{},
} }
...@@ -171,21 +143,46 @@ Displays the hashes of all local objects. ...@@ -171,21 +143,46 @@ Displays the hashes of all local objects.
return return
} }
piper, pipew := io.Pipe() out := make(chan interface{})
res.SetOutput((<-chan interface{})(out))
go func() { go func() {
defer pipew.Close() defer close(out)
for k := range allKeys { for k := range allKeys {
s := k.B58String() + "\n" out <- &RefWrapper{Ref: k.B58String()}
if _, err := pipew.Write([]byte(s)); err != nil {
log.Error("pipe write error: ", err)
return
}
} }
}() }()
},
Marshalers: refsMarshallerMap,
Type: RefWrapper{},
}
res.SetOutput(piper) 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
}
return &cmds.ChannelMarshaler{
Channel: outChan,
Marshaler: marshal,
Res: res,
}, nil
}, },
} }
......
...@@ -14,6 +14,11 @@ test_expect_sucess "commands command with flag flags works via HTTP API - #2301" ...@@ -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" 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_kill_ipfs_daemon
test_done test_done
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论