github.com/ipld/go-ipld-prime@v0.21.0/codec/cbor/multicodec.go (about)

     1  package cbor
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/ipld/go-ipld-prime/codec"
     7  	"github.com/ipld/go-ipld-prime/codec/dagcbor"
     8  	"github.com/ipld/go-ipld-prime/datamodel"
     9  	"github.com/ipld/go-ipld-prime/multicodec"
    10  )
    11  
    12  var (
    13  	_ codec.Decoder = Decode
    14  	_ codec.Encoder = Encode
    15  )
    16  
    17  func init() {
    18  	multicodec.RegisterEncoder(0x51, Encode)
    19  	multicodec.RegisterDecoder(0x51, Decode)
    20  }
    21  
    22  // Decode deserializes data from the given io.Reader and feeds it into the given datamodel.NodeAssembler.
    23  // Decode fits the codec.Decoder function interface.
    24  //
    25  // This is the function that will be registered in the default multicodec registry during package init time.
    26  func Decode(na datamodel.NodeAssembler, r io.Reader) error {
    27  	return dagcbor.DecodeOptions{
    28  		AllowLinks: false,
    29  	}.Decode(na, r)
    30  }
    31  
    32  // Encode walks the given datamodel.Node and serializes it to the given io.Writer.
    33  // Encode fits the codec.Encoder function interface.
    34  //
    35  // This is the function that will be registered in the default multicodec registry during package init time.
    36  func Encode(n datamodel.Node, w io.Writer) error {
    37  	return dagcbor.EncodeOptions{
    38  		AllowLinks: false,
    39  	}.Encode(n, w)
    40  }