From b8473079d0cdc9d6b4d8f386524ed54312cb7cda Mon Sep 17 00:00:00 2001 From: Eric Myhre Date: Mon, 15 Mar 2021 19:34:26 +0100 Subject: [PATCH] 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. --- README.md | 2 +- basics_test.go | 12 ++++++------ doc.go | 7 ++++--- multicodec.go | 16 ++++++++-------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 762922d..c481214 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/basics_test.go b/basics_test.go index 27b59c7..06bad95 100644 --- a/basics_test.go +++ b/basics_test.go @@ -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) } diff --git a/doc.go b/doc.go index 68663b6..fda961f 100644 --- a/doc.go +++ b/doc.go @@ -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 diff --git a/multicodec.go b/multicodec.go index a6079a9..73839d5 100644 --- a/multicodec.go +++ b/multicodec.go @@ -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) } -- GitLab