Skip to content

Commit 3a1084e

Browse files
committed
coreapi: remove ctx from ParsePath, split ParseCid
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 1b755d6 commit 3a1084e

File tree

10 files changed

+60
-38
lines changed

10 files changed

+60
-38
lines changed

core/coreapi/block.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
6565
return nil, err
6666
}
6767

68-
return api.ParseCid(b.Cid()), nil
68+
return api.core().IpldPath(b.Cid()), nil
6969
}
7070

7171
func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {
72-
rp, err := api.ResolvePath(ctx, p)
72+
rp, err := api.core().ResolvePath(ctx, p)
7373
if err != nil {
7474
return nil, err
7575
}
@@ -83,7 +83,7 @@ func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, erro
8383
}
8484

8585
func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.BlockRmOption) error {
86-
rp, err := api.ResolvePath(ctx, p)
86+
rp, err := api.core().ResolvePath(ctx, p)
8787
if err != nil {
8888
return err
8989
}
@@ -121,7 +121,7 @@ func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.Bl
121121
}
122122

123123
func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.BlockStat, error) {
124-
rp, err := api.ResolvePath(ctx, p)
124+
rp, err := api.core().ResolvePath(ctx, p)
125125
if err != nil {
126126
return nil, err
127127
}
@@ -132,7 +132,7 @@ func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.Bloc
132132
}
133133

134134
return &BlockStat{
135-
path: api.ParseCid(b.Cid()),
135+
path: api.core().IpldPath(b.Cid()),
136136
size: len(b.RawData()),
137137
}, nil
138138
}
@@ -144,3 +144,7 @@ func (bs *BlockStat) Size() int {
144144
func (bs *BlockStat) Path() coreiface.ResolvedPath {
145145
return bs.path
146146
}
147+
148+
func (api *BlockAPI) core() coreiface.CoreAPI {
149+
return (*CoreAPI)(api)
150+
}

core/coreapi/dag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
4444
return nil, err
4545
}
4646

47-
return api.ParseCid(nds[0].Cid()), nil
47+
return api.core().IpldPath(nds[0].Cid()), nil
4848
}
4949

5050
// Get resolves `path` using Unixfs resolver, returns the resolved Node.
@@ -66,7 +66,7 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.Da
6666
paths := n.Tree("", settings.Depth)
6767
out := make([]coreiface.Path, len(paths))
6868
for n, p2 := range paths {
69-
out[n], err = api.ParsePath(ctx, gopath.Join(p.String(), p2))
69+
out[n], err = api.core().ParsePath(gopath.Join(p.String(), p2))
7070
if err != nil {
7171
return nil, err
7272
}

core/coreapi/interface/coreapi.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ type CoreAPI interface {
4040
ResolveNode(context.Context, Path) (ipld.Node, error)
4141

4242
// ParsePath parses string path to a Path
43-
ParsePath(context.Context, string) (Path, error)
43+
ParsePath(string) (Path, error)
4444

45-
// ParseCid creates new path from the provided CID
46-
ParseCid(*cid.Cid) ResolvedPath
45+
// IpfsPath creates new /ipfs path from the provided CID
46+
IpfsPath(*cid.Cid) ResolvedPath
47+
48+
// IpldPath creates new /ipld path from the provided CID
49+
IpldPath(*cid.Cid) ResolvedPath
4750
}

core/coreapi/object.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
121121
return nil, err
122122
}
123123

124-
return api.ParseCid(dagnode.Cid()), nil
124+
return api.core().IpfsPath(dagnode.Cid()), nil
125125
}
126126

127127
func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (ipld.Node, error) {
@@ -218,7 +218,7 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
218218
return nil, err
219219
}
220220

221-
return api.ParseCid(nnode.Cid()), nil
221+
return api.core().IpfsPath(nnode.Cid()), nil
222222
}
223223

224224
func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.ResolvedPath, error) {
@@ -244,7 +244,7 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri
244244
return nil, err
245245
}
246246

247-
return api.ParseCid(nnode.Cid()), nil
247+
return api.core().IpfsPath(nnode.Cid()), nil
248248
}
249249

250250
func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
@@ -281,7 +281,7 @@ func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.R
281281
return nil, err
282282
}
283283

284-
return api.ParseCid(pbnd.Cid()), nil
284+
return api.core().IpfsPath(pbnd.Cid()), nil
285285
}
286286

