File tree 4 files changed +72
-1
lines changed
4 files changed +72
-1
lines changed Original file line number Diff line number Diff line change @@ -60,7 +60,9 @@ func (n *ProtoNode) getPBNode() *pb.PBNode {
60
60
pbn .Links [i ] = & pb.PBLink {}
61
61
pbn .Links [i ].Name = & l .Name
62
62
pbn .Links [i ].Tsize = & l .Size
63
- pbn .Links [i ].Hash = l .Cid .Bytes ()
63
+ if l .Cid != nil {
64
+ pbn .Links [i ].Hash = l .Cid .Bytes ()
65
+ }
64
66
}
65
67
66
68
if len (n .data ) > 0 {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package merkledag
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
7
8
node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
@@ -228,6 +229,31 @@ func (n *ProtoNode) Loggable() map[string]interface{} {
228
229
}
229
230
}
230
231
232
+ func (n * ProtoNode ) UnmarshalJSON (b []byte ) error {
233
+ s := struct {
234
+ Data []byte `json:"data"`
235
+ Links []* node.Link `json:"links"`
236
+ }{}
237
+
238
+ err := json .Unmarshal (b , & s )
239
+ if err != nil {
240
+ return err
241
+ }
242
+
243
+ n .data = s .Data
244
+ n .links = s .Links
245
+ return nil
246
+ }
247
+
248
+ func (n * ProtoNode ) MarshalJSON () ([]byte , error ) {
249
+ out := map [string ]interface {}{
250
+ "data" : n .data ,
251
+ "links" : n .links ,
252
+ }
253
+
254
+ return json .Marshal (out )
255
+ }
256
+
231
257
func (n * ProtoNode ) Cid () * cid.Cid {
232
258
if n .encoded != nil && n .cached != nil {
233
259
return n .cached
Original file line number Diff line number Diff line change 1
1
package merkledag_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"testing"
6
7
@@ -128,3 +129,32 @@ func TestNodeCopy(t *testing.T) {
128
129
t .Fatal ("should be different objects" )
129
130
}
130
131
}
132
+
133
+ func TestJsonRoundtrip (t * testing.T ) {
134
+ nd := new (ProtoNode )
135
+ nd .SetLinks ([]* node.Link {
136
+ {Name : "a" },
137
+ {Name : "c" },
138
+ {Name : "b" },
139
+ })
140
+ nd .SetData ([]byte ("testing" ))
141
+
142
+ jb , err := nd .MarshalJSON ()
143
+ if err != nil {
144
+ t .Fatal (err )
145
+ }
146
+
147
+ nn := new (ProtoNode )
148
+ err = nn .UnmarshalJSON (jb )
149
+ if err != nil {
150
+ t .Fatal (err )
151
+ }
152
+
153
+ if ! bytes .Equal (nn .Data (), nd .Data ()) {
154
+ t .Fatal ("data wasnt the same" )
155
+ }
156
+
157
+ if ! nn .Cid ().Equals (nd .Cid ()) {
158
+ t .Fatal ("objects differed after marshaling" )
159
+ }
160
+ }
Original file line number Diff line number Diff line change @@ -46,6 +46,19 @@ test_dag_cmd() {
46
46
test_cmp file2 out2 &&
47
47
test_cmp file3 out3
48
48
'
49
+
50
+ test_expect_success " add a normal file" '
51
+ HASH=$(echo "foobar" | ipfs add -q)
52
+ '
53
+
54
+ test_expect_success " can view protobuf object with dag get" '
55
+ ipfs dag get $HASH > dag_get_pb_out
56
+ '
57
+
58
+ test_expect_success " output looks correct" '
59
+ echo "{\"data\":\"CAISB2Zvb2JhcgoYBw==\",\"links\":[]}" > dag_get_pb_exp &&
60
+ test_cmp dag_get_pb_exp dag_get_pb_out
61
+ '
49
62
}
50
63
51
64
# should work offline
You can’t perform that action at this time.
0 commit comments