@cryptotaxi247 / kubo / commits / db318333a

coreapi: dag review

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Dec 21, 2017 at 01:56 UTC db318333abaca6490bfa618966816b1fc6d756a9
3 files changed +23 -21
core/coreapi/dag.go
+5 -9
@@ -19,7 +19,7 @@ type DagAPI struct {
19 *caopts.DagOptions
20 }
21
22 -func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) ([]coreiface.Node, error) {
22 +func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) {
23 settings, err := caopts.DagPutOptions(opts...)
24 if err != nil {
25 return nil, err
@@ -38,16 +38,12 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
38 return nil, fmt.Errorf("no node returned from ParseInputs")
39 }
40
41 - out := make([]coreiface.Node, len(nds))
42 - for n, nd := range nds {
43 - _, err := api.node.DAG.Add(nd)
44 - if err != nil {
45 - return nil, err
46 - }
47 - out[n] = nd
41 + _, err = api.node.DAG.Add(nds[0])
42 + if err != nil {
43 + return nil, err
44 }
45
50 - return out, nil
46 + return ParseCid(nds[0].Cid()), nil
47 }
48
49 func (api *DagAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
core/coreapi/dag_test.go
+15 -10
@@ -33,8 +33,8 @@ func TestPut(t *testing.T) {
33 t.Error(err)
34 }
35
36 - if res[0].Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
37 - t.Errorf("got wrong cid: %s", res[0].Cid().String())
36 + if res.Cid().String() != "zdpuAqckYF3ToF3gcJNxPZXmnmGuXd3gxHCXhq81HGxBejEvv" {
37 + t.Errorf("got wrong cid: %s", res.Cid().String())
38 }
39 }
40
@@ -50,8 +50,8 @@ func TestPutWithHash(t *testing.T) {
50 t.Error(err)
51 }
52
53 - if res[0].Cid().String() != "z5hRLNd2sv4z1c" {
54 - t.Errorf("got wrong cid: %s", res[0].Cid().String())
53 + if res.Cid().String() != "z5hRLNd2sv4z1c" {
54 + t.Errorf("got wrong cid: %s", res.Cid().String())
55 }
56 }
57
@@ -67,12 +67,12 @@ func TestPath(t *testing.T) {
67 t.Error(err)
68 }
69
70 - res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub[0].Cid().String()+`"}}`))
70 + res, err := api.Dag().Put(ctx, strings.NewReader(`{"lnk": {"/": "`+sub.Cid().String()+`"}}`))
71 if err != nil {
72 t.Error(err)
73 }
74
75 - p, err := coreapi.ParsePath(path.Join(res[0].Cid().String(), "lnk"))
75 + p, err := coreapi.ParsePath(path.Join(res.Cid().String(), "lnk"))
76 if err != nil {
77 t.Error(err)
78 }
@@ -82,8 +82,8 @@ func TestPath(t *testing.T) {
82 t.Error(err)
83 }
84
85 - if nd.Cid().String() != sub[0].Cid().String() {
86 - t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub[0].Cid().String())
85 + if nd.Cid().String() != sub.Cid().String() {
86 + t.Errorf("got unexpected cid %s, expected %s", nd.Cid().String(), sub.Cid().String())
87 }
88 }
89
@@ -94,12 +94,17 @@ func TestTree(t *testing.T) {
94 t.Error(err)
95 }
96
97 - res, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`))
97 + c, err := api.Dag().Put(ctx, strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`))
98 if err != nil {
99 t.Error(err)
100 }
101
102 - lst := res[0].Tree("", -1)
102 + res, err := api.Dag().Get(ctx, c)
103 + if err != nil {
104 + t.Error(err)
105 + }
106 +
107 + lst := res.Tree("", -1)
108 if len(lst) != len(treeExpected) {
109 t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
110 }
core/coreapi/interface/interface.go
+3 -2
@@ -61,8 +61,9 @@ type UnixfsAPI interface {
61 // DagAPI specifies the interface to IPLD
62 type DagAPI interface {
63 // Put inserts data using specified format and input encoding.
64 - // If format is not specified (nil), default dag-cbor/sha256 is used
65 - Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) ([]Node, error)
64 + // Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
65 + // "sha256" are used.
66 + Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error)
67
68 // WithInputEnc is an option for Put which specifies the input encoding of the
69 // data. Default is "json", most formats/codecs support "raw"