1
- package iface
1
+ package path
2
2
3
3
import (
4
- "github.com/ipfs/go-cid"
4
+ "strings"
5
+
6
+ cid "github.com/ipfs/go-cid"
5
7
ipfspath "github.com/ipfs/go-path"
6
8
)
7
9
8
- //TODO: merge with ipfspath so we don't depend on it
9
-
10
10
// Path is a generic wrapper for paths used in the API. A path can be resolved
11
11
// to a CID using one of Resolve functions in the API.
12
12
//
@@ -23,17 +23,25 @@ type Path interface {
23
23
// Namespace returns the first component of the path.
24
24
//
25
25
// 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
26
29
Namespace () string
27
30
28
31
// Mutable returns false if the data pointed to by this path in guaranteed
29
32
// to not change.
30
33
//
31
34
// Note that resolved mutable path can be immutable.
32
35
Mutable () bool
36
+
37
+ // IsValid checks if this path is a valid ipfs Path, returning nil iff it is
38
+ // valid
39
+ IsValid () error
33
40
}
34
41
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 {
37
45
// Cid returns the CID of the node referenced by the path. Remainder of the
38
46
// path is guaranteed to be within the node.
39
47
//
@@ -94,7 +102,7 @@ type ResolvedPath interface {
94
102
95
103
// path implements coreiface.Path
96
104
type path struct {
97
- path ipfspath. Path
105
+ path string
98
106
}
99
107
100
108
// resolvedPath implements coreiface.resolvedPath
@@ -107,68 +115,77 @@ type resolvedPath struct {
107
115
108
116
// Join appends provided segments to the base path
109
117
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 }
112
120
}
113
121
114
122
// IpfsPath creates new /ipfs path from the provided CID
115
- func IpfsPath (c cid.Cid ) ResolvedPath {
123
+ func IpfsPath (c cid.Cid ) Resolved {
116
124
return & resolvedPath {
117
- path : path {ipfspath . Path ( "/ipfs/" + c .String () )},
125
+ path : path {"/ipfs/" + c .String ()},
118
126
cid : c ,
119
127
root : c ,
120
128
remainder : "" ,
121
129
}
122
130
}
123
131
124
132
// IpldPath creates new /ipld path from the provided CID
125
- func IpldPath (c cid.Cid ) ResolvedPath {
133
+ func IpldPath (c cid.Cid ) Resolved {
126
134
return & resolvedPath {
127
- path : path {ipfspath . Path ( "/ipld/" + c .String () )},
135
+ path : path {"/ipld/" + c .String ()},
128
136
cid : c ,
129
137
root : c ,
130
138
remainder : "" ,
131
139
}
132
140
}
133
141
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 ()
139
146
}
140
147
141
- return & path {path : pp }, nil
148
+ return & path {path : p }
142
149
}
143
150
144
- // NewResolvedPath creates new ResolvedPath . This function performs no checks
151
+ // NewResolvedPath creates new Resolved path . This function performs no checks
145
152
// and is intended to be used by resolver implementations. Incorrect inputs may
146
153
// 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 {
148
155
return & resolvedPath {
149
- path : path {ipath },
156
+ path : path {ipath . String () },
150
157
cid : c ,
151
158
root : root ,
152
159
remainder : remainder ,
153
160
}
154
161
}
155
162
156
163
func (p * path ) String () string {
157
- return p .path . String ()
164
+ return p .path
158
165
}
159
166
160
167
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
163
175
}
164
- return p . path .Segments ()[0 ]
176
+ return ip .Segments ()[0 ]
165
177
}
166
178
167
179
func (p * path ) Mutable () bool {
168
- //TODO: MFS: check for /local
180
+ // TODO: MFS: check for /local
169
181
return p .Namespace () == "ipns"
170
182
}
171
183
184
+ func (p * path ) IsValid () error {
185
+ _ , err := ipfspath .ParsePath (p .path )
186
+ return err
187
+ }
188
+
172
189
func (p * resolvedPath ) Cid () cid.Cid {
173
190
return p .cid
174
191
}
0 commit comments