Skip to content

Commit 4598c0e

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

File tree

18 files changed

+156
-139
lines changed

18 files changed

+156
-139
lines changed

core/coreapi/block.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ type BlockAPI struct {
2323
}
2424

2525
type BlockStat struct {
26-
path coreiface.Path
26+
path coreiface.ResolvedPath
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)
@@ -114,7 +124,12 @@ func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.Bl
114124
}
115125

116126
func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.BlockStat, error) {
117-
b, err := api.node.Blocks.GetBlock(ctx, p.Cid())
127+
rp, err := api.ResolvePath(ctx, p)
128+
if err != nil {
129+
return nil, err
130+
}
131+
132+
b, err := api.node.Blocks.GetBlock(ctx, rp.Cid())
118133
if err != nil {
119134
return nil, err
120135
}
@@ -129,6 +144,6 @@ func (bs *BlockStat) Size() int {
129144
return bs.size
130145
}
131146

132-
func (bs *BlockStat) Path() coreiface.Path {
147+
func (bs *BlockStat) Path() coreiface.ResolvedPath {
133148
return bs.path
134149
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type DagAPI struct {
2323
// Put inserts data using specified format and input encoding. Unless used with
2424
// `WithCodes` or `WithHash`, the defaults "dag-cbor" and "sha256" are used.
2525
// Returns the path of the inserted data.
26-
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) {
26+
func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.ResolvedPath, error) {
2727
settings, err := caopts.DagPutOptions(opts...)
2828
if err != nil {
2929
return nil, err

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/block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ type BlockStat interface {
1313
Size() int
1414

1515
// Path returns path to the block
16-
Path() Path
16+
Path() ResolvedPath
1717
}
1818

1919
// BlockAPI specifies the interface to the block layer
2020
type BlockAPI interface {
2121
// Put imports raw block data, hashing it using specified settings.
22-
Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
22+
Put(context.Context, io.Reader, ...options.BlockPutOption) (ResolvedPath, error)
2323

2424
// WithFormat is an option for Put which specifies the multicodec to use to
2525
// serialize the object. Default is "v0"

core/coreapi/interface/coreapi.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ package iface
55
import (
66
"context"
77

8-
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9-
108
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
119
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
1210
)
@@ -35,19 +33,15 @@ type CoreAPI interface {
3533
Object() ObjectAPI
3634

3735
// ResolvePath resolves the path using Unixfs resolver
38-
ResolvePath(context.Context, Path) (Path, error)
36+
ResolvePath(context.Context, Path) (ResolvedPath, error)
3937

4038
// ResolveNode resolves the path (if not resolved already) using Unixfs
4139
// resolver, gets and returns the resolved Node
4240
ResolveNode(context.Context, Path) (ipld.Node, error)
4341

4442
// ParsePath parses string path to a Path
45-
ParsePath(context.Context, string, ...options.ParsePathOption) (Path, error)
46-
47-
// WithResolve is an option for ParsePath which when set to true tells
48-
// ParsePath to also resolve the path
49-
WithResolve(bool) options.ParsePathOption
43+
ParsePath(context.Context, string) (Path, error)
5044

5145
// ParseCid creates new path from the provided CID
52-
ParseCid(*cid.Cid) Path
46+
ParseCid(*cid.Cid) ResolvedPath
5347
}

core/coreapi/interface/dag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type DagAPI interface {
1414
// Put inserts data using specified format and input encoding.
1515
// Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
1616
// "sha256" are used.
17-
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error)
17+
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (ResolvedPath, error)
1818

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

core/coreapi/interface/object.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type ObjectAPI interface {
4646
WithType(string) options.ObjectNewOption
4747

4848
// Put imports the data into merkledag
49-
Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)
49+
Put(context.Context, io.Reader, ...options.ObjectPutOption) (ResolvedPath, error)
5050

5151
// WithInputEnc is an option for Put which specifies the input encoding of the
5252
// data. Default is "json".
@@ -79,18 +79,18 @@ type ObjectAPI interface {
7979
// AddLink adds a link under the specified path. child path can point to a
8080
// subdirectory within the patent which must be present (can be overridden
8181
// with WithCreate option).
82-
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Path, error)
82+
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (ResolvedPath, error)
8383

8484
// WithCreate is an option for AddLink which specifies whether create required
8585
// directories for the child
8686
WithCreate(create bool) options.ObjectAddLinkOption
8787

8888
// RmLink removes a link from the node
89-
RmLink(ctx context.Context, base Path, link string) (Path, error)
89+
RmLink(ctx context.Context, base Path, link string) (ResolvedPath, error)
9090

9191
// AppendData appends data to the node
92-
AppendData(context.Context, Path, io.Reader) (Path, error)
92+
AppendData(context.Context, Path, io.Reader) (ResolvedPath, error)
9393

9494
// SetData sets the data contained in the node
95-
SetData(context.Context, Path, io.Reader) (Path, error)
95+
SetData(context.Context, Path, io.Reader) (ResolvedPath, error)
9696
}

core/coreapi/interface/options/path.go

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

core/coreapi/interface/path.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ import (
66

77
// Path is a generic wrapper for paths used in the API. A path can be resolved
88
// to a CID using one of Resolve functions in the API.
9+
// TODO: figure out/explain namespaces
910
type Path interface {
1011
// String returns the path as a string.
1112
String() string
13+
14+
// Namespace returns the first component of the path
15+
Namespace() string
16+
}
17+
18+
// ResolvedPath is a resolved Path
19+
type ResolvedPath interface {
1220
// Cid returns cid referred to by path
1321
Cid() *cid.Cid
22+
1423
// Root returns cid of root path
1524
Root() *cid.Cid
16-
// Resolved returns whether path has been fully resolved
17-
Resolved() bool
25+
26+
//TODO: Path remainder
27+
28+
Path
1829
}

0 commit comments

Comments
 (0)