287287
func (api *ObjectAPI) core() coreiface.CoreAPI {

core/coreapi/path.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ type resolvedPath struct {
2626
root *cid.Cid
2727
}
2828

29-
// ParseCid parses the path from `c`, retruns the parsed path.
30-
func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.ResolvedPath {
31-
return &resolvedPath{path: path{ipfspath.FromCid(c)}, cid: c, root: c}
29+
// IpfsPath parses the path from `c`, reruns the parsed path.
30+
func (api *CoreAPI) IpfsPath(c *cid.Cid) coreiface.ResolvedPath {
31+
return &resolvedPath{path: path{ipfspath.Path("/ipfs/" + c.String())}, cid: c, root: c}
3232
}
3333

34-
// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
34+
// IpldPath parses the path from `c`, reruns the parsed path.
35+
func (api *CoreAPI) IpldPath(c *cid.Cid) coreiface.ResolvedPath {
36+
return &resolvedPath{path: path{ipfspath.Path("/ipld/" + c.String())}, cid: c, root: c}
37+
}
38+
39+
// ResolveNode resolves the path `p` using Unixfs resolver, gets and returns the
3540
// resolved Node.
3641
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
3742
return resolveNode(ctx, api.node.DAG, api.node.Namesys, p)
@@ -83,7 +88,7 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
8388
}
8489

8590
// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
86-
func (api *CoreAPI) ParsePath(ctx context.Context, p string) (coreiface.Path, error) {
91+
func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) {
8792
pp, err := ipfspath.ParsePath(p)
8893
if err != nil {
8994
return nil, err

core/coreapi/pin.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ func (api *PinAPI) Add(ctx context.Context, p coreiface.Path, opts ...caopts.Pin
2424

2525
defer api.node.Blockstore.PinLock().Unlock()
2626

27-
_, err = corerepo.Pin(api.node, ctx, []string{p.String()}, settings.Recursive)
27+
rp, err := api.core().ResolvePath(ctx, p)
28+
if err != nil {
29+
return err
30+
}
31+
32+
_, err = corerepo.Pin(api.node, ctx, []string{rp.Cid().String()}, settings.Recursive)
2833
if err != nil {
2934
return err
3035
}
@@ -62,12 +67,12 @@ func (api *PinAPI) Update(ctx context.Context, from coreiface.Path, to coreiface
6267
return err
6368
}
6469

65-
fp, err := api.ResolvePath(ctx, from)
70+
fp, err := api.core().ResolvePath(ctx, from)
6671
if err != nil {
6772
return err
6873
}
6974

70-
tp, err := api.ResolvePath(ctx, to)
75+
tp, err := api.core().ResolvePath(ctx, to)
7176
if err != nil {
7277
return err
7378
}
@@ -120,7 +125,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
120125
links, err := getLinks(ctx, root)
121126
if err != nil {
122127
status := &pinStatus{ok: false, cid: root}
123-
status.badNodes = []coreiface.BadPinNode{&badNode{path: api.ParseCid(root), err: err}}
128+
status.badNodes = []coreiface.BadPinNode{&badNode{path: api.core().IpldPath(root), err: err}}
124129
visited[key] = status
125130
return status
126131
}
@@ -170,7 +175,7 @@ func (api *PinAPI) pinLsAll(typeStr string, ctx context.Context) ([]coreiface.Pi
170175
for _, c := range keyList {
171176
keys[c.String()] = &pinInfo{
172177
pinType: typeStr,
173-
path: api.ParseCid(c),
178+
path: api.core().IpldPath(c),
174179
}
175180
}
176181
}
@@ -199,3 +204,7 @@ func (api *PinAPI) pinLsAll(typeStr string, ctx context.Context) ([]coreiface.Pi
199204

200205
return out, nil
201206
}
207+
208+
func (api *PinAPI) core() coreiface.CoreAPI {
209+
return (*CoreAPI)(api)
210+
}

core/coreapi/pin_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestPinSimple(t *testing.T) {
5252
t.Errorf("unexpected pin list len: %d", len(list))
5353
}
5454

55-
if list[0].Path().String() != p.String() {
55+
if list[0].Path().Cid().String() != p.Cid().String() {
5656
t.Error("paths don't match")
5757
}
5858

@@ -156,7 +156,7 @@ func TestPinRecursive(t *testing.T) {
156156
t.Errorf("unexpected pin list len: %d", len(list))
157157
}
158158

159-
if list[0].Path().String() != p0.String() {
159+
if list[0].Path().Cid().String() != p0.Cid().String() {
160160
t.Error("unexpected path")
161161
}
162162

@@ -195,7 +195,7 @@ func TestPinRecursive(t *testing.T) {
195195
t.Fatalf("unexpected badNodes len")
196196
}
197197

198-
if r.BadNodes()[0].Path().String() != p0.String() {
198+
if r.BadNodes()[0].Path().Cid().String() != p0.Cid().String() {
199199
t.Error("unexpected badNode path")
200200
}
201201

core/coreapi/unixfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.ResolvedP
2525
if err != nil {
2626
return nil, err
2727
}
28-
return api.core().ParseCid(c), nil
28+
return api.core().IpfsPath(c), nil
2929
}
3030

3131
// Cat returns the data contained by an IPFS or IPNS object(s) at path `p`.

core/coreapi/unixfs_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
core "github.com/ipfs/go-ipfs/core"
1313
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
1414
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
15+
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
1516
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
1617
keystore "github.com/ipfs/go-ipfs/keystore"
1718
mdag "github.com/ipfs/go-ipfs/merkledag"
@@ -213,18 +214,18 @@ func TestCatDir(t *testing.T) {
213214
if err != nil {
214215
t.Error(err)
215216
}
216-
p := api.ParseCid(edir.Cid())
217+
p := api.IpfsPath(edir.Cid())
217218

218-
emptyDir, err := api.Object().New(ctx, api.Object().WithType("unixfs-dir"))
219+
emptyDir, err := api.Object().New(ctx, options.Object.Type("unixfs-dir"))
219220
if err != nil {
220221
t.Error(err)
221222
}
222223

223-
if p.String() != api.ParseCid(emptyDir.Cid()).String() {
224+
if p.String() != api.IpfsPath(emptyDir.Cid()).String() {
224225
t.Fatalf("expected path %s, got: %s", emptyDir.Cid(), p.String())
225226
}
226227

227-
_, err = api.Unixfs().Cat(ctx, api.ParseCid(emptyDir.Cid()))
228+
_, err = api.Unixfs().Cat(ctx, api.IpfsPath(emptyDir.Cid()))
228229
if err != coreiface.ErrIsDir {
229230
t.Fatalf("expected ErrIsDir, got: %s", err)
230231
}
@@ -243,7 +244,7 @@ func TestCatNonUnixfs(t *testing.T) {
243244
t.Error(err)
244245
}
245246

246-
_, err = api.Unixfs().Cat(ctx, api.ParseCid(nd.Cid()))
247+
_, err = api.Unixfs().Cat(ctx, api.IpfsPath(nd.Cid()))
247248
if !strings.Contains(err.Error(), "proto: required field") {
248249
t.Fatalf("expected protobuf error, got: %s", err)
249250
}
@@ -318,12 +319,12 @@ func TestLsEmptyDir(t *testing.T) {
318319
t.Error(err)
319320
}
320321

321-
emptyDir, err := api.Object().New(ctx, api.Object().WithType("unixfs-dir"))
322+
emptyDir, err := api.Object().New(ctx, options.Object.Type("unixfs-dir"))
322323
if err != nil {
323324
t.Error(err)
324325
}
325326

326-
links, err := api.Unixfs().Ls(ctx, api.ParseCid(emptyDir.Cid()))
327+
links, err := api.Unixfs().Ls(ctx, api.IpfsPath(emptyDir.Cid()))
327328
if err != nil {
328329
t.Error(err)
329330
}
@@ -351,7 +352,7 @@ func TestLsNonUnixfs(t *testing.T) {
351352
t.Error(err)
352353
}
353354

354-
links, err := api.Unixfs().Ls(ctx, api.ParseCid(nd.Cid()))
355+
links, err := api.Unixfs().Ls(ctx, api.IpfsPath(nd.Cid()))
355356
if err != nil {
356357
t.Error(err)
357358
}

core/corehttp/gateway_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
161161
ipnsHostname = true
162162
}
163163

164-
parsedPath, err := i.api.ParsePath(ctx, urlPath)
164+
parsedPath, err := i.api.ParsePath(urlPath)
165165
if err != nil {
166166
webError(w, "invalid ipfs path", err, http.StatusBadRequest)
167167
return
@@ -294,7 +294,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
294294
return
295295
}
296296

297-
dr, err := i.api.Unixfs().Cat(ctx, i.api.ParseCid(ixnd.Cid()))
297+
dr, err := i.api.Unixfs().Cat(ctx, i.api.IpfsPath(ixnd.Cid()))
298298
if err != nil {
299299
internalWebError(w, err)
300300
return

0 commit comments

Comments
 (0)