Skip to content

Commit 87cb6a2

Browse files
committed
coreapi: separate path into two types
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent c2dd400 commit 87cb6a2

File tree

13 files changed

+167
-148
lines changed

13 files changed

+167
-148
lines changed

core/coreapi/block.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type BlockStat struct {
2727
size int
2828
}
2929

30-
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.Path, error) {
30+
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.ResolvedPath, error) {
3131
settings, err := caopts.BlockPutOptions(opts...)
3232
if err != nil {
3333
return nil, err
@@ -72,7 +72,12 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
7272
}
7373

7474
func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {
75-
b, err := api.node.Blocks.GetBlock(ctx, p.Cid())
75+
rp, err := api.ResolvePath(ctx, p)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
b, err := api.node.Blocks.GetBlock(ctx, rp.Cid())
7681
if err != nil {
7782
return nil, err
7883
}
@@ -81,11 +86,16 @@ func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, erro
8186
}
8287

8388
func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.BlockRmOption) error {
89+
rp, err := api.ResolvePath(ctx, p)
90+
if err != nil {
91+
return err
92+
}
93+
8494
settings, err := caopts.BlockRmOptions(opts...)
8595
if err != nil {
8696
return err
8797
}
88-
cids := []*cid.Cid{p.Cid()}
98+
cids := []*cid.Cid{rp.Cid()}
8999
o := util.RmBlocksOpts{Force: settings.Force}
90100

91101
out, err := util.RmBlocks(api.node.Blockstore, api.node.Pinning, cids, o)
@@ -116,7 +126,12 @@ func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.Bl
116126
}
117127

118128
func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.BlockStat, error) {
119-
b, err := api.node.Blocks.GetBlock(ctx, p.Cid())
129+
rp, err := api.ResolvePath(ctx, p)
130+
if err != nil {
131+
return nil, err
132+
}
133+
134+
b, err := api.node.Blocks.GetBlock(ctx, rp.Cid())
120135
if err != nil {
121136
return nil, err
122137
}

core/coreapi/coreapi.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ package coreapi
33
import (
44
core "github.com/ipfs/go-ipfs/core"
55
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
6-
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
76
)
87

98
type CoreAPI struct {
109
node *core.IpfsNode
11-
*caopts.ApiOptions
1210
}
1311

1412
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
1513
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
16-
api := &CoreAPI{n, nil}
14+
api := &CoreAPI{n}
1715
return api
1816
}
1917

