Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Update to latest ipld before rework #2

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
merkledag_pb "github.com/ipfs/go-merkledag/pb"
ipld "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/schema"
)

// DecodeDagProto is a fast path decoding to protobuf
Expand All @@ -30,43 +31,59 @@ func (nb _PBNode__NodeBuilder) DecodeDagProto(r io.Reader) (ipld.Node, error) {
return nil, fmt.Errorf("unmarshal failed. %v", err)
}
pbLinks = append(pbLinks, PBLink{
&Link{cidlink.Link{Cid: hash}},
&String{link.GetName()},
&Int{int(link.GetTsize())},
d: PBLink__Content{
Hash: MaybeLink{
Maybe: schema.Maybe_Value,
Value: Link{cidlink.Link{Cid: hash}},
},
Name: MaybeString{
Maybe: schema.Maybe_Value,
Value: String{link.GetName()},
},
Tsize: MaybeInt{
Maybe: schema.Maybe_Value,
Value: Int{int(link.GetTsize())},
},
},
})
}
pbData := Bytes{pbn.GetData()}
return PBNode{PBLinks{pbLinks}, pbData}, nil
return PBNode{d: PBNode__Content{
Links: PBLinks{x: pbLinks},
Data: pbData,
},
}, nil
}

// EncodeDagProto is a fast path encoding to protobuf
// for PBNode types
func (nd PBNode) EncodeDagProto(w io.Writer) error {
pbn := new(merkledag_pb.PBNode)
pbn.Links = make([]*merkledag_pb.PBLink, 0, len(nd.Links.x))
for _, link := range nd.Links.x {
pbn.Links = make([]*merkledag_pb.PBLink, 0, len(nd.d.Links.x))
for _, link := range nd.d.Links.x {
var hash []byte
if link.Hash != nil {
cid := link.Hash.x.(cidlink.Link).Cid
if link.d.Hash.Maybe == schema.Maybe_Value {
cid := link.d.Hash.Value.x.(cidlink.Link).Cid
if cid.Defined() {
hash = cid.Bytes()
}
}
var name *string
if link.Name != nil {
name = &link.Name.x
if link.d.Name.Maybe == schema.Maybe_Value {
tmp := link.d.Name.Value.x
name = &tmp
}
var tsize *uint64
if link.Tsize != nil {
tmp := uint64(link.Tsize.x)
if link.d.Tsize.Maybe == schema.Maybe_Value {
tmp := uint64(link.d.Tsize.Value.x)
tsize = &tmp
}
pbn.Links = append(pbn.Links, &merkledag_pb.PBLink{
Hash: hash,
Name: name,
Tsize: tsize})
}
pbn.Data = nd.Data.x
pbn.Data = nd.d.Data.x
data, err := pbn.Marshal()
if err != nil {
return fmt.Errorf("marshal failed. %v", err)
Expand Down
24 changes: 12 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ go 1.13

require (
github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-blockservice v0.1.0
github.com/ipfs/go-cid v0.0.3
github.com/ipfs/go-datastore v0.0.5
github.com/ipfs/go-ipfs-blockstore v0.0.1
github.com/ipfs/go-ipfs-chunker v0.0.1
github.com/ipfs/go-blockservice v0.1.3
github.com/ipfs/go-cid v0.0.5
github.com/ipfs/go-datastore v0.4.4
github.com/ipfs/go-ipfs-blockstore v0.1.4
github.com/ipfs/go-ipfs-chunker v0.0.5
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-files v0.0.4
github.com/ipfs/go-ipld-format v0.0.2
github.com/ipfs/go-merkledag v0.2.3
github.com/ipfs/go-unixfs v0.2.2-0.20190827150610-868af2e9e5cb
github.com/ipld/go-ipld-prime v0.0.2-0.20191108012745-28a82f04c785
github.com/ipfs/go-ipfs-files v0.0.8
github.com/ipfs/go-ipld-format v0.2.0
github.com/ipfs/go-merkledag v0.3.1
github.com/ipfs/go-unixfs v0.2.4
github.com/ipld/go-ipld-prime v0.0.2-0.20200229094926-eb71617f4aeb
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/multiformats/go-multihash v0.0.5
github.com/warpfork/go-wish v0.0.0-20190328234359-8b3e70f8e830
github.com/multiformats/go-multihash v0.0.13
github.com/warpfork/go-wish v0.0.0-20200122115046-b9ea61034e4a
)
74 changes: 68 additions & 6 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions node_builder_chooser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
// AddDagPBSupportToChooser takes an existing NodeBuilderChooser and subs in
// Protobuf and Raw node builders where neccesary
func AddDagPBSupportToChooser(existing traversal.NodeBuilderChooser) traversal.NodeBuilderChooser {
return func(lnk ipld.Link, lnkCtx ipld.LinkContext) ipld.NodeBuilder {
return func(lnk ipld.Link, lnkCtx ipld.LinkContext) (ipld.NodeBuilder, error) {
c, ok := lnk.(cidlink.Link)
if !ok {
return existing(lnk, lnkCtx)
}
switch c.Cid.Prefix().Codec {
case 0x70:
return PBNode__NodeBuilder()
return PBNode__NodeBuilder(), nil
case 0x55:
return RawNode__NodeBuilder()
return RawNode__NodeBuilder(), nil
default:
return existing(lnk, lnkCtx)
}
Expand Down
32 changes: 22 additions & 10 deletions node_builder_chooser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
func TestNodeBuilderChooser(t *testing.T) {
nb1 := ipldfree.NodeBuilder()
nb2 := dagpb.String__NodeBuilder()
var nb1Chooser traversal.NodeBuilderChooser = dagpb.AddDagPBSupportToChooser(func(ipld.Link, ipld.LinkContext) ipld.NodeBuilder {
return nb1
var nb1Chooser traversal.NodeBuilderChooser = dagpb.AddDagPBSupportToChooser(func(ipld.Link, ipld.LinkContext) (ipld.NodeBuilder, error) {
return nb1, nil
})
var nb2Chooser traversal.NodeBuilderChooser = dagpb.AddDagPBSupportToChooser(func(ipld.Link, ipld.LinkContext) ipld.NodeBuilder {
return nb2
var nb2Chooser traversal.NodeBuilderChooser = dagpb.AddDagPBSupportToChooser(func(ipld.Link, ipld.LinkContext) (ipld.NodeBuilder, error) {
return nb2, nil
})
bytes := randomBytes(256)
protoPrefix := merkledag.V1CidPrefix()
Expand All @@ -48,11 +48,23 @@ func TestNodeBuilderChooser(t *testing.T) {
rawLink := cidlink.Link{Cid: rawCid}
cborLink := cidlink.Link{Cid: cborCid}

Wish(t, nb1Chooser(protoLink, ipld.LinkContext{}), ShouldEqual, dagpb.PBNode__NodeBuilder())
Wish(t, nb1Chooser(rawLink, ipld.LinkContext{}), ShouldEqual, dagpb.RawNode__NodeBuilder())
Wish(t, nb1Chooser(cborLink, ipld.LinkContext{}), ShouldEqual, nb1)
Wish(t, nb2Chooser(protoLink, ipld.LinkContext{}), ShouldEqual, dagpb.PBNode__NodeBuilder())
Wish(t, nb2Chooser(rawLink, ipld.LinkContext{}), ShouldEqual, dagpb.RawNode__NodeBuilder())
Wish(t, nb2Chooser(cborLink, ipld.LinkContext{}), ShouldEqual, nb2)
nb, err := nb1Chooser(protoLink, ipld.LinkContext{})
Wish(t, err, ShouldEqual, nil)
Wish(t, nb, ShouldEqual, dagpb.PBNode__NodeBuilder())
nb, err = nb1Chooser(rawLink, ipld.LinkContext{})
Wish(t, err, ShouldEqual, nil)
Wish(t, nb, ShouldEqual, dagpb.RawNode__NodeBuilder())
nb, err = nb1Chooser(cborLink, ipld.LinkContext{})
Wish(t, err, ShouldEqual, nil)
Wish(t, nb, ShouldEqual, nb1)
nb, err = nb2Chooser(protoLink, ipld.LinkContext{})
Wish(t, err, ShouldEqual, nil)
Wish(t, nb, ShouldEqual, dagpb.PBNode__NodeBuilder())
nb, err = nb2Chooser(rawLink, ipld.LinkContext{})
Wish(t, err, ShouldEqual, nil)
Wish(t, nb, ShouldEqual, dagpb.RawNode__NodeBuilder())
nb, err = nb2Chooser(cborLink, ipld.LinkContext{})
Wish(t, err, ShouldEqual, nil)
Wish(t, nb, ShouldEqual, nb2)

}
Loading