coreapi: separate path into two types
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Feb 8, 2018 at 17:39 UTC
15f8fc60c0fc49044385aa5319004785002aaa73
18 files changed
+157
-140
core/coreapi/block.go
+21
-6
@@ -20,11 +20,11 @@ import (
20
type BlockAPI CoreAPI
21
22
type BlockStat struct {
23
- path coreiface.Path
23
+ path coreiface.ResolvedPath
24
size int
25
}
26
27
-func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.Path, error) {
27
+func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.ResolvedPath, error) {
28
settings, err := caopts.BlockPutOptions(opts...)
29
if err != nil {
30
return nil, err
@@ -69,7 +69,12 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
69
}
70
71
func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {
72
- b, err := api.node.Blocks.GetBlock(ctx, p.Cid())
72
+ rp, err := api.ResolvePath(ctx, p)
73
+ if err != nil {
74
+ return nil, err
75
+ }
76
+
77
+ b, err := api.node.Blocks.GetBlock(ctx, rp.Cid())
78
if err != nil {
79
return nil, err
80
}
@@ -78,11 +83,16 @@ func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, erro
83
}
84
85
func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.BlockRmOption) error {
86
+ rp, err := api.ResolvePath(ctx, p)
87
+ if err != nil {
88
+ return err
89
+ }
90
+
91
settings, err := caopts.BlockRmOptions(opts...)
92
if err != nil {
93
return err
94
}
85
- cids := []*cid.Cid{p.Cid()}
95
+ cids := []*cid.Cid{rp.Cid()}
96
o := util.RmBlocksOpts{Force: settings.Force}
97
98
out, err := util.RmBlocks(api.node.Blockstore, api.node.Pinning, cids, o)
@@ -111,7 +121,12 @@ func (api *BlockAPI) Rm(ctx context.Context, p coreiface.Path, opts ...caopts.Bl
121
}
122
123
func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.BlockStat, error) {
114
- b, err := api.node.Blocks.GetBlock(ctx, p.Cid())
124
+ rp, err := api.ResolvePath(ctx, p)
125
+ if err != nil {
126
+ return nil, err
127
+ }
128
+
129
+ b, err := api.node.Blocks.GetBlock(ctx, rp.Cid())
130
if err != nil {
131
return nil, err
132
}
@@ -126,6 +141,6 @@ func (bs *BlockStat) Size() int {
141
return bs.size
142
}
143
129
-func (bs *BlockStat) Path() coreiface.Path {
144
+func (bs *BlockStat) Path() coreiface.ResolvedPath {
145
return bs.path
146
}
core/coreapi/coreapi.go
+1
-3
@@ -16,17 +16,15 @@ package coreapi
16
import (
17
core "github.com/ipfs/go-ipfs/core"
18
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
19
- caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
19
)
20
21
type CoreAPI struct {
22
node *core.IpfsNode
24
- *caopts.ApiOptions
23
}
24
25
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
26
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
29
- api := &CoreAPI{n, nil}
27
+ api := &CoreAPI{n}
28
return api
29
}
30
core/coreapi/dag.go
+1
-1
@@ -20,7 +20,7 @@ type DagAPI CoreAPI
20
// Put inserts data using specified format and input encoding. Unless used with
21
// `WithCodes` or `WithHash`, the defaults "dag-cbor" and "sha256" are used.
22
// Returns the path of the inserted data.
23
-func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.Path, error) {
23
+func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPutOption) (coreiface.ResolvedPath, error) {
24
settings, err := caopts.DagPutOptions(opts...)
25
if err != nil {
26
return nil, err
core/coreapi/dag_test.go
+1
-1
@@ -72,7 +72,7 @@ func TestPath(t *testing.T) {
72
t.Error(err)
73
}
74
75
- p, err := api.ParsePath(ctx, path.Join(res.Cid().String(), "lnk"))
75
+ p, err := api.ParsePath(path.Join(res.Cid().String(), "lnk"))
76
if err != nil {
77
t.Error(err)
78
}
core/coreapi/interface/block.go
+2
-2
@@ -13,13 +13,13 @@ type BlockStat interface {
13
Size() int
14
15
// Path returns path to the block
16
- Path() Path
16
+ Path() ResolvedPath
17
}
18
19
// BlockAPI specifies the interface to the block layer
20
type BlockAPI interface {
21
// 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)
23
24
// Get attempts to resolve the path and return a reader for data in the block
25
Get(context.Context, Path) (io.Reader, error)
core/coreapi/interface/coreapi.go
+4
-10
@@ -5,10 +5,8 @@ package iface
5
import (
6
"context"
7
8
- options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
-
10
- cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
8
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
9
+ cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
10
)
11
12
// CoreAPI defines an unified interface to IPFS for Go programs
@@ -35,19 +33,15 @@ type CoreAPI interface {
33
Object() ObjectAPI
34
35
// ResolvePath resolves the path using Unixfs resolver
38
- ResolvePath(context.Context, Path) (Path, error)
36
+ ResolvePath(context.Context, Path) (ResolvedPath, error)
37
38
// ResolveNode resolves the path (if not resolved already) using Unixfs
39
// resolver, gets and returns the resolved Node
40
ResolveNode(context.Context, Path) (ipld.Node, error)
41
42
// 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)
44
45
// ParseCid creates new path from the provided CID
52
- ParseCid(*cid.Cid) Path
46
+ ParseCid(*cid.Cid) ResolvedPath
47
}
core/coreapi/interface/dag.go
+1
-1
@@ -14,7 +14,7 @@ type DagAPI interface {
14
// Put inserts data using specified format and input encoding.
15
// Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
16
// "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)
18
19
// Get attempts to resolve and get the node specified by the path
20
Get(ctx context.Context, path Path) (ipld.Node, error)
core/coreapi/interface/object.go
+5
-5
@@ -38,7 +38,7 @@ type ObjectAPI interface {
38
New(context.Context, ...options.ObjectNewOption) (ipld.Node, error)
39
40
// Put imports the data into merkledag
41
- Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)
41
+ Put(context.Context, io.Reader, ...options.ObjectPutOption) (ResolvedPath, error)
42
43
// Get returns the node for the path
44
Get(context.Context, Path) (ipld.Node, error)
@@ -55,14 +55,14 @@ type ObjectAPI interface {
55
// AddLink adds a link under the specified path. child path can point to a
56
// subdirectory within the patent which must be present (can be overridden
57
// with WithCreate option).
58
- AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Path, error)
58
+ AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (ResolvedPath, error)
59
60
// RmLink removes a link from the node
61
- RmLink(ctx context.Context, base Path, link string) (Path, error)
61
+ RmLink(ctx context.Context, base Path, link string) (ResolvedPath, error)
62
63
// AppendData appends data to the node
64
- AppendData(context.Context, Path, io.Reader) (Path, error)
64
+ AppendData(context.Context, Path, io.Reader) (ResolvedPath, error)
65
66
// SetData sets the data contained in the node
67
- SetData(context.Context, Path, io.Reader) (Path, error)
67
+ SetData(context.Context, Path, io.Reader) (ResolvedPath, error)
68
}
core/coreapi/interface/options/path.go
deleted
-30
@@ -1,30 +0,0 @@
1
-package options
2
-
3
-type ParsePathSettings struct {
4
- Resolve bool
5
-}
6
-
7
-type ParsePathOption func(*ParsePathSettings) error
8
-
9
-func ParsePathOptions(opts ...ParsePathOption) (*ParsePathSettings, error) {
10
- options := &ParsePathSettings{
11
- Resolve: false,
12
- }
13
-
14
- for _, opt := range opts {
15
- err := opt(options)
16
- if err != nil {
17
- return nil, err
18
- }
19
- }
20
- return options, nil
21
-}
22
-
23
-type ApiOptions struct{}
24
-
25
-func (api *ApiOptions) WithResolve(r bool) ParsePathOption {
26
- return func(settings *ParsePathSettings) error {
27
- settings.Resolve = r
28
- return nil
29
- }
30
-}
core/coreapi/interface/path.go
+13
-2
@@ -6,13 +6,24 @@ import (
6
7
// Path is a generic wrapper for paths used in the API. A path can be resolved
8
// to a CID using one of Resolve functions in the API.
9
+// TODO: figure out/explain namespaces
10
type Path interface {
11
// String returns the path as a string.
12
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 {
20
// Cid returns cid referred to by path
21
Cid() *cid.Cid
22
+
23
// Root returns cid of root path
24
Root() *cid.Cid
16
- // Resolved returns whether path has been fully resolved
17
- Resolved() bool
25
+
26
+ //TODO: Path remainder
27
+
28
+ Path
29
}
core/coreapi/interface/pin.go
+2
-2
@@ -9,7 +9,7 @@ import (
9
// Pin holds information about pinned resource
10
type Pin interface {
11
// Path to the pinned object
12
- Path() Path
12
+ Path() ResolvedPath
13
14
// Type of the pin
15
Type() string
@@ -27,7 +27,7 @@ type PinStatus interface {
27
// BadPinNode is a node that has been marked as bad by Pin.Verify
28
type BadPinNode interface {
29
// Path is the path of the node
30
- Path() Path
30
+ Path() ResolvedPath
31
32
// Err is the reason why the node has been marked as bad
33
Err() error
core/coreapi/interface/unixfs.go
+1
-1
@@ -10,7 +10,7 @@ import (
10
// UnixfsAPI is the basic interface to immutable files in IPFS
11
type UnixfsAPI interface {
12
// Add imports the data from the reader into merkledag file
13
- Add(context.Context, io.Reader) (Path, error)
13
+ Add(context.Context, io.Reader) (ResolvedPath, error)
14
15
// Cat returns a reader for the file
16
Cat(context.Context, Path) (Reader, error)
core/coreapi/object.go
+6
-6
@@ -56,7 +56,7 @@ func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (
56
return n, nil
57
}
58
59
-func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.Path, error) {
59
+func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.ResolvedPath, error) {
60
options, err := caopts.ObjectPutOptions(opts...)
61
if err != nil {
62
return nil, err
@@ -180,7 +180,7 @@ func (api *ObjectAPI) Stat(ctx context.Context, path coreiface.Path) (*coreiface
180
return out, nil
181
}
182
183
-func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.Path, error) {
183
+func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.ResolvedPath, error) {
184
options, err := caopts.ObjectAddLinkOptions(opts...)
185
if err != nil {
186
return nil, err
@@ -221,7 +221,7 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
221
return api.ParseCid(nnode.Cid()), nil
222
}
223
224
-func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Path, error) {
224
+func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.ResolvedPath, error) {
225
baseNd, err := api.core().ResolveNode(ctx, base)
226
if err != nil {
227
return nil, err
@@ -247,15 +247,15 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri
247
return api.ParseCid(nnode.Cid()), nil
248
}
249
250
-func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
250
+func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
251
return api.patchData(ctx, path, r, true)
252
}
253
254
-func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
254
+func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
255
return api.patchData(ctx, path, r, false)
256
}
257
258
-func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.Path, error) {
258
+func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.ResolvedPath, error) {
259
nd, err := api.core().ResolveNode(ctx, path)
260
if err != nil {
261
return nil, err
core/coreapi/path.go
+39
-44
@@ -5,7 +5,6 @@ import (
5
6
core "github.com/ipfs/go-ipfs/core"
7
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
8
- caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
8
namesys "github.com/ipfs/go-ipfs/namesys"
9
ipfspath "github.com/ipfs/go-ipfs/path"
10
resolver "github.com/ipfs/go-ipfs/path/resolver"
@@ -15,35 +14,51 @@ import (
14
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
15
)
16
17
+// path implements coreiface.Path
18
+type path struct {
19
+ path ipfspath.Path
20
+}
21
+
22
+// resolvedPath implements coreiface.resolvedPath
23
+type resolvedPath struct {
24
+ path
25
+ cid *cid.Cid
26
+ root *cid.Cid
27
+}
28
+
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}
32
+}
33
+
34
// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
35
// resolved Node.
36
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
37
return resolveNode(ctx, api.node.DAG, api.node.Namesys, p)
38
}
39
40
+// ResolvePath resolves the path `p` using Unixfs resolver, returns the
41
+// resolved path.
42
+func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.ResolvedPath, error) {
43
+ return resolvePath(ctx, api.node.DAG, api.node.Namesys, p)
44
+}
45
+
46
func resolveNode(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (ipld.Node, error) {
25
- p, err := resolvePath(ctx, ng, nsys, p)
47
+ rp, err := resolvePath(ctx, ng, nsys, p)
48
if err != nil {
49
return nil, err
50
}
51
30
- node, err := ng.Get(ctx, p.Cid())
52
+ node, err := ng.Get(ctx, rp.Cid())
53
if err != nil {
54
return nil, err
55
}
56
return node, nil
57
}
58
37
-// ResolvePath resolves the path `p` using Unixfs resolver, returns the
38
-// resolved path.
39
-// TODO: store all of ipfspath.Resolver.ResolvePathComponents() in Path
40
-func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.Path, error) {
41
- return resolvePath(ctx, api.node.DAG, api.node.Namesys, p)
42
-}
43
-
44
-func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (coreiface.Path, error) {
45
- if p.Resolved() {
46
- return p, nil
59
+func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (coreiface.ResolvedPath, error) {
60
+ if _, ok := p.(coreiface.ResolvedPath); ok {
61
+ return p.(coreiface.ResolvedPath), nil
62
}
63
64
r := &resolver.Resolver{
@@ -64,46 +79,26 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
79
root = node.Cid()
80
}
81
67
- return ResolvedPath(p.String(), node.Cid(), root), nil
68
-}
69
-
70
-// Implements coreiface.Path
71
-type path struct {
72
- path ipfspath.Path
73
- cid *cid.Cid
74
- root *cid.Cid
82
+ return &resolvedPath{path: path{p2}, cid: node.Cid(), root: root}, nil
83
}
84
85
// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
78
-func (api *CoreAPI) ParsePath(ctx context.Context, p string, opts ...caopts.ParsePathOption) (coreiface.Path, error) {
79
- options, err := caopts.ParsePathOptions(opts...)
80
- if err != nil {
81
- return nil, err
82
- }
83
-
86
+func (api *CoreAPI) ParsePath(ctx context.Context, p string) (coreiface.Path, error) {
87
pp, err := ipfspath.ParsePath(p)
88
if err != nil {
89
return nil, err
90
}
91
89
- res := &path{path: pp}
90
- if options.Resolve {
91
- return api.ResolvePath(ctx, res)
92
- }
93
- return res, nil
92
+ return &path{path: pp}, nil
93
}
94
96
-// ParseCid parses the path from `c`, retruns the parsed path.
97
-func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.Path {
98
- return &path{path: ipfspath.FromCid(c), cid: c, root: c}
99
-}
100
-
101
-// ResolvePath parses path from string `p`, returns parsed path.
102
-func ResolvedPath(p string, c *cid.Cid, r *cid.Cid) coreiface.Path {
103
- return &path{path: ipfspath.FromString(p), cid: c, root: r}
95
+func (p *path) String() string { return p.path.String() }
96
+func (p *path) Namespace() string {
97
+ if len(p.path.Segments()) < 1 {
98
+ return ""
99
+ }
100
+ return p.path.Segments()[0]
101
}
102
106
-func (p *path) String() string { return p.path.String() }
107
-func (p *path) Cid() *cid.Cid { return p.cid }
108
-func (p *path) Root() *cid.Cid { return p.root }
109
-func (p *path) Resolved() bool { return p.cid != nil }
103
+func (p *resolvedPath) Cid() *cid.Cid { return p.cid }
104
+func (p *resolvedPath) Root() *cid.Cid { return p.root }
core/coreapi/pin.go
+17
-7
@@ -62,7 +62,17 @@ func (api *PinAPI) Update(ctx context.Context, from coreiface.Path, to coreiface
62
return err
63
}
64
65
- return api.node.Pinning.Update(ctx, from.Cid(), to.Cid(), settings.Unpin)
65
+ fp, err := api.ResolvePath(ctx, from)
66
+ if err != nil {
67
+ return err
68
+ }
69
+
70
+ tp, err := api.ResolvePath(ctx, to)
71
+ if err != nil {
72
+ return err
73
+ }
74
+
75
+ return api.node.Pinning.Update(ctx, fp.Cid(), tp.Cid(), settings.Unpin)
76
}
77
78
type pinStatus struct {
@@ -73,7 +83,7 @@ type pinStatus struct {
83
84
// BadNode is used in PinVerifyRes
85
type badNode struct {
76
- path coreiface.Path
86
+ path coreiface.ResolvedPath
87
err error
88
}
89
@@ -85,7 +95,7 @@ func (s *pinStatus) BadNodes() []coreiface.BadPinNode {
95
return s.badNodes
96
}
97
88
-func (n *badNode) Path() coreiface.Path {
98
+func (n *badNode) Path() coreiface.ResolvedPath {
99
return n.path
100
}
101
@@ -141,11 +151,11 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
151
152
type pinInfo struct {
153
pinType string
144
- object coreiface.Path
154
+ path coreiface.ResolvedPath
155
}
156
147
-func (p *pinInfo) Path() coreiface.Path {
148
- return p.object
157
+func (p *pinInfo) Path() coreiface.ResolvedPath {
158
+ return p.path
159
}
160
161
func (p *pinInfo) Type() string {
@@ -160,7 +170,7 @@ func (api *PinAPI) pinLsAll(typeStr string, ctx context.Context) ([]coreiface.Pi
170
for _, c := range keyList {
171
keys[c.String()] = &pinInfo{
172
pinType: typeStr,
163
- object: api.ParseCid(c),
173
+ path: api.ParseCid(c),
174
}
175
}
176
}
core/coreapi/unixfs.go
+1
-1
@@ -16,7 +16,7 @@ type UnixfsAPI CoreAPI
16
17
// Add builds a merkledag node from a reader, adds it to the blockstore,
18
// and returns the key representing that node.
19
-func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.Path, error) {
19
+func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.ResolvedPath, error) {
20
k, err := coreunix.AddWithContext(ctx, api.node, r)
21
if err != nil {
22
return nil, err
core/coreapi/unixfs_test.go
+41
-17
@@ -29,14 +29,11 @@ import (
29
const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe"
30
31
// `echo -n 'hello, world!' | ipfs add`
32
-var hello = coreapi.ResolvedPath("/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk", nil, nil)
32
+var hello = "/ipfs/QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk"
33
var helloStr = "hello, world!"
34
35
-// `ipfs object new unixfs-dir`
36
-var emptyDir = coreapi.ResolvedPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", nil, nil)
37
-
35
// `echo -n | ipfs add`
39
-var emptyFile = coreapi.ResolvedPath("/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", nil, nil)
36
+var emptyFile = "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
37
38
func makeAPIIdent(ctx context.Context, fullIdentity bool) (*core.IpfsNode, coreiface.CoreAPI, error) {
39
var ident config.Identity
@@ -98,11 +95,11 @@ func TestAdd(t *testing.T) {
95
t.Error(err)
96
}
97
101
- if p.String() != hello.String() {
98
+ if p.String() != hello {
99
t.Fatalf("expected path %s, got: %s", hello, p)
100
}
101
105
- r, err := api.Unixfs().Cat(ctx, hello)
102
+ r, err := api.Unixfs().Cat(ctx, p)
103
if err != nil {
104
t.Fatal(err)
105
}
@@ -130,7 +127,7 @@ func TestAddEmptyFile(t *testing.T) {
127
t.Error(err)
128
}
129
133
- if p.String() != emptyFile.String() {
130
+ if p.String() != emptyFile {
131
t.Fatalf("expected path %s, got: %s", hello, p)
132
}
133
}
@@ -149,11 +146,16 @@ func TestCatBasic(t *testing.T) {
146
}
147
p = "/ipfs/" + p
148
152
- if p != hello.String() {
149
+ if p != hello {
150
t.Fatalf("expected CID %s, got: %s", hello, p)
151
}
152
156
- r, err := api.Unixfs().Cat(ctx, hello)
153
+ helloPath, err := api.ParsePath(hello)
154
+ if err != nil {
155
+ t.Fatal(err)
156
+ }
157
+
158
+ r, err := api.Unixfs().Cat(ctx, helloPath)
159
if err != nil {
160
t.Fatal(err)
161
}
@@ -180,7 +182,12 @@ func TestCatEmptyFile(t *testing.T) {
182
t.Fatal(err)
183
}
184
183
- r, err := api.Unixfs().Cat(ctx, emptyFile)
185
+ emptyFilePath, err := api.ParsePath(emptyFile)
186
+ if err != nil {
187
+ t.Fatal(err)
188
+ }
189
+
190
+ r, err := api.Unixfs().Cat(ctx, emptyFilePath)
191
if err != nil {
192
t.Fatal(err)
193
}
@@ -208,11 +215,16 @@ func TestCatDir(t *testing.T) {
215
}
216
p := api.ParseCid(edir.Cid())
217
211
- if p.String() != emptyDir.String() {
212
- t.Fatalf("expected path %s, got: %s", emptyDir, p)
218
+ emptyDir, err := api.Object().New(ctx, api.Object().WithType("unixfs-dir"))
219
+ if err != nil {
220
+ t.Error(err)
221
}
222
215
- _, err = api.Unixfs().Cat(ctx, emptyDir)
223
+ if p.String() != api.ParseCid(emptyDir.Cid()).String() {
224
+ t.Fatalf("expected path %s, got: %s", emptyDir.Cid(), p.String())
225
+ }
226
+
227
+ _, err = api.Unixfs().Cat(ctx, api.ParseCid(emptyDir.Cid()))
228
if err != coreiface.ErrIsDir {
229
t.Fatalf("expected ErrIsDir, got: %s", err)
230
}
@@ -244,7 +256,11 @@ func TestCatOffline(t *testing.T) {
256
t.Error(err)
257
}
258
247
- _, err = api.Unixfs().Cat(ctx, coreapi.ResolvedPath("/ipns/Qmfoobar", nil, nil))
259
+ p, err := api.ParsePath("/ipns/Qmfoobar")
260
+ if err != nil {
261
+ t.Error(err)
262
+ }
263
+ _, err = api.Unixfs().Cat(ctx, p)
264
if err != coreiface.ErrOffline {
265
t.Fatalf("expected ErrOffline, got: %s", err)
266
}
@@ -266,7 +282,10 @@ func TestLs(t *testing.T) {
282
if len(parts) != 2 {
283
t.Errorf("unexpected path: %s", k)
284
}
269
- p := coreapi.ResolvedPath("/ipfs/"+parts[0], nil, nil)
285
+ p, err := api.ParsePath("/ipfs/" + parts[0])
286
+ if err != nil {
287
+ t.Error(err)
288
+ }
289
290
links, err := api.Unixfs().Ls(ctx, p)
291
if err != nil {
@@ -299,7 +318,12 @@ func TestLsEmptyDir(t *testing.T) {
318
t.Error(err)
319
}
320
302
- links, err := api.Unixfs().Ls(ctx, emptyDir)
321
+ emptyDir, err := api.Object().New(ctx, api.Object().WithType("unixfs-dir"))
322
+ if err != nil {
323
+ t.Error(err)
324
+ }
325
+
326
+ links, err := api.Unixfs().Ls(ctx, api.ParseCid(emptyDir.Cid()))
327
if err != nil {
328
t.Error(err)
329
}
path/path.go
+1
-1
@@ -120,7 +120,7 @@ func ParsePath(txt string) (Path, error) {
120
if _, err := ParseCidToPath(parts[2]); err != nil {
121
return "", err
122
}
123
- } else if parts[1] != "ipns" {
123
+ } else if parts[1] != "ipns" && parts[1] != "ipld" { //TODO: make this smarter
124
return "", ErrBadPath
125
}
126