Skip to content

Commit

Permalink
coreapi: dag review
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Dec 22, 2017
1 parent f153c01 commit f213060
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
14 changes: 5 additions & 9 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type DagAPI struct {
*caopts.DagOptions
}

func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) ([]coreiface.Node, error) {
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) {
settings, err := caopts.DagPutOptions(opts...)
if err != nil {
return nil, err
Expand All @@ -38,16 +38,12 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
return nil, fmt.Errorf("no node returned from ParseInputs")
}

out := make([]coreiface.Node, len(nds))
for n, nd := range nds {
_, err := api.node.DAG.Add(nd)
if err != nil {
return nil, err
}
out[n] = nd
_, err = api.node.DAG.Add(nds[0])
if err != nil {
return nil, err
}

return out, nil
return ParseCid(nds[0].Cid()), nil
}

func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
Expand Down
18 changes: 9 additions & 9 deletions core/coreapi/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestPut(t *testing.T) {
t.Error(err)
}

if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
t.Errorf("got wrong cid: %s", res[0].Cid().String())
if res.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
t.Errorf("got wrong cid: %s", res.Cid().String())
}
}

Expand All @@ -50,8 +50,8 @@ func TestPutWithHash(t *testing.T) {
t.Error(err)
}

if res[0].Cid().String() != "z5hRLNd2sv4z1c" {
t.Errorf("got wrong cid: %s", res[0].Cid().String())
if res.Cid().String() != "z5hRLNd2sv4z1c" {
t.Errorf("got wrong cid: %s", res.Cid().String())
}
}

Expand All @@ -67,12 +67,12 @@ func TestPath(t *testing.T) {
t.Error(err)
}

res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`))
res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub.Cid().String()+`"}}`))
if err != nil {
t.Error(err)
}

p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk"))
p, err := coreapi.ParsePath(path.Join(res.Cid().String(), "lnk"))
if err != nil {
t.Error(err)
}
Expand All @@ -82,8 +82,8 @@ func TestPath(t *testing.T) {
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())
if nd.Cid().String() != sub.Cid().String() {
t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub.Cid().String())
}
}

Expand All @@ -99,7 +99,7 @@ func TestTree(t *testing.T) {
t.Error(err)
}

lst := res[0].Tree("", -1)
lst := res.Tree("", -1)
if len(lst) != len(treeExpected) {
t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
}
Expand Down
5 changes: 3 additions & 2 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ type UnixfsAPI interface {
// 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, opts ...options.DagPutOption) ([]Node, error)
// Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
// "sha256" are used.
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error)

// WithInputEnc is an option for Put which specifies the input encoding of the
// data. Default is "json", most formats/codecs support "raw"
Expand Down

0 comments on commit f213060

Please sign in to comment.