github.com/ipld/go-ipld-prime@v0.21.0/codec/dagcbor/multicodec.go (about) 1 package dagcbor 2 3 import ( 4 "io" 5 6 "github.com/ipld/go-ipld-prime/codec" 7 "github.com/ipld/go-ipld-prime/datamodel" 8 "github.com/ipld/go-ipld-prime/multicodec" 9 ) 10 11 var ( 12 _ codec.Decoder = Decode 13 _ codec.Encoder = Encode 14 ) 15 16 func init() { 17 multicodec.RegisterEncoder(0x71, Encode) 18 multicodec.RegisterDecoder(0x71, Decode) 19 } 20 21 // Decode deserializes data from the given io.Reader and feeds it into the given datamodel.NodeAssembler. 22 // Decode fits the codec.Decoder function interface. 23 // 24 // A similar function is available on DecodeOptions type if you would like to customize any of the decoding details. 25 // This function uses the defaults for the dag-cbor codec 26 // (meaning: links (indicated by tag 42) are decoded). 27 // 28 // This is the function that will be registered in the default multicodec registry during package init time. 29 func Decode(na datamodel.NodeAssembler, r io.Reader) error { 30 return DecodeOptions{ 31 AllowLinks: true, 32 }.Decode(na, r) 33 } 34 35 // Encode walks the given datamodel.Node and serializes it to the given io.Writer. 36 // Encode fits the codec.Encoder function interface. 37 // 38 // A similar function is available on EncodeOptions type if you would like to customize any of the encoding details. 39 // This function uses the defaults for the dag-cbor codec 40 // (meaning: links are encoded, and map keys are sorted (with RFC7049 ordering!) during encode). 41 // 42 // This is the function that will be registered in the default multicodec registry during package init time. 43 func Encode(n datamodel.Node, w io.Writer) error { 44 return EncodeOptions{ 45 AllowLinks: true, 46 MapSortMode: codec.MapSortMode_RFC7049, 47 }.Encode(n, w) 48 }