Skip to content

Commit 7adf1cb

Browse files
committed
coreapi: move path utils to interface
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent 082498d commit 7adf1cb

File tree

14 files changed

+128
-120
lines changed

14 files changed

+128
-120
lines changed

core/coreapi/block.go

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

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

7171
func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {
@@ -132,7 +132,7 @@ func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.Bloc
132132
}
133133

134134
return &BlockStat{
135-
path: api.core().IpldPath(b.Cid()),
135+
path: coreiface.IpldPath(b.Cid()),
136136
size: len(b.RawData()),
137137
}, nil
138138
}

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.core().IpldPath(nds[0].Cid()), nil
47+
return coreiface.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.core().ParsePath(gopath.Join(p.String(), p2))
69+
out[n], err = coreiface.ParsePath(gopath.Join(p.String(), p2))
7070
if err != nil {
7171
return nil, err
7272
}

core/coreapi/dag_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"strings"
77
"testing"
88

9-
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
10-
9+
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
1110
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11+
12+
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
1213
)
1314

1415
var (
@@ -72,7 +73,7 @@ func TestPath(t *testing.T) {
7273
t.Error(err)
7374
}
7475

75-
p, err := api.ParsePath(path.Join(res.Cid().String(), "lnk"))
76+
p, err := coreiface.ParsePath(path.Join(res.Cid().String(), "lnk"))
7677
if err != nil {
7778
t.Error(err)
7879
}

core/coreapi/interface/coreapi.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"context"
77

88
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
9-
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
109
)
1110

1211
// CoreAPI defines an unified interface to IPFS for Go programs
@@ -38,13 +37,4 @@ type CoreAPI interface {
3837
// ResolveNode resolves the path (if not resolved already) using Unixfs
3938
// resolver, gets and returns the resolved Node
4039
ResolveNode(context.Context, Path) (ipld.Node, error)
41-
42-
// ParsePath parses string path to a Path
43-
ParsePath(string) (Path, error)
44-
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
5040
}

core/coreapi/interface/path.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package iface
22

33
import (
4+
ipfspath "github.com/ipfs/go-ipfs/path"
5+
46
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
57
)
68

9+
//TODO: merge with ipfspath so we don't depend on it
10+
711
// Path is a generic wrapper for paths used in the API. A path can be resolved
812
// to a CID using one of Resolve functions in the API.
913
//
@@ -87,3 +91,86 @@ type ResolvedPath interface {
8791

8892
Path
8993
}
94+
95+
// path implements coreiface.Path
96+
type path struct {
97+
path ipfspath.Path
98+
}
99+
100+
// resolvedPath implements coreiface.resolvedPath
101+
type resolvedPath struct {
102+
path
103+
cid *cid.Cid
104+
root *cid.Cid
105+
remainder string
106+
}
107+
108+
// IpfsPath creates new /ipfs path from the provided CID
109+
func IpfsPath(c *cid.Cid) ResolvedPath {
110+
return &resolvedPath{
111+
path: path{ipfspath.Path("/ipfs/" + c.String())},
112+
cid: c,
113+
root: c,
114+
remainder: "",
115+
}
116+
}
117+
118+
// IpldPath creates new /ipld path from the provided CID
119+
func IpldPath(c *cid.Cid) ResolvedPath {
120+
return &resolvedPath{
121+
path: path{ipfspath.Path("/ipld/" + c.String())},
122+
cid: c,
123+
root: c,
124+
remainder: "",
125+
}
126+
}
127+
128+
// ParsePath parses string path to a Path
129+
func ParsePath(p string) (Path, error) {
130+
pp, err := ipfspath.ParsePath(p)
131+
if err != nil {
132+
return nil, err
133+
}
134+
135+
return &path{path: pp}, nil
136+
}
137+
138+
// NewResolvedPath creates new ResolvedPath. This function performs no checks
139+
// and is intended to be used by resolver implementations. Incorrect inputs may
140+
// cause panics. Handle with care.
141+
func NewResolvedPath(ipath ipfspath.Path, c *cid.Cid, root *cid.Cid, remainder string) ResolvedPath {
142+
return &resolvedPath{
143+
path: path{ipath},
144+
cid: c,
145+
root: root,
146+
remainder: remainder,
147+
}
148+
}
149+
150+
func (p *path) String() string {
151+
return p.path.String()
152+
}
153+
154+
func (p *path) Namespace() string {
155+
if len(p.path.Segments()) < 1 {
156+
panic("path without namespace") //this shouldn't happen under any scenario
157+
}
158+
return p.path.Segments()[0]
159+
}
160+
161+
func (p *path) Mutable() bool {
162+
//TODO: MFS: check for /local
163+
return p.Namespace() == "ipns"
164+
}
165+
166+
func (p *resolvedPath) Cid() *cid.Cid {
167+
return p.cid
168+
}
169+
170+
func (p *resolvedPath) Root() *cid.Cid {
171+
return p.root
172+
}
173+
174+
func (p *resolvedPath) Remainder() string {
175+
return p.remainder
176+
}

core/coreapi/key.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ func (k *key) Name() string {
2828

2929
// Path returns the path of the key.
3030
func (k *key) Path() coreiface.Path {
31-
return &path{path: ipfspath.FromString(ipfspath.Join([]string{"/ipns", k.peerId}))}
31+
path, err := coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerId}))
32+
if err != nil {
33+
panic("error parsing path: " + err.Error())
34+
}
35+
36+
return path
3237
}
3338

3439
// Generate generates new key, stores it in the keystore under the specified

core/coreapi/name.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
129129
return nil, err
130130
}
131131

132-
return &path{path: output}, nil
132+
return coreiface.ParsePath(output.String())
133133
}
134134

135135
func keylookup(n *core.IpfsNode, k string) (crypto.PrivKey, error) {

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.core().IpfsPath(dagnode.Cid()), nil
124+
return coreiface.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.core().IpfsPath(nnode.Cid()), nil
221+
return coreiface.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.core().IpfsPath(nnode.Cid()), nil
247+
return coreiface.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.core().IpfsPath(pbnd.Cid()), nil
284+
return coreiface.IpfsPath(pbnd.Cid()), nil
285285
}
286286

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

core/coreapi/path.go

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,6 @@ import (
1616
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
1717
)
1818

19-
// path implements coreiface.Path
20-
type path struct {
21-
path ipfspath.Path
22-
}
23-
24-
// resolvedPath implements coreiface.resolvedPath
25-
type resolvedPath struct {
26-
path
27-
cid *cid.Cid
28-
root *cid.Cid
29-
remainder string
30-
}
31-
32-
// IpfsPath parses the path from `c`, reruns the parsed path.
33-
func (api *CoreAPI) IpfsPath(c *cid.Cid) coreiface.ResolvedPath {
34-
return &resolvedPath{
35-
path: path{ipfspath.Path("/ipfs/" + c.String())},
36-
cid: c,
37-
root: c,
38-
remainder: "",
39-
}
40-
}
41-
42-
// IpldPath parses the path from `c`, reruns the parsed path.
43-
func (api *CoreAPI) IpldPath(c *cid.Cid) coreiface.ResolvedPath {
44-
return &resolvedPath{
45-
path: path{ipfspath.Path("/ipld/" + c.String())},
46-
cid: c,
47-
root: c,
48-
remainder: "",
49-
}
50-
}
51-
5219
// ResolveNode resolves the path `p` using Unixfs resolver, gets and returns the
5320
// resolved Node.
5421
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
@@ -79,7 +46,7 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
7946
return p.(coreiface.ResolvedPath), nil
8047
}
8148

82-
ipath := p.(*path).path
49+
ipath := ipfspath.Path(p.String())
8350
ipath, err := core.ResolveIPNS(ctx, nsys, ipath)
8451
if err == core.ErrNoNamesys {
8552
return nil, coreiface.ErrOffline
@@ -113,48 +80,5 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
11380
return nil, err
11481
}
11582

116-
return &resolvedPath{
117-
path: path{ipath},
118-
cid: node.Cid(),
119-
root: root,
120-
remainder: gopath.Join(rest...),
121-
}, nil
122-
}
123-
124-
// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
125-
func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) {
126-
pp, err := ipfspath.ParsePath(p)
127-
if err != nil {
128-
return nil, err
129-
}
130-
131-
return &path{path: pp}, nil
132-
}
133-
134-
func (p *path) String() string {
135-
return p.path.String()
136-
}
137-
138-
func (p *path) Namespace() string {
139-
if len(p.path.Segments()) < 1 {
140-
panic("path without namespace") //this shouldn't happen under any scenario
141-
}
142-
return p.path.Segments()[0]
143-
}
144-
145-
func (p *path) Mutable() bool {
146-
//TODO: MFS: check for /local
147-
return p.Namespace() == "ipns"
148-
}
149-
150-
func (p *resolvedPath) Cid() *cid.Cid {
151-
return p.cid
152-
}
153-
154-
func (p *resolvedPath) Root() *cid.Cid {
155-
return p.root
156-
}
157-
158-
func (p *resolvedPath) Remainder() string {
159-
return p.remainder
83+
return coreiface.NewResolvedPath(ipath, node.Cid(), root, gopath.Join(rest...)), nil
16084
}

core/coreapi/path_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"testing"
77

8+
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
89
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
910
)
1011

@@ -47,7 +48,7 @@ func TestPathRemainder(t *testing.T) {
4748
t.Fatal(err)
4849
}
4950

50-
p1, err := api.ParsePath(obj.String() + "/foo/bar")
51+
p1, err := coreiface.ParsePath(obj.String() + "/foo/bar")
5152
if err != nil {
5253
t.Error(err)
5354
}
@@ -78,7 +79,7 @@ func TestEmptyPathRemainder(t *testing.T) {
7879
t.Error("expected the resolved path to not have a remainder")
7980
}
8081

81-
p1, err := api.ParsePath(obj.String())
82+
p1, err := coreiface.ParsePath(obj.String())
8283
if err != nil {
8384
t.Error(err)
8485
}
@@ -105,7 +106,7 @@ func TestInvalidPathRemainder(t *testing.T) {
105106
t.Fatal(err)
106107
}
107108

108-
p1, err := api.ParsePath(obj.String() + "/bar/baz")
109+
p1, err := coreiface.ParsePath(obj.String() + "/bar/baz")
109110
if err != nil {
110111
t.Error(err)
111112
}
@@ -133,7 +134,7 @@ func TestPathRoot(t *testing.T) {
133134
t.Fatal(err)
134135
}
135136

136-
p1, err := api.ParsePath(obj.String() + "/foo")
137+
p1, err := coreiface.ParsePath(obj.String() + "/foo")
137138
if err != nil {
138139
t.Error(err)
139140
}

0 commit comments

Comments
 (0)