Skip to content

Commit b508c23

Browse files
committedNov 3, 2015
Merge pull request #1935 from ipfs/fix/nil-data-marshal
set data and links nil if not present
2 parents e90bd93 + 0d35cc9 commit b508c23

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed
 

‎merkledag/coding.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func (n *Node) Marshal() ([]byte, error) {
5050

5151
func (n *Node) getPBNode() *pb.PBNode {
5252
pbn := &pb.PBNode{}
53-
pbn.Links = make([]*pb.PBLink, len(n.Links))
53+
if len(n.Links) > 0 {
54+
pbn.Links = make([]*pb.PBLink, len(n.Links))
55+
}
5456

5557
sort.Stable(LinkSlice(n.Links)) // keep links sorted
5658
for i, l := range n.Links {
@@ -60,7 +62,9 @@ func (n *Node) getPBNode() *pb.PBNode {
6062
pbn.Links[i].Hash = []byte(l.Hash)
6163
}
6264

63-
pbn.Data = n.Data
65+
if len(n.Data) > 0 {
66+
pbn.Data = n.Data
67+
}
6468
return pbn
6569
}
6670

‎merkledag/node.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,15 @@ func (n *Node) GetLinkedNode(ctx context.Context, ds DAGService, name string) (*
176176
// NOTE: does not make copies of Node objects in the links.
177177
func (n *Node) Copy() *Node {
178178
nnode := new(Node)
179-
nnode.Data = make([]byte, len(n.Data))
180-
copy(nnode.Data, n.Data)
179+
if len(n.Data) > 0 {
180+
nnode.Data = make([]byte, len(n.Data))
181+
copy(nnode.Data, n.Data)
182+
}
181183

182-
nnode.Links = make([]*Link, len(n.Links))
183-
copy(nnode.Links, n.Links)
184+
if len(n.Links) > 0 {
185+
nnode.Links = make([]*Link, len(n.Links))
186+
copy(nnode.Links, n.Links)
187+
}
184188
return nnode
185189
}
186190

‎merkledag/utils/utils_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func TestInsertNode(t *testing.T) {
8585
t.Fatal(err)
8686
}
8787

88-
if k.B58String() != "QmThorWojP6YzLJwDukxiYCoKQSwyrMCvdt4WZ6rPm221t" {
89-
t.Fatal("output was different than expected")
88+
if k.B58String() != "QmZ8yeT9uD6ouJPNAYt62XffYuXBT6b4mP4obRSE9cJrSt" {
89+
t.Fatal("output was different than expected: ", k)
9090
}
9191
}
9292

‎test/sharness/t0051-object.sh

+6
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ test_object_cmd() {
234234

235235
test_patch_create_path $EMPTY a/b/b/b/b $FILE
236236

237+
test_expect_success "can create blank object" '
238+
BLANK=$(ipfs object new)
239+
'
240+
241+
test_patch_create_path $BLANK a $FILE
242+
237243
test_expect_success "create bad path fails" '
238244
test_must_fail ipfs object patch --create $EMPTY add-link / $FILE
239245
'

0 commit comments

Comments
 (0)
Please sign in to comment.