From b641b0ff139c9342c4de8a84bafc015422708662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 12 Mar 2021 18:16:28 +0000 Subject: [PATCH] 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. --- multicodec.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/multicodec.go b/multicodec.go index 82bd90f..180328a 100644 --- a/multicodec.go +++ b/multicodec.go @@ -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) + } +} -- GitLab