path: drop error from ParsePath
This commit was moved from ipfs/interface-go-ipfs-core@2b9bff7523c812447641aa70c39ec0b096f5b5c4 This commit was moved from ipfs/boxo@31071e1f5e59a62d53e2c2c7e259eedff6d49f6f
Łukasz Magiera committed
Mar 25, 2019 at 17:03 UTC
1a2e8ce6d76f9517240ca53339937d4e07dcfe96
1 file changed
+35
-16
core/coreiface/path.go
+35
-16
@@ -1,7 +1,9 @@
1
package iface
2
3
import (
4
- "github.com/ipfs/go-cid"
4
+ "strings"
5
+
6
+ cid "github.com/ipfs/go-cid"
7
ipfspath "github.com/ipfs/go-path"
8
)
9
@@ -23,6 +25,9 @@ type Path interface {
25
// Namespace returns the first component of the path.
26
//
27
// For example path "/ipfs/QmHash", calling Namespace() will return "ipfs"
28
+ //
29
+ // Calling this method on invalid paths (IsValid() != nil) will result in
30
+ // empty string
31
Namespace() string
32
33
// Mutable returns false if the data pointed to by this path in guaranteed
@@ -30,9 +35,14 @@ type Path interface {
35
//
36
// Note that resolved mutable path can be immutable.
37
Mutable() bool
38
+
39
+ // IsValid checks if this path is a valid ipfs Path, returning nil iff it is
40
+ // valid
41
+ IsValid() error
42
}
43
35
-// ResolvedPath is a path which was resolved to the last resolvable node
44
+// ResolvedPath is a path which was resolved to the last resolvable node.
45
+// ResolvedPaths are guaranteed to return nil from `IsValid`
46
type ResolvedPath interface {
47
// Cid returns the CID of the node referenced by the path. Remainder of the
48
// path is guaranteed to be within the node.
@@ -94,7 +104,7 @@ type ResolvedPath interface {
104
105
// path implements coreiface.Path
106
type path struct {
97
- path ipfspath.Path
107
+ path string
108
}
109
110
// resolvedPath implements coreiface.resolvedPath
@@ -107,14 +117,14 @@ type resolvedPath struct {
117
118
// Join appends provided segments to the base path
119
func Join(base Path, a ...string) Path {
110
- s := ipfspath.Join(append([]string{base.String()}, a...))
111
- return &path{path: ipfspath.FromString(s)}
120
+ s := strings.Join(append([]string{base.String()}, a...), "/")
121
+ return &path{path: s}
122
}
123
124
// IpfsPath creates new /ipfs path from the provided CID
125
func IpfsPath(c cid.Cid) ResolvedPath {
126
return &resolvedPath{
117
- path: path{ipfspath.Path("/ipfs/" + c.String())},
127
+ path: path{"/ipfs/" + c.String()},
128
cid: c,
129
root: c,
130
remainder: "",
@@ -124,7 +134,7 @@ func IpfsPath(c cid.Cid) ResolvedPath {
134
// IpldPath creates new /ipld path from the provided CID
135
func IpldPath(c cid.Cid) ResolvedPath {
136
return &resolvedPath{
127
- path: path{ipfspath.Path("/ipld/" + c.String())},
137
+ path: path{"/ipld/" + c.String()},
138
cid: c,
139
root: c,
140
remainder: "",
@@ -132,13 +142,12 @@ func IpldPath(c cid.Cid) ResolvedPath {
142
}
143
144
// 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
145
+func ParsePath(p string) Path {
146
+ if pp, err := ipfspath.ParsePath(p); err == nil {
147
+ p = pp.String()
148
}
149
141
- return &path{path: pp}, nil
150
+ return &path{path: p}
151
}
152
153
// NewResolvedPath creates new ResolvedPath. This function performs no checks
@@ -146,7 +155,7 @@ func ParsePath(p string) (Path, error) {
155
// cause panics. Handle with care.
156
func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder string) ResolvedPath {
157
return &resolvedPath{
149
- path: path{ipath},
158
+ path: path{ipath.String()},
159
cid: c,
160
root: root,
161
remainder: remainder,
@@ -154,14 +163,19 @@ func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder str
163
}
164
165
func (p *path) String() string {
157
- return p.path.String()
166
+ return p.path
167
}
168
169
func (p *path) Namespace() string {
161
- if len(p.path.Segments()) < 1 {
170
+ ip, err := ipfspath.ParsePath(p.path)
171
+ if err != nil {
172
+ return ""
173
+ }
174
+
175
+ if len(ip.Segments()) < 1 {
176
panic("path without namespace") //this shouldn't happen under any scenario
177
}
164
- return p.path.Segments()[0]
178
+ return ip.Segments()[0]
179
}
180
181
func (p *path) Mutable() bool {
@@ -169,6 +183,11 @@ func (p *path) Mutable() bool {
183
return p.Namespace() == "ipns"
184
}
185
186
+func (p *path) IsValid() error {
187
+ _, err := ipfspath.ParsePath(p.path)
188
+ return err
189
+}
190
+
191
func (p *resolvedPath) Cid() cid.Cid {
192
return p.cid
193
}