Skip to content
This repository has been archived by the owner on Aug 9, 2018. It is now read-only.

Commit

Permalink
Rename packages
Browse files Browse the repository at this point in the history
Move ipld.Node in a memory package (memory.Node) and reader package at the
ipld root as it provides common interfaces for ipld.

# Conflicts:
#	coding/coding.go
#	coding/coding_test.go
#	coding/pb/pbcodec.go
  • Loading branch information
mildred committed Jan 22, 2016
1 parent 1c37932 commit 2a18296
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 88 deletions.
2 changes: 1 addition & 1 deletion reader/base_reader.go → base_reader.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reader
package ipld

type BaseReader struct {
Callback ReadFun
Expand Down
4 changes: 2 additions & 2 deletions coding/cbor_stream.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ipfsld
package coding

import (
"fmt"
"io"
"math/big"

reader "github.com/ipfs/go-ipld/reader"
reader "github.com/ipfs/go-ipld"
cbor "github.com/whyrusleeping/cbor/go"
)

Expand Down
12 changes: 6 additions & 6 deletions coding/coding.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ipfsld
package coding

import (
"fmt"
Expand All @@ -9,9 +9,9 @@ import (
mcjson "github.com/jbenet/go-multicodec/json"
mcmux "github.com/jbenet/go-multicodec/mux"

ipld "github.com/ipfs/go-ipld"
reader "github.com/ipfs/go-ipld"
pb "github.com/ipfs/go-ipld/coding/pb"
reader "github.com/ipfs/go-ipld/reader"
memory "github.com/ipfs/go-ipld/memory"
)

var Header []byte
Expand Down Expand Up @@ -61,7 +61,7 @@ func Multicodec() mc.Multicodec {
}

func selectCodec(v interface{}, codecs []mc.Multicodec) mc.Multicodec {
vn, ok := v.(*ipld.Node)
vn, ok := v.(*memory.Node)
if !ok {
return nil
}
Expand All @@ -80,8 +80,8 @@ func selectCodec(v interface{}, codecs []mc.Multicodec) mc.Multicodec {
return nil // no codec
}

func codecKey(n ipld.Node) (string, error) {
chdr, ok := (n)[ipld.CodecKey]
func codecKey(n memory.Node) (string, error) {
chdr, ok := (n)[memory.CodecKey]
if !ok {
// if no codec is defined, use our default codec
chdr = defaultCodec
Expand Down
45 changes: 21 additions & 24 deletions coding/coding_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ipfsld
package coding

import (
"bytes"
"io/ioutil"
"reflect"
"testing"

ipld "github.com/ipfs/go-ipld"
reader "github.com/ipfs/go-ipld/reader"
readertest "github.com/ipfs/go-ipld/reader/test"
reader "github.com/ipfs/go-ipld"
memory "github.com/ipfs/go-ipld/memory"
readertest "github.com/ipfs/go-ipld/test"

mc "github.com/jbenet/go-multicodec"
mctest "github.com/jbenet/go-multicodec/test"
Expand All @@ -32,8 +32,8 @@ func init() {

type TC struct {
cbor []byte
src ipld.Node
links map[string]ipld.Link
src memory.Node
links map[string]memory.Link
typ string
ctx interface{}
}
Expand All @@ -43,15 +43,15 @@ var testCases []TC
func init() {
testCases = append(testCases, TC{
[]byte{},
ipld.Node{
memory.Node{
"foo": "bar",
"bar": []int{1, 2, 3},
"baz": ipld.Node{
"baz": memory.Node{
"@type": "mlink",
"hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo",
},
},
map[string]ipld.Link{
map[string]memory.Link{
"baz": {"@type": "mlink", "hash": ("QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo")},
},
"",
Expand All @@ -60,30 +60,30 @@ func init() {

testCases = append(testCases, TC{
[]byte{},
ipld.Node{
memory.Node{
"foo": "bar",
"@type": "commit",
"@context": "/ipfs/QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo/mdag",
"baz": ipld.Node{
"baz": memory.Node{
"@type": "mlink",
"hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo",
},
"bazz": ipld.Node{
"bazz": memory.Node{
"@type": "mlink",
"hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo",
},
"bar": ipld.Node{
"bar": memory.Node{
"@type": "mlinkoo",
"hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo",
},
"bar2": ipld.Node{
"foo": ipld.Node{
"bar2": memory.Node{
"foo": memory.Node{
"@type": "mlink",
"hash": "QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo",
},
},
},
map[string]ipld.Link{
map[string]memory.Link{
"baz": {"@type": "mlink", "hash": ("QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo")},
"bazz": {"@type": "mlink", "hash": ("QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo")},
"bar2/foo": {"@type": "mlink", "hash": ("QmZku7P7KeeHAnwMr6c4HveYfMzmtVinNXzibkiNbfDbPo")},
Expand All @@ -104,15 +104,15 @@ func TestHeaderMC(t *testing.T) {
func TestRoundtripBasicMC(t *testing.T) {
codec := Multicodec()
for _, tca := range testCases {
var tcb ipld.Node
var tcb memory.Node
mctest.RoundTripTest(t, codec, &(tca.src), &tcb)
}
}

// Test decoding and encoding a json and cbor file
func TestCodecsDecodeEncode(t *testing.T) {
for fname, testfile := range codedFiles {
var n ipld.Node
var n memory.Node
codec := Multicodec()

if err := mc.Unmarshal(codec, testfile, &n); err != nil {
Expand All @@ -121,12 +121,12 @@ func TestCodecsDecodeEncode(t *testing.T) {
continue
}

linksExpected := map[string]ipld.Link{
"abc": ipld.Link{
linksExpected := map[string]memory.Link{
"abc": memory.Link{
"mlink": "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V",
},
}
linksActual := ipld.Links(n)
linksActual := memory.Links(n)
if !reflect.DeepEqual(linksExpected, linksActual) {
t.Logf("Expected: %#v", linksExpected)
t.Logf("Actual: %#v", linksActual)
Expand Down Expand Up @@ -187,7 +187,4 @@ func TestCborStream(t *testing.T) {
readertest.Callback{[]interface{}{"@codec"}, reader.TokenValue, "/json"},
readertest.Callback{[]interface{}{}, reader.TokenEndNode, nil},
})
}
readertest.Callback{[]interface{}{}, reader.TokenEndNode, nil},
})
}
4 changes: 2 additions & 2 deletions coding/json_stream.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ipfsld
package coding

import (
"encoding/json"
"fmt"
"io"

reader "github.com/ipfs/go-ipld/reader"
reader "github.com/ipfs/go-ipld"
)

type JSONDecoder struct {
Expand Down
42 changes: 21 additions & 21 deletions coding/pb/pbcodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
mc "github.com/jbenet/go-multicodec"
mcproto "github.com/jbenet/go-multicodec/protobuf"

ipld "github.com/ipfs/go-ipld"
memory "github.com/ipfs/go-ipld/memory"
)

var Header []byte
Expand Down Expand Up @@ -56,9 +56,9 @@ type decoder struct {
}

func (c *encoder) Encode(v interface{}) error {
nv, ok := v.(*ipld.Node)
nv, ok := v.(*memory.Node)
if !ok {
return errors.New("must encode *ipld.Node")
return errors.New("must encode *memory.Node")
}

if _, err := c.w.Write(c.c.Header()); err != nil {
Expand All @@ -74,9 +74,9 @@ func (c *encoder) Encode(v interface{}) error {
}

func (c *decoder) Decode(v interface{}) error {
nv, ok := v.(*ipld.Node)
nv, ok := v.(*memory.Node)
if !ok {
return errors.New("must decode to *ipld.Node")
return errors.New("must decode to *memory.Node")
}

if err := mc.ConsumeHeader(c.r, c.c.Header()); err != nil {
Expand All @@ -92,14 +92,14 @@ func (c *decoder) Decode(v interface{}) error {
return nil
}

func ld2pbNode(in *ipld.Node) (*PBNode, error) {
func ld2pbNode(in *memory.Node) (*PBNode, error) {
n := *in
var pbn PBNode
var attrs ipld.Node
var attrs memory.Node

if attrsvalue, hasattrs := n["@attrs"]; hasattrs {
var ok bool
attrs, ok = attrsvalue.(ipld.Node)
attrs, ok = attrsvalue.(memory.Node)
if !ok {
return nil, errInvalidData
}
Expand All @@ -116,7 +116,7 @@ func ld2pbNode(in *ipld.Node) (*PBNode, error) {
}

if links, haslinks := attrs["links"]; haslinks {
links, ok := links.([]ipld.Node)
links, ok := links.([]memory.Node)
if !ok {
return nil, errInvalidLink
}
Expand All @@ -132,37 +132,37 @@ func ld2pbNode(in *ipld.Node) (*PBNode, error) {
return &pbn, nil
}

func pb2ldNode(pbn *PBNode, in *ipld.Node) {
*in = make(ipld.Node)
func pb2ldNode(pbn *PBNode, in *memory.Node) {
*in = make(memory.Node)
n := *in

links := make([]ipld.Node, len(pbn.Links))
links := make([]memory.Node, len(pbn.Links))
for i, link := range pbn.Links {
links[i] = pb2ldLink(link)
n[ipld.EscapePathComponent(link.GetName())] = links[i]
n[memory.EscapePathComponent(link.GetName())] = links[i]
}

n["@attrs"] = ipld.Node{
n["@attrs"] = memory.Node{
"links": links,
"data": pbn.Data,
"data": pbn.Data,
}
}

func pb2ldLink(pbl *PBLink) (link ipld.Node) {
func pb2ldLink(pbl *PBLink) (link memory.Node) {
defer func() {
if recover() != nil {
link = nil
}
}()

link = make(ipld.Node)
link = make(memory.Node)
link["hash"] = pbl.Hash
link["name"] = *pbl.Name
link["size"] = uint64(*pbl.Tsize)
return link
}

func ld2pbLink(link ipld.Node) (pbl *PBLink) {
func ld2pbLink(link memory.Node) (pbl *PBLink) {
defer func() {
if recover() != nil {
pbl = nil
Expand All @@ -180,7 +180,7 @@ func ld2pbLink(link ipld.Node) (pbl *PBLink) {
return pbl
}

func IsOldProtobufNode(n ipld.Node) bool {
func IsOldProtobufNode(n memory.Node) bool {
if len(n) > 2 { // short circuit
return false
}
Expand All @@ -206,14 +206,14 @@ func IsOldProtobufNode(n ipld.Node) bool {
}

if hasLinks {
links, ok := links.([]ipld.Node)
links, ok := links.([]memory.Node)
if !ok {
return false // invalid links.
}

// every link must be a mlink
for _, link := range links {
if !ipld.IsLink(link) {
if !memory.IsLink(link) {
return false
}
}
Expand Down
Loading

0 comments on commit 2a18296

Please sign in to comment.