Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error when trying to encode an empty link #52

Merged
merged 3 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,5 +534,8 @@ func castBytesToCid(x []byte) (cid.Cid, error) {
}

func castCidToBytes(link cid.Cid) ([]byte, error) {
if !link.Defined() {
return nil, ErrEmptyLink
}
return append([]byte{0}, link.Bytes()...), nil
}
25 changes: 21 additions & 4 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func TestBasicMarshal(t *testing.T) {
"name": "foo",
"bar": c,
}
fmt.Printf("cid: %s\n", c.String())
nd, err := WrapObject(obj, mh.SHA2_256, -1)
if err != nil {
t.Fatal(err)
Expand All @@ -103,9 +102,6 @@ func TestBasicMarshal(t *testing.T) {
t.Fatal(err)
}

fmt.Printf("before %v\n", nd.RawData())
fmt.Printf("after %v\n", back.RawData())

if err := assertCid(back.Cid(), "zdpuApUZEHofKXuTs2Yv2CLBeiASQrc9FojFLSZWcyZq6dZhb"); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -529,6 +525,27 @@ func TestCidAndBigInt(t *testing.T) {
}
}

func TestEmptyCid(t *testing.T) {
type Foo struct {
A cid.Cid
}
type Bar struct {
A cid.Cid `refmt:",omitempty"`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semantics without omitempty are surprising. I get that omitempty skips encoding zero values. But without omitempty you can't encode a zero value? What's the intent of semantics like that and what if I really, truly want an empty value?

}
RegisterCborType(Foo{})
RegisterCborType(Bar{})

_, err := WrapObject(&Foo{}, mh.SHA2_256, -1)
if err == nil {
t.Fatal("should have failed to encode an object with an empty but non-omitted CID")
}

_, err = WrapObject(&Bar{}, mh.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}
}

func TestCanonicalStructEncoding(t *testing.T) {
type Foo struct {
Zebra string
Expand Down