From 99c91d9bebf6de909a7d4c51838548fc5a7a7b19 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Wed, 25 May 2022 12:59:15 +1000 Subject: [PATCH] fixup! fix(codecs): coerce cid.Undef to null in dag{json,cbor} output --- codec/dagcbor/marshal.go | 8 ++------ codec/dagcbor/marshal_test.go | 18 ++---------------- codec/dagjson/marshal.go | 8 ++------ codec/dagjson/marshal_test.go | 5 ++--- 4 files changed, 8 insertions(+), 31 deletions(-) diff --git a/codec/dagcbor/marshal.go b/codec/dagcbor/marshal.go index 328d6dcb..608f8a3e 100644 --- a/codec/dagcbor/marshal.go +++ b/codec/dagcbor/marshal.go @@ -5,7 +5,6 @@ import ( "io" "sort" - "github.com/ipfs/go-cid" "github.com/polydawn/refmt/cbor" "github.com/polydawn/refmt/shared" "github.com/polydawn/refmt/tok" @@ -145,11 +144,8 @@ func marshal(n datamodel.Node, tk *tok.Token, sink shared.TokenSink, options Enc } switch lnk := v.(type) { case cidlink.Link: - if lnk.Cid == cid.Undef { - // Undef is akin to null, so coerce it rather than produce an invalid output - tk.Type = tok.TNull - _, err := sink.Step(tk) - return err + if !lnk.Cid.Defined() { + return fmt.Errorf("encoding undefined CIDs are not supported by this codec") } tk.Type = tok.TBytes tk.Bytes = append([]byte{0}, lnk.Bytes()...) diff --git a/codec/dagcbor/marshal_test.go b/codec/dagcbor/marshal_test.go index 53341948..7fc87f6b 100644 --- a/codec/dagcbor/marshal_test.go +++ b/codec/dagcbor/marshal_test.go @@ -2,7 +2,6 @@ package dagcbor import ( "bytes" - "encoding/hex" "math/rand" "testing" "time" @@ -82,19 +81,6 @@ func TestMarshalUndefCid(t *testing.T) { qp.MapEntry(ma, "DefCid", qp.Link(cidlink.Link{Cid: link})) }) qt.Assert(t, err, qt.IsNil) - byts, err := ipld.Encode(node, Encode) - qt.Assert(t, err, qt.IsNil) - /* this is what we're expecting: - a2 # map(2) - 66 # string(6) - 446566436964 # "DefCid" - d8 2a # tag(42) - 58 25 # bytes(37) - 0001701220c3c4733ec8affd06cf9e9ff50ffc6bcd # "\x00\x01p\x12 ÃÄs>ȯý\x06Ï\x9e\x9fõ\x0fükÍ" - 2ec85a6170004bb709669c31de94391a # ".ÈZap\x00K·\x09f\x9c1Þ\x949\x1a" - 68 # string(8) - 556e646566436964 # "UndefCid" - f6 # null - */ - qt.Assert(t, hex.EncodeToString(byts), qt.Equals, "a266446566436964d82a58250001701220c3c4733ec8affd06cf9e9ff50ffc6bcd2ec85a6170004bb709669c31de94391a68556e646566436964f6") + _, err = ipld.Encode(node, Encode) + qt.Assert(t, err, qt.ErrorMatches, "encoding undefined CIDs are not supported by this codec") } diff --git a/codec/dagjson/marshal.go b/codec/dagjson/marshal.go index 467ac46d..8e1bba2a 100644 --- a/codec/dagjson/marshal.go +++ b/codec/dagjson/marshal.go @@ -6,7 +6,6 @@ import ( "io" "sort" - "github.com/ipfs/go-cid" "github.com/polydawn/refmt/json" "github.com/polydawn/refmt/shared" "github.com/polydawn/refmt/tok" @@ -258,11 +257,8 @@ func Marshal(n datamodel.Node, sink shared.TokenSink, options EncodeOptions) err } switch lnk := v.(type) { case cidlink.Link: - if lnk.Cid == cid.Undef { - // Undef is akin to null, so coerce it rather than produce an invalid output - tk.Type = tok.TNull - _, err := sink.Step(&tk) - return err + if !lnk.Cid.Defined() { + return fmt.Errorf("encoding undefined CIDs are not supported by this codec") } // Precisely four tokens to emit: tk.Type = tok.TMapOpen diff --git a/codec/dagjson/marshal_test.go b/codec/dagjson/marshal_test.go index 17f0e691..542d2986 100644 --- a/codec/dagjson/marshal_test.go +++ b/codec/dagjson/marshal_test.go @@ -20,7 +20,6 @@ func TestMarshalUndefCid(t *testing.T) { qp.MapEntry(ma, "DefCid", qp.Link(cidlink.Link{Cid: link})) }) qt.Assert(t, err, qt.IsNil) - byts, err := ipld.Encode(node, Encode) - qt.Assert(t, err, qt.IsNil) - qt.Assert(t, string(byts), qt.Equals, `{"DefCid":{"/":"bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi"},"UndefCid":null}`) + _, err = ipld.Encode(node, Encode) + qt.Assert(t, err, qt.ErrorMatches, "encoding undefined CIDs are not supported by this codec") }