diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 9426e63aeb2..bc6aa8ce267 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -25,7 +25,7 @@ func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI { } func (api *CoreAPI) Dag() coreiface.DagAPI { - return (*DagAPI)(api) + return (*dagAPI)(api) } func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) { diff --git a/core/coreapi/dag.go b/core/coreapi/dag.go index f32cf0b7390..f50fe6877f9 100644 --- a/core/coreapi/dag.go +++ b/core/coreapi/dag.go @@ -14,9 +14,9 @@ import ( mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash" ) -type DagAPI CoreAPI +type dagAPI CoreAPI -func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) { +func (api *dagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]coreiface.Node, error) { if format == nil { format = &cid.Prefix{ Version: 1, @@ -51,11 +51,11 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, inputEnc string, form return out, nil } -func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { +func (api *dagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) { return api.core().ResolveNode(ctx, path) } -func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) { +func (api *dagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]coreiface.Path, error) { n, err := api.Get(ctx, p) if err != nil { return nil, err @@ -72,6 +72,6 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, depth int) ([]cor return out, nil } -func (api *DagAPI) core() coreiface.CoreAPI { +func (api *dagAPI) core() coreiface.CoreAPI { return (*CoreAPI)(api) } diff --git a/core/coreapi/dag_test.go b/core/coreapi/dag_test.go new file mode 100644 index 00000000000..ee2d8ee22c8 --- /dev/null +++ b/core/coreapi/dag_test.go @@ -0,0 +1,93 @@ +package coreapi_test + +import ( + "context" + "path" + "strings" + "testing" + + coreapi "github.com/ipfs/go-ipfs/core/coreapi" +) + +var ( + treeExpected = map[string]struct{}{ + "a": {}, + "b": {}, + "c": {}, + "c/d": {}, + "c/e": {}, + } +) + +func TestPut(t *testing.T) { + ctx := context.Background() + _, api, err := makeAPI(ctx) + if err != nil { + t.Error(err) + } + + res, err := api.Dag().Put(ctx, strings.NewReader(`"Hello"`), "json", nil) + if err != nil { + t.Error(err) + } + + if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" { + t.Errorf("got wrong cid: %s", res[0].Cid().String()) + } +} + +func TestPath(t *testing.T) { + ctx := context.Background() + _, api, err := makeAPI(ctx) + if err != nil { + t.Error(err) + } + + sub, err := api.Dag().Put(ctx, strings.NewReader(`"foo"`), "json", nil) + if err != nil { + t.Error(err) + } + + res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`), "json", nil) + if err != nil { + t.Error(err) + } + + p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk")) + if err != nil { + t.Error(err) + } + + nd, err := api.Dag().Get(ctx, p) + if err != nil { + t.Error(err) + } + + if nd.Cid().String() != sub[0].Cid().String() { + t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub[0].Cid().String()) + } +} + +func TestTree(t *testing.T) { + ctx := context.Background() + _, api, err := makeAPI(ctx) + if err != nil { + t.Error(err) + } + + res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), "json", nil) + if err != nil { + t.Error(err) + } + + lst := res[0].Tree("", -1) + if len(lst) != len(treeExpected) { + t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected)) + } + + for _, ent := range lst { + if _, ok := treeExpected[ent]; !ok { + t.Errorf("unexpected tree entry %s", ent) + } + } +} diff --git a/core/coreapi/interface/interface.go b/core/coreapi/interface/interface.go index d774b65bd47..55d63693b52 100644 --- a/core/coreapi/interface/interface.go +++ b/core/coreapi/interface/interface.go @@ -31,7 +31,7 @@ type CoreAPI interface { Dag() DagAPI ResolvePath(context.Context, Path) (Path, error) - ResolveNode(context.Context, Path) (Node, error) //TODO: should this get dropped in favor of DagAPI.Get? + ResolveNode(context.Context, Path) (Node, error) } type UnixfsAPI interface { @@ -40,9 +40,17 @@ type UnixfsAPI interface { Ls(context.Context, Path) ([]*Link, error) } +// DagAPI specifies the interface to IPLD type DagAPI interface { + // Put inserts data using specified format and input encoding. If format is + // not specified (nil), default dag-cbor/sha256 is used Put(ctx context.Context, src io.Reader, inputEnc string, format *cid.Prefix) ([]Node, error) + + // Get attempts to resolve and get the node specified by the path Get(ctx context.Context, path Path) (Node, error) + + // Tree returns list of paths within a node specified by the path. + // To get all paths in a tree, set depth to -1 Tree(ctx context.Context, path Path, depth int) ([]Path, error) } diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index 49f8815b91f..114785f5810 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -8,6 +8,8 @@ import ( "strings" "testing" + cbor "gx/ipfs/QmWCs8kMecJwCPK8JThue8TjgM2ieJ2HjTLDu7Cv2NEmZi/go-ipld-cbor" + core "github.com/ipfs/go-ipfs/core" coreapi "github.com/ipfs/go-ipfs/core/coreapi" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" @@ -17,7 +19,6 @@ import ( config "github.com/ipfs/go-ipfs/repo/config" ds2 "github.com/ipfs/go-ipfs/thirdparty/datastore2" unixfs "github.com/ipfs/go-ipfs/unixfs" - cbor "gx/ipfs/QmWCs8kMecJwCPK8JThue8TjgM2ieJ2HjTLDu7Cv2NEmZi/go-ipld-cbor" ) // `echo -n 'hello, world!' | ipfs add` @@ -30,7 +31,7 @@ var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbs // `echo -n | ipfs add` var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil) -func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) { +func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) { r := &repo.Mock{ C: config.Config{ Identity: config.Identity{ @@ -43,7 +44,7 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.UnixfsAPI, error) { if err != nil { return nil, nil, err } - api := coreapi.NewCoreAPI(node).Unixfs() + api := coreapi.NewCoreAPI(node) return node, api, nil } @@ -55,7 +56,7 @@ func TestAdd(t *testing.T) { } str := strings.NewReader(helloStr) - p, err := api.Add(ctx, str) + p, err := api.Unixfs().Add(ctx, str) if err != nil { t.Error(err) } @@ -64,7 +65,7 @@ func TestAdd(t *testing.T) { t.Fatalf("expected path %s, got: %s", hello, p) } - r, err := api.Cat(ctx, hello) + r, err := api.Unixfs().Cat(ctx, hello) if err != nil { t.Fatal(err) } @@ -87,7 +88,7 @@ func TestAddEmptyFile(t *testing.T) { } str := strings.NewReader("") - p, err := api.Add(ctx, str) + p, err := api.Unixfs().Add(ctx, str) if err != nil { t.Error(err) } @@ -115,7 +116,7 @@ func TestCatBasic(t *testing.T) { t.Fatalf("expected CID %s, got: %s", hello, p) } - r, err := api.Cat(ctx, hello) + r, err := api.Unixfs().Cat(ctx, hello) if err != nil { t.Fatal(err) } @@ -142,7 +143,7 @@ func TestCatEmptyFile(t *testing.T) { t.Fatal(err) } - r, err := api.Cat(ctx, emptyFile) + r, err := api.Unixfs().Cat(ctx, emptyFile) if err != nil { t.Fatal(err) } @@ -174,7 +175,7 @@ func TestCatDir(t *testing.T) { t.Fatalf("expected path %s, got: %s", emptyDir, p) } - _, err = api.Cat(ctx, emptyDir) + _, err = api.Unixfs().Cat(ctx, emptyDir) if err != coreiface.ErrIsDir { t.Fatalf("expected ErrIsDir, got: %s", err) } @@ -192,7 +193,7 @@ func TestCatNonUnixfs(t *testing.T) { t.Error(err) } - _, err = api.Cat(ctx, coreapi.ParseCid(c)) + _, err = api.Unixfs().Cat(ctx, coreapi.ParseCid(c)) if !strings.Contains(err.Error(), "proto: required field") { t.Fatalf("expected protobuf error, got: %s", err) } @@ -205,7 +206,7 @@ func TestCatOffline(t *testing.T) { t.Error(err) } - _, err = api.Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil)) + _, err = api.Unixfs().Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil)) if err != coreiface.ErrOffline { t.Fatalf("expected ErrOffline, got: %s", err) } @@ -229,7 +230,7 @@ func TestLs(t *testing.T) { } p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil) - links, err := api.Ls(ctx, p) + links, err := api.Unixfs().Ls(ctx, p) if err != nil { t.Error(err) } @@ -260,7 +261,7 @@ func TestLsEmptyDir(t *testing.T) { t.Error(err) } - links, err := api.Ls(ctx, emptyDir) + links, err := api.Unixfs().Ls(ctx, emptyDir) if err != nil { t.Error(err) } @@ -288,7 +289,7 @@ func TestLsNonUnixfs(t *testing.T) { t.Error(err) } - links, err := api.Ls(ctx, coreapi.ParseCid(c)) + links, err := api.Unixfs().Ls(ctx, coreapi.ParseCid(c)) if err != nil { t.Error(err) }