@cryptotaxi247 / kubo / commits / 0d35cc9ae

set data and links nil if not present

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Nov 2, 2015 at 23:19 UTC 0d35cc9aeaba9880028a60b6e6d57c750fc9fb3d
4 files changed +22 -8
merkledag/coding.go
+6 -2
@@ -50,7 +50,9 @@ func (n *Node) Marshal() ([]byte, error) {
50
51 func (n *Node) getPBNode() *pb.PBNode {
52 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 + }
56
57 sort.Stable(LinkSlice(n.Links)) // keep links sorted
58 for i, l := range n.Links {
@@ -60,7 +62,9 @@ func (n *Node) getPBNode() *pb.PBNode {
62 pbn.Links[i].Hash = []byte(l.Hash)
63 }
64
63 - pbn.Data = n.Data
65 + if len(n.Data) > 0 {
66 + pbn.Data = n.Data
67 + }
68 return pbn
69 }
70
merkledag/node.go
+8 -4
@@ -176,11 +176,15 @@ func (n *Node) GetLinkedNode(ctx context.Context, ds DAGService, name string) (*
176 // NOTE: does not make copies of Node objects in the links.
177 func (n *Node) Copy() *Node {
178 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 + }
183
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 + }
188 return nnode
189 }
190
merkledag/utils/utils_test.go
+2 -2
@@ -85,8 +85,8 @@ func TestInsertNode(t *testing.T) {
85 t.Fatal(err)
86 }
87
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)
90 }
91 }
92
test/sharness/t0051-object.sh
+6
@@ -234,6 +234,12 @@ test_object_cmd() {
234
235 test_patch_create_path $EMPTY a/b/b/b/b $FILE
236
237 + test_expect_success "can create blank object" '
238 + BLANK=$(ipfs object new)
239 + '
240 +
241 + test_patch_create_path $BLANK a $FILE
242 +
243 test_expect_success "create bad path fails" '
244 test_must_fail ipfs object patch --create $EMPTY add-link / $FILE
245 '