Commit b8473079 authored by Eric Myhre's avatar Eric Myhre

Encode and Decode switch to verb names; docs.

A similar change to Encode and Decode function names came in
go-ipld-prime v0.9.0, so this keeps this library in sync.

Improved package docs a bit to talk about the new go-ipld-prime
LinkSystem, which is a bit clearer to refer to than before.

Target-of-opportunity fix some typos in docs.
parent e60519b4
......@@ -2,7 +2,7 @@
**An implementation of the IPLD [DAG-PB](https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-pb.md) spec for [go-ipld-prime](https://github.com/ipld/go-ipld-prime/)**
Use `Decoder(ipld.NodeAssembler, io.Reader)` and `Encoder(ipld.Node, io.Writer)` directly, or import this package to have this codec registered into the go-ipld-prime CID link loader.
Use `Decode(ipld.NodeAssembler, io.Reader)` and `Encode(ipld.Node, io.Writer)` directly, or import this package to have this codec registered into the go-ipld-prime CID link loader.
Nodes encoded with this codec _must_ conform to the DAG-PB spec. Specifically, they should have the non-optional fields shown in the DAG-PB schema:
......
......@@ -96,7 +96,7 @@ func runTest(t *testing.T, bytsHex string, expected pbNode) {
roundTrip := func(t *testing.T, node ipld.Node) {
var buf bytes.Buffer
if err := Encoder(node, &buf); err != nil {
if err := Encode(node, &buf); err != nil {
t.Fatal(err)
}
......@@ -108,7 +108,7 @@ func runTest(t *testing.T, bytsHex string, expected pbNode) {
t.Run("basicnode", func(t *testing.T) {
nb := basicnode.Prototype__Map{}.NewBuilder()
err := Decoder(nb, bytes.NewReader(byts))
err := Decode(nb, bytes.NewReader(byts))
if err != nil {
t.Fatal(err)
}
......@@ -120,7 +120,7 @@ func runTest(t *testing.T, bytsHex string, expected pbNode) {
t.Run("typed", func(t *testing.T) {
nb := Type.PBNode.NewBuilder()
err := Decoder(nb, bytes.NewReader(byts))
err := Decode(nb, bytes.NewReader(byts))
if err != nil {
t.Fatal(err)
}
......@@ -180,7 +180,7 @@ func TestNodeWithTwoUnsortedLinks(t *testing.T) {
})
var buf bytes.Buffer
if err := Encoder(node, &buf); err != nil {
if err := Encode(node, &buf); err != nil {
t.Fatal(err)
}
if hex.EncodeToString(buf.Bytes()) != encoded {
......@@ -217,11 +217,11 @@ func TestNodeWithStableSortedLinks(t *testing.T) {
})
var buf bytes.Buffer
if err := Encoder(node, &buf); err != nil {
if err := Encode(node, &buf); err != nil {
t.Fatal(err)
}
nb := basicnode.Prototype__Map{}.NewBuilder()
err := Decoder(nb, bytes.NewReader(buf.Bytes()))
err := Decode(nb, bytes.NewReader(buf.Bytes()))
if err != nil {
t.Fatal(err)
}
......
......@@ -3,8 +3,9 @@ Package dagpb provides an implementation of the IPLD DAG-PB spec
(https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-pb.md) for
go-ipld-prime (https://github.com/ipld/go-ipld-prime/).
Use Decoder() and Encoder() directly, or import this package to have this codec
registered into the go-ipld-prime CID link loader.
Use Decode() and Encode() directly, or import this package to have this codec
registered into the go-ipld-prime multicodec registry and available from the
cidlink.DefaultLinkSystem.
Nodes encoded with this codec _must_ conform to the DAG-PB spec. Specifically,
they should have the non-optional fields shown in the DAG-PB schema:
......@@ -21,7 +22,7 @@ they should have the non-optional fields shown in the DAG-PB schema:
}
Use dagpb.Type.PBNode and friends directly for strictness guarantees. Basic
ipld.Node's will need to have the appropraite fields (and no others) to
ipld.Node's will need to have the appropriate fields (and no others) to
successfully encode using this codec.
*/
package dagpb
......@@ -8,32 +8,32 @@ import (
)
var (
_ ipld.Decoder = Decoder
_ ipld.Encoder = Encoder
_ ipld.Decode = Decode
_ ipld.Encode = Encode
)
func init() {
multicodec.RegisterDecoder(0x70, Decoder)
multicodec.RegisterEncoder(0x70, Encoder)
multicodec.RegisterDecoder(0x70, Decode)
multicodec.RegisterEncoder(0x70, Encode)
}
// Decoder provides an IPLD codec decode interface for DAG-PB data. Provide a
// Decode 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.
// 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.
func Decoder(na ipld.NodeAssembler, r io.Reader) error {
func Decode(na ipld.NodeAssembler, r io.Reader) error {
return Unmarshal(na, r)
}
// Encoder provides an IPLD codec encode interface for DAG-PB data. Provide a
// Encode 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).
// 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.
func Encoder(n ipld.Node, w io.Writer) error {
func Encode(n ipld.Node, w io.Writer) error {
return Marshal(n, w)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment