Skip to content

Commit

Permalink
Merge pull request ipfs/go-merkledag#36 from ipfs/fix/go-ipfs-6076
Browse files Browse the repository at this point in the history
add function to marshal raw nodes to json

This commit was moved from ipfs/go-merkledag@fe3c46b
  • Loading branch information
Stebalien committed Mar 13, 2019
2 parents 6e836c6 + f609ab5 commit f8819a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ipld/merkledag/merkledag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package merkledag_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -640,6 +641,27 @@ func TestCidRawDoesnNeedData(t *testing.T) {
}
}

func TestRawToJson(t *testing.T) {
rawData := []byte{1, 2, 3, 4}
nd := NewRawNode(rawData)
encoded, err := nd.MarshalJSON()
if err != nil {
t.Fatal(err)
}
var res interface{}
err = json.Unmarshal(encoded, &res)
if err != nil {
t.Fatal(err)
}
resBytes, ok := res.(string)
if !ok {
t.Fatal("expected to marshal to a string")
}
if string(rawData) != resBytes {
t.Fatal("failed to round-trip bytes")
}
}

func TestGetManyDuplicate(t *testing.T) {
ctx := context.Background()

Expand Down
6 changes: 6 additions & 0 deletions ipld/merkledag/raw.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package merkledag

import (
"encoding/json"
"fmt"
"github.com/ipfs/go-block-format"

Expand Down Expand Up @@ -94,4 +95,9 @@ func (rn *RawNode) Stat() (*ipld.NodeStat, error) {
}, nil
}

// MarshalJSON is required for our "ipfs dag" commands.
func (rn *RawNode) MarshalJSON() ([]byte, error) {
return json.Marshal(string(rn.RawData()))
}

var _ ipld.Node = (*RawNode)(nil)

0 comments on commit f8819a5

Please sign in to comment.