@@ -103,6 +103,8 @@ func (ip ImmutablePath) Segments() []string {
103103 return ip .path .Segments ()
104104}
105105
106+ var _ Path = path {}
107+
106108type path struct {
107109 str string
108110 root cid.Cid
@@ -133,6 +135,8 @@ func (p path) Segments() []string {
133135 return strings .Split (str , "/" )
134136}
135137
138+ var _ ResolvedPath = resolvedPath {}
139+
136140type resolvedPath struct {
137141 path
138142 cid cid.Cid
@@ -174,6 +178,9 @@ func NewIPLDPath(cid cid.Cid) ResolvedPath {
174178}
175179
176180// NewIPNSPath returns a new "/ipns" path with the provided CID.
181+ // TODO: it is better to use [ipns.Name], but that leads to import cycle. Maybe
182+ // this specific function could be moved to the iPNS package? It'sn an IPNS path
183+ // after all.
177184func NewIPNSPath (cid cid.Cid ) Path {
178185 return & path {
179186 str : fmt .Sprintf ("/%s/%s" , IPNSNamespace , cid .String ()),
@@ -191,16 +198,8 @@ func NewDNSLinkPath(domain string) Path {
191198}
192199
193200// NewPath returns a well-formed [Path]. The returned path will always be prefixed
194- // with a valid namespace (/ipfs, /ipld, or /ipns). The prefix will be added if not
195- // present in the given string. The rules are:
196- //
197- // 1. If the path has a single component (no slashes) ans it is a valid CID,
198- // an /ipfs path is returned. If the CID is encoded with the Libp2pKey codec,
199- // then a /ipns path is returned.
200- // 2. If the path has a valid CID root but does not have a namespace, the /ipfs
201- // namespace is automatically added.
202- //
203- // This function returns an error when the given string is not a valid path.
201+ // with a valid namespace (/ipfs, /ipld, or /ipns). This function returns an error
202+ // when the given string is not a valid path.
204203func NewPath (str string ) (Path , error ) {
205204 cleaned := gopath .Clean (str )
206205 components := strings .Split (cleaned , "/" )
@@ -210,33 +209,6 @@ func NewPath(str string) (Path, error) {
210209 cleaned += "/"
211210 }
212211
213- // If there's only one component, check if it's a CID, or Peer ID.
214- if len (components ) == 1 {
215- c , err := cid .Decode (components [0 ])
216- if err == nil {
217- if c .Prefix ().GetCodec () == cid .Libp2pKey {
218- return NewIPNSPath (c ), nil
219- } else {
220- return NewIPFSPath (c ), nil
221- }
222- }
223- }
224-
225- // If the path doesn't begin with a "/", we expect it to start with a CID and
226- // be an IPFS Path.
227- if components [0 ] != "" {
228- root , err := cid .Decode (components [0 ])
229- if err != nil {
230- return nil , & ErrInvalidPath {error : err , path : str }
231- }
232-
233- return & path {
234- str : cleaned ,
235- root : root ,
236- namespace : IPFSNamespace ,
237- }, nil
238- }
239-
240212 if len (components ) < 3 {
241213 return nil , & ErrInvalidPath {error : fmt .Errorf ("not enough path components" ), path : str }
242214 }
0 commit comments