Skip to content

Commit

Permalink
chore(ipld): switch to using top-level ipld-prime codec helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed May 27, 2022
1 parent 49f40a5 commit 36a159c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
19 changes: 6 additions & 13 deletions ipldutil/ipldutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@ package ipldutil
import (
"bytes"

"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
_ "github.com/ipld/go-ipld-prime/codec/raw"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
)

func EncodeNode(node datamodel.Node) ([]byte, error) {
var buffer bytes.Buffer
err := EncodeNodeInto(node, &buffer)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
return ipld.Encode(node, dagcbor.Encode)
}

func EncodeNodeInto(node datamodel.Node, buffer *bytes.Buffer) error {
return dagcbor.Encode(node, buffer)
return ipld.EncodeStreaming(buffer, node, dagcbor.Encode)
}

func DecodeNode(encoded []byte) (datamodel.Node, error) {
return DecodeNodeInto(encoded, basicnode.Prototype.Any.NewBuilder())
return DecodeNodeInto(encoded, basicnode.Prototype.Any)
}

func DecodeNodeInto(encoded []byte, nb datamodel.NodeBuilder) (datamodel.Node, error) {
if err := dagcbor.Decode(nb, bytes.NewReader(encoded)); err != nil {
return nil, err
}
return nb.Build(), nil
func DecodeNodeInto(encoded []byte, proto datamodel.NodePrototype) (datamodel.Node, error) {
return ipld.DecodeUsingPrototype(encoded, dagcbor.Decode, proto)
}
6 changes: 2 additions & 4 deletions message/message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package message

import (
"bytes"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -47,9 +46,8 @@ type GraphSyncRequest struct {
func (gsr GraphSyncRequest) String() string {
sel := "nil"
if gsr.selector != nil {
var buf bytes.Buffer
dagjson.Encode(gsr.selector, &buf)
sel = buf.String()
byts, _ := ipld.Encode(gsr.selector, dagjson.Encode)
sel = string(byts)
}
extStr := strings.Builder{}
for _, name := range gsr.ExtensionNames() {
Expand Down
10 changes: 3 additions & 7 deletions message/v2/ipld_roundtrip_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package v2

import (
"bytes"
"testing"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
Expand Down Expand Up @@ -65,18 +65,14 @@ func TestIPLDRoundTrip(t *testing.T) {
require.NoError(t, err)

// ipld TypedNode format
var buf bytes.Buffer
node := bindnode.Wrap(igsm, ipldbind.Prototype.Message.Type())

byts, err := ipld.Encode(node, dagcbor.Encode)
// dag-cbor binary format
err = dagcbor.Encode(node.Representation(), &buf)
require.NoError(t, err)

// back to bindnode internal format
builder := ipldbind.Prototype.Message.Representation().NewBuilder()
err = dagcbor.Decode(builder, &buf)
rtnode, err := ipld.DecodeUsingPrototype(byts, dagcbor.Decode, ipldbind.Prototype.Message.Representation())
require.NoError(t, err)
rtnode := builder.Build()
rtigsm := bindnode.Unwrap(rtnode)

// back to message format
Expand Down
2 changes: 1 addition & 1 deletion message/v2/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (mh *MessageHandler) FromMsgReader(_ peer.ID, r msgio.Reader) (message.Grap
return message.GraphSyncMessage{}, err
}

node, err := ipldutil.DecodeNodeInto(msg, ipldbind.Prototype.Message.Representation().NewBuilder())
node, err := ipldutil.DecodeNodeInto(msg, ipldbind.Prototype.Message.Representation())
if err != nil {
return message.GraphSyncMessage{}, err
}
Expand Down

0 comments on commit 36a159c

Please sign in to comment.