From 953448af829f9fe5bd00099a1d6bf05d0d0c3928 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 6 Jun 2016 12:31:05 +0200 Subject: [PATCH 1/3] Fix refs local marshalling License: MIT Signed-off-by: Jakub Sztandera --- core/commands/refs.go | 73 +++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/core/commands/refs.go b/core/commands/refs.go index 106ee4fe940..02f7b337ec6 100644 --- a/core/commands/refs.go +++ b/core/commands/refs.go @@ -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{ @@ -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) + 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 }, } From b2a082fb13282276ec8ffa38d5e6616866f1da92 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Mon, 6 Jun 2016 12:35:29 +0200 Subject: [PATCH 2/3] Add tests to prevent future regressions License: MIT Signed-off-by: Jakub Sztandera --- test/sharness/t0600-issues-and-regressions-online.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/sharness/t0600-issues-and-regressions-online.sh b/test/sharness/t0600-issues-and-regressions-online.sh index 89e42e8567d..8b1885da38f 100755 --- a/test/sharness/t0600-issues-and-regressions-online.sh +++ b/test/sharness/t0600-issues-and-regressions-online.sh @@ -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 From 8be37ea51bcfc54df185c3e9e99d3251a50041d6 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 7 Jun 2016 10:22:02 +0200 Subject: [PATCH 3/3] Rremove unneeded print License: MIT Signed-off-by: Jakub Sztandera --- core/commands/refs.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/commands/refs.go b/core/commands/refs.go index 02f7b337ec6..75ffffc88fd 100644 --- a/core/commands/refs.go +++ b/core/commands/refs.go @@ -3,7 +3,6 @@ package commands import ( "bytes" "errors" - "fmt" "io" "strings" @@ -169,7 +168,6 @@ var refsMarshallerMap = cmds.MarshalerMap{ marshal := func(v interface{}) (io.Reader, error) { obj, ok := v.(*RefWrapper) if !ok { - fmt.Println("%#v", v) return nil, u.ErrCast() }