multicodec.go 1.3 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.Decode = Decode
	_ ipld.Encode = Encode
Rod Vagg's avatar
Rod Vagg committed
13 14 15
)

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

20
// Decode provides an IPLD codec decode interface for DAG-PB data. Provide a
Rod Vagg's avatar
Rod Vagg committed
21
// 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.
26
func Decode(na ipld.NodeAssembler, r io.Reader) error {
Rod Vagg's avatar
Rod Vagg committed
27 28 29
	return Unmarshal(na, r)
}

30
// Encode provides an IPLD codec encode interface for DAG-PB data. Provide a
Rod Vagg's avatar
Rod Vagg committed
31 32 33
// 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.
37
func Encode(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
}