Skip to content

Commit

Permalink
add a prototype chooser API for integrations
Browse files Browse the repository at this point in the history
At least go-merkledag and go-graphsync still need a prototype chooser,
and go-ipld-prime-proto provides one, so this simplifies integration
with downstreams.

It's worth noting that we just add PBNode here, while
go-ipld-prime-proto also added RawNode. This is because this dag-pb
module leaves raw nodes out, since those are now in
go-ipld-prime/codec/raw.

The logic is just three lines, but it's still easier to not have to
copy-paste these between different projects.
  • Loading branch information
mvdan committed Mar 16, 2021
1 parent 748624f commit b641b0f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions multicodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"io"

ipld "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/multicodec"
"github.com/ipld/go-ipld-prime/traversal"
)

var (
Expand Down Expand Up @@ -37,3 +39,14 @@ func Decode(na ipld.NodeAssembler, r io.Reader) error {
func Encode(n ipld.Node, w io.Writer) error {
return Marshal(n, w)
}

// AddSupportToChooser takes an existing node prototype chooser and subs in
// PBNode for the dag-pb multicodec code.
func AddSupportToChooser(existing traversal.LinkTargetNodePrototypeChooser) traversal.LinkTargetNodePrototypeChooser {
return func(lnk ipld.Link, lnkCtx ipld.LinkContext) (ipld.NodePrototype, error) {
if lnk, ok := lnk.(cidlink.Link); ok && lnk.Cid.Prefix().Codec == 0x70 {
return Type.PBNode, nil
}
return existing(lnk, lnkCtx)
}
}

0 comments on commit b641b0f

Please sign in to comment.