core/coreapi/dag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type DagAPI struct {
2222
// Put inserts data using specified format and input encoding. Unless used with
2323
// `WithCodes` or `WithHash`, the defaults "dag-cbor" and "sha256" are used.
2424
// Returns the path of the inserted data.
25-
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) {
25+
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.ResolvedPath, error) {
2626
settings, err := caopts.DagPutOptions(opts...)
2727
if err != nil {
2828
return nil, err
@@ -68,7 +68,7 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.Da
6868
paths := n.Tree("", settings.Depth)
6969
out := make([]coreiface.Path, len(paths))
7070
for n, p2 := range paths {
71-
out[n], err = api.ParsePath(ctx, gopath.Join(p.String(), p2))
71+
out[n], err = api.ParsePath(gopath.Join(p.String(), p2))
7272
if err != nil {
7373
return nil, err
7474
}

core/coreapi/dag_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestPath(t *testing.T) {
7070
t.Error(err)
7171
}
7272

73-
p, err := api.ParsePath(ctx, path.Join(res.Cid().String(), "lnk"))
73+
p, err := api.ParsePath(path.Join(res.Cid().String(), "lnk"))
7474
if err != nil {
7575
t.Error(err)
7676
}

core/coreapi/interface/interface.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,26 @@ import (
1616

1717
// Path is a generic wrapper for paths used in the API. A path can be resolved
1818
// to a CID using one of Resolve functions in the API.
19+
// TODO: figure out/explain namespaces
1920
type Path interface {
2021
// String returns the path as a string.
2122
String() string
23+
24+
// Namespace returns the first component of the path
25+
Namespace() string
26+
}
27+
28+
// ResolvedPath is a resolved Path
29+
type ResolvedPath interface {
2230
// Cid returns cid referred to by path
2331
Cid() *cid.Cid
32+
2433
// Root returns cid of root path
2534
Root() *cid.Cid
26-
// Resolved returns whether path has been fully resolved
27-
Resolved() bool
35+
36+
//TODO: Path remainder
37+
38+
Path
2839
}
2940

3041
// TODO: should we really copy these?
@@ -61,7 +72,7 @@ type BlockStat interface {
6172
// Pin holds information about pinned resource
6273
type Pin interface {
6374
// Path to the pinned object
64-
Path() Path
75+
Path() ResolvedPath
6576

6677
// Type of the pin
6778
Type() string
@@ -79,7 +90,7 @@ type PinStatus interface {
7990
// BadPinNode is a node that has been marked as bad by Pin.Verify
8091
type BadPinNode interface {
8192
// Path is the path of the node
82-
Path() Path
93+
Path() ResolvedPath
8394

8495
// Err is the reason why the node has been marked as bad
8596
Err() error
@@ -101,33 +112,31 @@ type CoreAPI interface {
101112

102113
// Key returns an implementation of Key API.
103114
Key() KeyAPI
115+
116+
// Pin returns an implementation of Pin API
104117
Pin() PinAPI
105118

106119
// ObjectAPI returns an implementation of Object API
107120
Object() ObjectAPI
108121

109122
// ResolvePath resolves the path using Unixfs resolver
110-
ResolvePath(context.Context, Path) (Path, error)
123+
ResolvePath(context.Context, Path) (ResolvedPath, error)
111124

112125
// ResolveNode resolves the path (if not resolved already) using Unixfs
113126
// resolver, gets and returns the resolved Node
114127
ResolveNode(context.Context, Path) (Node, error)
115128

116129
// ParsePath parses string path to a Path
117-
ParsePath(context.Context, string, ...options.ParsePathOption) (Path, error)
118-
119-
// WithResolve is an option for ParsePath which when set to true tells
120-
// ParsePath to also resolve the path
121-
WithResolve(bool) options.ParsePathOption
130+
ParsePath(string) (Path, error)
122131

123132
// ParseCid creates new path from the provided CID
124-
ParseCid(*cid.Cid) Path
133+
ParseCid(*cid.Cid) ResolvedPath
125134
}
126135

127136
// UnixfsAPI is the basic interface to immutable files in IPFS
128137
type UnixfsAPI interface {
129138
// Add imports the data from the reader into merkledag file
130-
Add(context.Context, io.Reader) (Path, error)
139+
Add(context.Context, io.Reader) (ResolvedPath, error)
131140

132141
// Cat returns a reader for the file
133142
Cat(context.Context, Path) (Reader, error)
@@ -139,7 +148,7 @@ type UnixfsAPI interface {
139148
// BlockAPI specifies the interface to the block layer
140149
type BlockAPI interface {
141150
// Put imports raw block data, hashing it using specified settings.
142-
Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
151+
Put(context.Context, io.Reader, ...options.BlockPutOption) (ResolvedPath, error)
143152

144153
// WithFormat is an option for Put which specifies the multicodec to use to
145154
// serialize the object. Default is "v0"
@@ -173,7 +182,7 @@ type DagAPI interface {
173182
// Put inserts data using specified format and input encoding.
174183
// Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
175184
// "sha256" are used.
176-
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error)
185+
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (ResolvedPath, error)
177186

178187
// WithInputEnc is an option for Put which specifies the input encoding of the
179188
// data. Default is "json", most formats/codecs support "raw"
@@ -290,13 +299,13 @@ type ObjectAPI interface {
290299
WithType(string) options.ObjectNewOption
291300

292301
// Put imports the data into merkledag
293-
Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)
302+
Put(context.Context, io.Reader, ...options.ObjectPutOption) (ResolvedPath, error)
294303

295304
// WithInputEnc is an option for Put which specifies the input encoding of the
296305
// data. Default is "json".
297306
//
298307
// Supported encodings:
299-
// * "protobuf"
308+
// * "protobuf"reselved version of Path
300309
// * "json"
301310
WithInputEnc(e string) options.ObjectPutOption
302311

@@ -323,20 +332,20 @@ type ObjectAPI interface {
323332
// AddLink adds a link under the specified path. child path can point to a
324333
// subdirectory within the patent which must be present (can be overridden
325334
// with WithCreate option).
326-
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Path, error)
335+
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (ResolvedPath, error)
327336

328337
// WithCreate is an option for AddLink which specifies whether create required
329338
// directories for the child
330339
WithCreate(create bool) options.ObjectAddLinkOption
331340

332341
// RmLink removes a link from the node
333-
RmLink(ctx context.Context, base Path, link string) (Path, error)
342+
RmLink(ctx context.Context, base Path, link string) (ResolvedPath, error)
334343

335344
// AppendData appends data to the node
336-
AppendData(context.Context, Path, io.Reader) (Path, error)
345+
AppendData(context.Context, Path, io.Reader) (ResolvedPath, error)
337346

338347
// SetData sets the data contained in the node
339-
SetData(context.Context, Path, io.Reader) (Path, error)
348+
SetData(context.Context, Path, io.Reader) (ResolvedPath, error)
340349
}
341350

342351
// ObjectStat provides information about dag nodes

core/coreapi/interface/options/path.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

core/coreapi/object.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (
5959
return n, nil
6060
}
6161

62-
func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.Path, error) {
62+
func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.ResolvedPath, error) {
6363
options, err := caopts.ObjectPutOptions(opts...)
6464
if err != nil {
6565
return nil, err
@@ -183,7 +183,7 @@ func (api *ObjectAPI) Stat(ctx context.Context, path coreiface.Path) (*coreiface
183183
return out, nil
184184
}
185185

186-
func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.Path, error) {
186+
func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.ResolvedPath, error) {
187187
options, err := caopts.ObjectAddLinkOptions(opts...)
188188
if err != nil {
189189
return nil, err
@@ -224,7 +224,7 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
224224
return api.ParseCid(nnode.Cid()), nil
225225
}
226226

227-
func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Path, error) {
227+
func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.ResolvedPath, error) {
228228
baseNd, err := api.core().ResolveNode(ctx, base)
229229
if err != nil {
230230
return nil, err
@@ -250,15 +250,15 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri
250250
return api.ParseCid(nnode.Cid()), nil
251251
}
252252

253-
func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
253+
func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
254254
return api.patchData(ctx, path, r, true)
255255
}
256256

257-
func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
257+
func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
258258
return api.patchData(ctx, path, r, false)
259259
}
260260

261-
func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.Path, error) {
261+
func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.ResolvedPath, error) {
262262
nd, err := api.core().ResolveNode(ctx, path)
263263
if err != nil {
264264
return nil, err

0 commit comments

Comments
 (0)