Skip to content

Commit

Permalink
fixup! fix(codecs): coerce cid.Undef to null in dag{json,cbor} output
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed May 25, 2022
1 parent c09cd98 commit 99c91d9
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 31 deletions.
8 changes: 2 additions & 6 deletions codec/dagcbor/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()...)
Expand Down
18 changes: 2 additions & 16 deletions codec/dagcbor/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dagcbor

import (
"bytes"
"encoding/hex"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -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")
}
8 changes: 2 additions & 6 deletions codec/dagjson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions codec/dagjson/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 99c91d9

Please sign in to comment.