Skip to content
This repository has been archived by the owner on Aug 9, 2018. It is now read-only.

Commit

Permalink
fix protocol buffer codec
Browse files Browse the repository at this point in the history
  • Loading branch information
mildred committed Feb 13, 2016
1 parent c39006e commit 826050a
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 113 deletions.
26 changes: 8 additions & 18 deletions coding/coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ import (

cbor "github.com/ipfs/go-ipld/coding/cbor"
json "github.com/ipfs/go-ipld/coding/json"
mc "github.com/jbenet/go-multicodec"
mcproto "github.com/jbenet/go-multicodec/protobuf"

pb "github.com/ipfs/go-ipld/coding/pb"

memory "github.com/ipfs/go-ipld/memory"
stream "github.com/ipfs/go-ipld/stream"
mc "github.com/jbenet/go-multicodec"
)

var Header []byte

const (
HeaderPath = "/mdagv1"
ProtobufPath = "/protobuf/msgio"
HeaderPath = "/mdagv1"
)

type Codec int
Expand Down Expand Up @@ -47,8 +45,8 @@ func init() {
cbor.HeaderWithTagsPath: func(r io.ReadSeeker) (stream.NodeReader, error) {
return cbor.NewCBORDecoder(r)
},
ProtobufPath: func(r io.ReadSeeker) (stream.NodeReader, error) {
return DecodeLegacyProtobuf(r)
pb.MsgIOHeaderPath: func(r io.ReadSeeker) (stream.NodeReader, error) {
return pb.Decode(mc.WrapHeaderReader(pb.MsgIOHeader, r))
},
}
}
Expand All @@ -57,7 +55,7 @@ func Decode(r io.ReadSeeker) (stream.NodeReader, error) {
// get multicodec first header, should be mcmux.Header
err := mc.ConsumeHeader(r, Header)
if err != nil {
return DecodeLegacyProtobuf(r)
return nil, err
}

// get next header, to select codec
Expand All @@ -79,16 +77,8 @@ func DecodeBytes(data []byte) (stream.NodeReader, error) {
return Decode(bytes.NewReader(data))
}

func DecodeLegacyProtobuf(r io.Reader) (stream.NodeReader, error) {
var node memory.Node = memory.Node{}
r = mc.WrapHeaderReader(mcproto.HeaderMsgio, r)
r = mc.WrapHeaderReader(pb.Header, r)
err := pb.Multicodec().Decoder(r).Decode(&node)
return node, err
}

func DecodeLegacyProtobufBytes(data []byte) (stream.NodeReader, error) {
return DecodeLegacyProtobuf(bytes.NewReader(data))
return pb.RawDecode(data)
}

func EncodeRaw(codec Codec, w io.Writer, node memory.Node) error {
Expand All @@ -100,7 +90,7 @@ func EncodeRaw(codec Codec, w io.Writer, node memory.Node) error {
case CodecJSON:
return json.Encode(w, node)
case CodecProtobuf:
return fmt.Errorf("Protocol Buffer codec is not writeable")
return pb.Encode(w, node, true)
default:
return fmt.Errorf("Unknown codec %v", codec)
}
Expand Down
60 changes: 30 additions & 30 deletions coding/coding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"testing"

links "github.com/ipfs/go-ipld/links"
memory "github.com/ipfs/go-ipld/memory"
reader "github.com/ipfs/go-ipld/stream"
readertest "github.com/ipfs/go-ipld/test"
Expand Down Expand Up @@ -37,8 +38,7 @@ type TC struct {

// Test decoding and encoding a json and cbor file
func TestCodecsEncodeDecode(t *testing.T) {
for _, fname := range []string{"json.testfile", "cbor.testfile"} {
testfile := codedFiles[fname]
for fname, testfile := range codedFiles {

r, err := DecodeBytes(testfile)
if err != nil {
Expand All @@ -52,16 +52,22 @@ func TestCodecsEncodeDecode(t *testing.T) {
codec = CodecJSON
case "cbor.testfile":
codec = CodecCBOR
case "protobuf.testfile":
codec = CodecProtobuf
default:
panic("should not arrive here")
}

t.Logf("Decoded %s: %#v", fname, r)

n, err := memory.NewNodeFrom(r)
if err != nil {
t.Error(err)
continue
}

t.Logf("In memory %s: %#v", fname, n)

outData, err := EncodeBytes(codec, n)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -120,41 +126,35 @@ func TestCborStream(t *testing.T) {
func TestPbStream(t *testing.T) {
a := assrt.NewAssert(t)
t.Logf("Reading protobuf.testfile")
t.Logf("Bytes: %v", codedFiles["protobuf.testfile"])
pb, err := Decode(bytes.NewReader(codedFiles["protobuf.testfile"]))
a.MustNil(err)

readertest.CheckReader(t, pb, []readertest.Callback{
readertest.Callback{[]interface{}{}, reader.TokenNode, nil},
readertest.Callback{[]interface{}{}, reader.TokenKey, "data"},
readertest.Callback{[]interface{}{"data"}, reader.TokenValue, []byte{0x08, 0x01}},
readertest.Callback{[]interface{}{}, reader.TokenKey, "named-links"},
readertest.Callback{[]interface{}{"named-links"}, reader.TokenNode, nil},
readertest.Callback{[]interface{}{"named-links"}, reader.TokenKey, "a"},
readertest.Callback{[]interface{}{"named-links", "a"}, reader.TokenNode, nil},
readertest.Callback{[]interface{}{"named-links", "a"}, reader.TokenKey, "link"},
readertest.Callback{[]interface{}{"named-links", "a", "link"}, reader.TokenValue, "Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"},
readertest.Callback{[]interface{}{"named-links", "a"}, reader.TokenKey, "name"},
readertest.Callback{[]interface{}{"named-links", "a", "name"}, reader.TokenValue, "a"},
readertest.Callback{[]interface{}{"named-links", "a"}, reader.TokenKey, "size"},
readertest.Callback{[]interface{}{"named-links", "a", "size"}, reader.TokenValue, uint64(10)},
readertest.Callback{[]interface{}{"named-links", "a"}, reader.TokenEndNode, nil},
readertest.Callback{[]interface{}{"named-links"}, reader.TokenKey, "b"},
readertest.Callback{[]interface{}{"named-links", "b"}, reader.TokenNode, nil},
readertest.Callback{[]interface{}{"named-links", "b"}, reader.TokenKey, "link"},
readertest.Callback{[]interface{}{"named-links", "b", "link"}, reader.TokenValue, "QmR9pC5uCF3UExca8RSrCVL8eKv7nHMpATzbEQkAHpXmVM"},
readertest.Callback{[]interface{}{"named-links", "b"}, reader.TokenKey, "name"},
readertest.Callback{[]interface{}{"named-links", "b", "name"}, reader.TokenValue, "b"},
readertest.Callback{[]interface{}{"named-links", "b"}, reader.TokenKey, "size"},
readertest.Callback{[]interface{}{"named-links", "b", "size"}, reader.TokenValue, uint64(10)},
readertest.Callback{[]interface{}{"named-links", "b"}, reader.TokenEndNode, nil},
readertest.Callback{[]interface{}{"named-links"}, reader.TokenEndNode, nil},
readertest.Callback{[]interface{}{}, reader.TokenKey, "ordered-links"},
readertest.Callback{[]interface{}{"ordered-links"}, reader.TokenArray, nil},
readertest.Callback{[]interface{}{"ordered-links"}, reader.TokenIndex, 0},
readertest.Callback{[]interface{}{"ordered-links", 0}, reader.TokenValue, "a"},
readertest.Callback{[]interface{}{"ordered-links"}, reader.TokenIndex, 1},
readertest.Callback{[]interface{}{"ordered-links", 1}, reader.TokenValue, "b"},
readertest.Callback{[]interface{}{"ordered-links"}, reader.TokenEndArray, nil},
readertest.Callback{[]interface{}{}, reader.TokenKey, "links"},
readertest.Callback{[]interface{}{"links"}, reader.TokenArray, nil},
readertest.Callback{[]interface{}{"links"}, reader.TokenIndex, 0},
readertest.Callback{[]interface{}{"links", 0}, reader.TokenNode, nil},
readertest.Callback{[]interface{}{"links", 0}, reader.TokenKey, links.LinkKey},
readertest.Callback{[]interface{}{"links", 0, links.LinkKey}, reader.TokenValue, "Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9"},
readertest.Callback{[]interface{}{"links", 0}, reader.TokenKey, "name"},
readertest.Callback{[]interface{}{"links", 0, "name"}, reader.TokenValue, "a"},
readertest.Callback{[]interface{}{"links", 0}, reader.TokenKey, "size"},
readertest.Callback{[]interface{}{"links", 0, "size"}, reader.TokenValue, uint64(10)},
readertest.Callback{[]interface{}{"links", 0}, reader.TokenEndNode, nil},
readertest.Callback{[]interface{}{"links"}, reader.TokenIndex, 1},
readertest.Callback{[]interface{}{"links", 1}, reader.TokenNode, nil},
readertest.Callback{[]interface{}{"links", 1}, reader.TokenKey, links.LinkKey},
readertest.Callback{[]interface{}{"links", 1, links.LinkKey}, reader.TokenValue, "QmR9pC5uCF3UExca8RSrCVL8eKv7nHMpATzbEQkAHpXmVM"},
readertest.Callback{[]interface{}{"links", 1}, reader.TokenKey, "name"},
readertest.Callback{[]interface{}{"links", 1, "name"}, reader.TokenValue, "b"},
readertest.Callback{[]interface{}{"links", 1}, reader.TokenKey, "size"},
readertest.Callback{[]interface{}{"links", 1, "size"}, reader.TokenValue, uint64(10)},
readertest.Callback{[]interface{}{"links", 1}, reader.TokenEndNode, nil},
readertest.Callback{[]interface{}{"links"}, reader.TokenEndArray, nil},
readertest.Callback{[]interface{}{}, reader.TokenEndNode, nil},
})
}
Loading

0 comments on commit 826050a

Please sign in to comment.