提交 db318333 作者: Łukasz Magiera

coreapi: dag review

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 f153c01d
...@@ -19,7 +19,7 @@ type DagAPI struct { ...@@ -19,7 +19,7 @@ type DagAPI struct {
*caopts.DagOptions *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...) settings, err := caopts.DagPutOptions(opts...)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -38,16 +38,12 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut ...@@ -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") return nil, fmt.Errorf("no node returned from ParseInputs")
} }
out := make([]coreiface.Node, len(nds)) _, err = api.node.DAG.Add(nds[0])
for n, nd := range nds { if err != nil {
_, err := api.node.DAG.Add(nd) return nil, err
if err != nil {
return nil, err
}
out[n] = nd
} }
return out, nil return ParseCid(nds[0].Cid()), 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) {
......
...@@ -33,8 +33,8 @@ func TestPut(t *testing.T) { ...@@ -33,8 +33,8 @@ func TestPut(t *testing.T) {
t.Error(err) t.Error(err)
} }
if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" { if res.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
t.Errorf("got wrong cid: %s", res[0].Cid().String()) t.Errorf("got wrong cid: %s", res.Cid().String())
} }
} }
...@@ -50,8 +50,8 @@ func TestPutWithHash(t *testing.T) { ...@@ -50,8 +50,8 @@ func TestPutWithHash(t *testing.T) {
t.Error(err) t.Error(err)
} }
if res[0].Cid().String() != "z5hRLNd2sv4z1c" { if res.Cid().String() != "z5hRLNd2sv4z1c" {
t.Errorf("got wrong cid: %s", res[0].Cid().String()) t.Errorf("got wrong cid: %s", res.Cid().String())
} }
} }
...@@ -67,12 +67,12 @@ func TestPath(t *testing.T) { ...@@ -67,12 +67,12 @@ func TestPath(t *testing.T) {
t.Error(err) 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 { if err != nil {
t.Error(err) 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 { if err != nil {
t.Error(err) t.Error(err)
} }
...@@ -82,8 +82,8 @@ func TestPath(t *testing.T) { ...@@ -82,8 +82,8 @@ func TestPath(t *testing.T) {
t.Error(err) t.Error(err)
} }
if 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[0].Cid().String()) t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub.Cid().String())
} }
} }
...@@ -94,12 +94,17 @@ func TestTree(t *testing.T) { ...@@ -94,12 +94,17 @@ func TestTree(t *testing.T) {
t.Error(err) t.Error(err)
} }
res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`)) c, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
lst := res[0].Tree("", -1) res, err := api.Dag().Get(ctx, c)
if err != nil {
t.Error(err)
}
lst := res.Tree("", -1)
if len(lst) != len(treeExpected) { if len(lst) != len(treeExpected) {
t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected)) t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
} }
......
...@@ -61,8 +61,9 @@ type UnixfsAPI interface { ...@@ -61,8 +61,9 @@ type UnixfsAPI interface {
// DagAPI specifies the interface to IPLD // DagAPI specifies the interface to IPLD
type DagAPI interface { type DagAPI interface {
// Put inserts data using specified format and input encoding. // Put inserts data using specified format and input encoding.
// If format is not specified (nil), default dag-cbor/sha256 is used // Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) ([]Node, error) // "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 // WithInputEnc is an option for Put which specifies the input encoding of the
// data. Default is "json", most formats/codecs support "raw" // data. Default is "json", most formats/codecs support "raw"
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论