multicodec.go 1.31 KB
Newer Older
Rod Vagg's avatar
Rod Vagg committed
1 2 3 4 5 6
package dagpb

import (
	"io"

	ipld "github.com/ipld/go-ipld-prime"
7
	"github.com/ipld/go-ipld-prime/multicodec"
Rod Vagg's avatar
Rod Vagg committed
8 9 10
)

var (
11 12
	_ ipld.Decoder = Decoder
	_ ipld.Encoder = Encoder
Rod Vagg's avatar
Rod Vagg committed
13 14 15
)

func init() {
acruikshank's avatar
acruikshank committed
16 17
	multicodec.RegisterDecoder(0x70, Decoder)
	multicodec.RegisterEncoder(0x70, Encoder)
Rod Vagg's avatar
Rod Vagg committed
18 19
}

Rod Vagg's avatar
Rod Vagg committed
20 21
// Decoder provides an IPLD codec decode interface for DAG-PB data. Provide a
// compatible NodeAssembler and a byte source to unmarshal a DAG-PB IPLD Node.
Rod Vagg's avatar
Rod Vagg committed
22 23 24 25
// Use the NodeAssembler from the PBNode type for safest construction
// (Type.PBNode.NewBuilder()). A Map assembler will also work.
// This function is registered via the go-ipld-prime link loader for multicodec
// code 0x70 when this package is invoked via init.
Rod Vagg's avatar
Rod Vagg committed
26 27 28 29
func Decoder(na ipld.NodeAssembler, r io.Reader) error {
	return Unmarshal(na, r)
}

Rod Vagg's avatar
Rod Vagg committed
30 31 32 33
// Encoder provides an IPLD codec encode interface for DAG-PB data. Provide a
// conforming Node and a destination for bytes to marshal a DAG-PB IPLD Node.
// The Node must strictly conform to the DAG-PB schema
// (https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-pb.md).
Rod Vagg's avatar
Rod Vagg committed
34 35 36
// For safest use, build Nodes using the Type.PBNode type.
// This function is registered via the go-ipld-prime link loader for multicodec
// code 0x70 when this package is invoked via init.
Rod Vagg's avatar
Rod Vagg committed
37
func Encoder(n ipld.Node, w io.Writer) error {
Rod Vagg's avatar
Rod Vagg committed
38
	return Marshal(n, w)
Rod Vagg's avatar
Rod Vagg committed
39
}