Commit b641b0ff authored by Daniel Martí's avatar Daniel Martí

add a prototype chooser API for integrations

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.
parent 748624f5
......@@ -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 (
......@@ -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)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment