Skip to content

Commit 00b2c9a

Browse files
authored
Merge pull request ipfs/interface-go-ipfs-core#22 from ipfs/feat/drop-path-err
path: drop error from ParsePath This commit was moved from ipfs/interface-go-ipfs-core@7786158
2 parents fea9afd + fbc9ab8 commit 00b2c9a

15 files changed

+132
-139
lines changed

coreiface/block.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package iface
22

33
import (
44
"context"
5+
path "github.com/ipfs/interface-go-ipfs-core/path"
56
"io"
67

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

1011
// BlockStat contains information about a block
@@ -13,7 +14,7 @@ type BlockStat interface {
1314
Size() int
1415

1516
// Path returns path to the block
16-
Path() ResolvedPath
17+
Path() path.Resolved
1718
}
1819

1920
// BlockAPI specifies the interface to the block layer
@@ -22,15 +23,15 @@ type BlockAPI interface {
2223
Put(context.Context, io.Reader, ...options.BlockPutOption) (BlockStat, error)
2324

2425
// Get attempts to resolve the path and return a reader for data in the block
25-
Get(context.Context, Path) (io.Reader, error)
26+
Get(context.Context, path.Path) (io.Reader, error)
2627

2728
// Rm removes the block specified by the path from local blockstore.
2829
// By default an error will be returned if the block can't be found locally.
2930
//
3031
// NOTE: If the specified block is pinned it won't be removed and no error
3132
// will be returned
32-
Rm(context.Context, Path, ...options.BlockRmOption) error
33+
Rm(context.Context, path.Path, ...options.BlockRmOption) error
3334

3435
// Stat returns information on
35-
Stat(context.Context, Path) (BlockStat, error)
36+
Stat(context.Context, path.Path) (BlockStat, error)
3637
}

coreiface/coreapi.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package iface
44

55
import (
66
"context"
7+
path "github.com/ipfs/interface-go-ipfs-core/path"
78

89
"github.com/ipfs/interface-go-ipfs-core/options"
910

@@ -43,11 +44,11 @@ type CoreAPI interface {
4344
PubSub() PubSubAPI
4445

4546
// ResolvePath resolves the path using Unixfs resolver
46-
ResolvePath(context.Context, Path) (ResolvedPath, error)
47+
ResolvePath(context.Context, path.Path) (path.Resolved, error)
4748

4849
// ResolveNode resolves the path (if not resolved already) using Unixfs
4950
// resolver, gets and returns the resolved Node
50-
ResolveNode(context.Context, Path) (ipld.Node, error)
51+
ResolveNode(context.Context, path.Path) (ipld.Node, error)
5152

5253
// WithOptions creates new instance of CoreAPI based on this instance with
5354
// a set of options applied

coreiface/dht.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package iface
22

33
import (
44
"context"
5+
path "github.com/ipfs/interface-go-ipfs-core/path"
56

67
"github.com/ipfs/interface-go-ipfs-core/options"
78

8-
peer "github.com/libp2p/go-libp2p-peer"
9+
"github.com/libp2p/go-libp2p-peer"
910
pstore "github.com/libp2p/go-libp2p-peerstore"
1011
)
1112

@@ -19,8 +20,8 @@ type DhtAPI interface {
1920

2021
// FindProviders finds peers in the DHT who can provide a specific value
2122
// given a key.
22-
FindProviders(context.Context, Path, ...options.DhtFindProvidersOption) (<-chan pstore.PeerInfo, error)
23+
FindProviders(context.Context, path.Path, ...options.DhtFindProvidersOption) (<-chan pstore.PeerInfo, error)
2324

2425
// Provide announces to the network that you are providing given values
25-
Provide(context.Context, Path, ...options.DhtProvideOption) error
26+
Provide(context.Context, path.Path, ...options.DhtProvideOption) error
2627
}

coreiface/key.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package iface
22

33
import (
44
"context"
5+
path "github.com/ipfs/interface-go-ipfs-core/path"
56

6-
options "github.com/ipfs/interface-go-ipfs-core/options"
7+
"github.com/ipfs/interface-go-ipfs-core/options"
78

89
"github.com/libp2p/go-libp2p-peer"
910
)
@@ -14,7 +15,7 @@ type Key interface {
1415
Name() string
1516

1617
// Path returns key path
17-
Path() Path
18+
Path() path.Path
1819

1920
// ID returns key PeerID
2021
ID() peer.ID

coreiface/name.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package iface
33
import (
44
"context"
55
"errors"
6+
path "github.com/ipfs/interface-go-ipfs-core/path"
67

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

1011
var ErrResolveFailed = errors.New("could not resolve name")
@@ -14,11 +15,11 @@ type IpnsEntry interface {
1415
// Name returns IpnsEntry name
1516
Name() string
1617
// Value returns IpnsEntry value
17-
Value() Path
18+
Value() path.Path
1819
}
1920

2021
type IpnsResult struct {
21-
Path
22+
path.Path
2223
Err error
2324
}
2425

@@ -32,10 +33,10 @@ type IpnsResult struct {
3233
// You can use .Key API to list and generate more names and their respective keys.
3334
type NameAPI interface {
3435
// Publish announces new IPNS name
35-
Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (IpnsEntry, error)
36+
Publish(ctx context.Context, path path.Path, opts ...options.NamePublishOption) (IpnsEntry, error)
3637

3738
// Resolve attempts to resolve the newest version of the specified name
38-
Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)
39+
Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (path.Path, error)
3940

4041
// Search is a version of Resolve which outputs paths as they are discovered,
4142
// reducing the time to first entry

coreiface/object.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package iface
22

33
import (
44
"context"
5+
path "github.com/ipfs/interface-go-ipfs-core/path"
56
"io"
67

7-
options "github.com/ipfs/interface-go-ipfs-core/options"
8+
"github.com/ipfs/interface-go-ipfs-core/options"
89

9-
cid "github.com/ipfs/go-cid"
10+
"github.com/ipfs/go-cid"
1011
ipld "github.com/ipfs/go-ipld-format"
1112
)
1213

@@ -58,11 +59,11 @@ type ObjectChange struct {
5859

5960
// Before holds the link path before the change. Note that when a link is
6061
// added, this will be nil.
61-
Before ResolvedPath
62+
Before path.Resolved
6263

6364
// After holds the link path after the change. Note that when a link is
6465
// removed, this will be nil.
65-
After ResolvedPath
66+
After path.Resolved
6667
}
6768

6869
// ObjectAPI specifies the interface to MerkleDAG and contains useful utilities
@@ -72,35 +73,35 @@ type ObjectAPI interface {
7273
New(context.Context, ...options.ObjectNewOption) (ipld.Node, error)
7374

7475
// Put imports the data into merkledag
75-
Put(context.Context, io.Reader, ...options.ObjectPutOption) (ResolvedPath, error)
76+
Put(context.Context, io.Reader, ...options.ObjectPutOption) (path.Resolved, error)
7677

7778
// Get returns the node for the path
78-
Get(context.Context, Path) (ipld.Node, error)
79+
Get(context.Context, path.Path) (ipld.Node, error)
7980

8081
// Data returns reader for data of the node
81-
Data(context.Context, Path) (io.Reader, error)
82+
Data(context.Context, path.Path) (io.Reader, error)
8283

8384
// Links returns lint or links the node contains
84-
Links(context.Context, Path) ([]*ipld.Link, error)
85+
Links(context.Context, path.Path) ([]*ipld.Link, error)
8586

8687
// Stat returns information about the node
87-
Stat(context.Context, Path) (*ObjectStat, error)
88+
Stat(context.Context, path.Path) (*ObjectStat, error)
8889

8990
// AddLink adds a link under the specified path. child path can point to a
9091
// subdirectory within the patent which must be present (can be overridden
9192
// with WithCreate option).
92-
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (ResolvedPath, error)
93+
AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...options.ObjectAddLinkOption) (path.Resolved, error)
9394

9495
// RmLink removes a link from the node
95-
RmLink(ctx context.Context, base Path, link string) (ResolvedPath, error)
96+
RmLink(ctx context.Context, base path.Path, link string) (path.Resolved, error)
9697

9798
// AppendData appends data to the node
98-
AppendData(context.Context, Path, io.Reader) (ResolvedPath, error)
99+
AppendData(context.Context, path.Path, io.Reader) (path.Resolved, error)
99100

100101
// SetData sets the data contained in the node
101-
SetData(context.Context, Path, io.Reader) (ResolvedPath, error)
102+
SetData(context.Context, path.Path, io.Reader) (path.Resolved, error)
102103

103104
// Diff returns a set of changes needed to transform the first object into the
104105
// second.
105-
Diff(context.Context, Path, Path) ([]ObjectChange, error)
106+
Diff(context.Context, path.Path, path.Path) ([]ObjectChange, error)
106107
}

coreiface/path.go coreiface/path/path.go

+44-27
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package iface
1+
package path
22

33
import (
4-
"github.com/ipfs/go-cid"
4+
"strings"
5+
6+
cid "github.com/ipfs/go-cid"
57
ipfspath "github.com/ipfs/go-path"
68
)
79

8-
//TODO: merge with ipfspath so we don't depend on it
9-
1010
// Path is a generic wrapper for paths used in the API. A path can be resolved
1111
// to a CID using one of Resolve functions in the API.
1212
//
@@ -23,17 +23,25 @@ type Path interface {
2323
// Namespace returns the first component of the path.
2424
//
2525
// For example path "/ipfs/QmHash", calling Namespace() will return "ipfs"
26+
//
27+
// Calling this method on invalid paths (IsValid() != nil) will result in
28+
// empty string
2629
Namespace() string
2730

2831
// Mutable returns false if the data pointed to by this path in guaranteed
2932
// to not change.
3033
//
3134
// Note that resolved mutable path can be immutable.
3235
Mutable() bool
36+
37+
// IsValid checks if this path is a valid ipfs Path, returning nil iff it is
38+
// valid
39+
IsValid() error
3340
}
3441

35-
// ResolvedPath is a path which was resolved to the last resolvable node
36-
type ResolvedPath interface {
42+
// Resolved is a path which was resolved to the last resolvable node.
43+
// ResolvedPaths are guaranteed to return nil from `IsValid`
44+
type Resolved interface {
3745
// Cid returns the CID of the node referenced by the path. Remainder of the
3846
// path is guaranteed to be within the node.
3947
//
@@ -94,7 +102,7 @@ type ResolvedPath interface {
94102

95103
// path implements coreiface.Path
96104
type path struct {
97-
path ipfspath.Path
105+
path string
98106
}
99107

100108
// resolvedPath implements coreiface.resolvedPath
@@ -107,68 +115,77 @@ type resolvedPath struct {
107115

108116
// Join appends provided segments to the base path
109117
func Join(base Path, a ...string) Path {
110-
s := ipfspath.Join(append([]string{base.String()}, a...))
111-
return &path{path: ipfspath.FromString(s)}
118+
s := strings.Join(append([]string{base.String()}, a...), "/")
119+
return &path{path: s}
112120
}
113121

114122
// IpfsPath creates new /ipfs path from the provided CID
115-
func IpfsPath(c cid.Cid) ResolvedPath {
123+
func IpfsPath(c cid.Cid) Resolved {
116124
return &resolvedPath{
117-
path: path{ipfspath.Path("/ipfs/" + c.String())},
125+
path: path{"/ipfs/" + c.String()},
118126
cid: c,
119127
root: c,
120128
remainder: "",
121129
}
122130
}
123131

124132
// IpldPath creates new /ipld path from the provided CID
125-
func IpldPath(c cid.Cid) ResolvedPath {
133+
func IpldPath(c cid.Cid) Resolved {
126134
return &resolvedPath{
127-
path: path{ipfspath.Path("/ipld/" + c.String())},
135+
path: path{"/ipld/" + c.String()},
128136
cid: c,
129137
root: c,
130138
remainder: "",
131139
}
132140
}
133141

134-
// ParsePath parses string path to a Path
135-
func ParsePath(p string) (Path, error) {
136-
pp, err := ipfspath.ParsePath(p)
137-
if err != nil {
138-
return nil, err
142+
// New parses string path to a Path
143+
func New(p string) Path {
144+
if pp, err := ipfspath.ParsePath(p); err == nil {
145+
p = pp.String()
139146
}
140147

141-
return &path{path: pp}, nil
148+
return &path{path: p}
142149
}
143150

144-
// NewResolvedPath creates new ResolvedPath. This function performs no checks
151+
// NewResolvedPath creates new Resolved path. This function performs no checks
145152
// and is intended to be used by resolver implementations. Incorrect inputs may
146153
// cause panics. Handle with care.
147-
func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder string) ResolvedPath {
154+
func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder string) Resolved {
148155
return &resolvedPath{
149-
path: path{ipath},
156+
path: path{ipath.String()},
150157
cid: c,
151158
root: root,
152159
remainder: remainder,
153160
}
154161
}
155162

156163
func (p *path) String() string {
157-
return p.path.String()
164+
return p.path
158165
}
159166

160167
func (p *path) Namespace() string {
161-
if len(p.path.Segments()) < 1 {
162-
panic("path without namespace") //this shouldn't happen under any scenario
168+
ip, err := ipfspath.ParsePath(p.path)
169+
if err != nil {
170+
return ""
171+
}
172+
173+
if len(ip.Segments()) < 1 {
174+
panic("path without namespace") // this shouldn't happen under any scenario
163175
}
164-
return p.path.Segments()[0]
176+
return ip.Segments()[0]
165177
}
166178

167179
func (p *path) Mutable() bool {
168-
//TODO: MFS: check for /local
180+
// TODO: MFS: check for /local
169181
return p.Namespace() == "ipns"
170182
}
171183

184+
func (p *path) IsValid() error {
185+
_, err := ipfspath.ParsePath(p.path)
186+
return err
187+
}
188+
172189
func (p *resolvedPath) Cid() cid.Cid {
173190
return p.cid
174191
}

0 commit comments

Comments
 (0)