1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-09 17:22:21 +08:00

Implements Path.PopLastSegment().

This allows a path (/ipfs/foo/bar) to be separated between its head
(/ipfs/foo) and its tail (bar).

License: MIT
Signed-off-by: Stephen Whitmore <noffle@ipfs.io>
This commit is contained in:
Stephen Whitmore
2016-01-24 23:29:41 -08:00
parent 8a2bdc86fb
commit cf0a85b7a4
2 changed files with 46 additions and 0 deletions

View File

@ -50,6 +50,24 @@ func (p Path) IsJustAKey() bool {
return (len(parts) == 2 && parts[0] == "ipfs")
}
// PopLastSegment returns a new Path without its final segment, and the final
// segment, separately. If there is no more to pop (the path is just a key),
// the original path is returned.
func (p Path) PopLastSegment() (Path, string, error) {
if p.IsJustAKey() {
return p, "", nil
}
segs := p.Segments()
newPath, err := ParsePath("/" + strings.Join(segs[:len(segs)-1], "/"))
if err != nil {
return "", "", err
}
return newPath, segs[len(segs)-1], nil
}
func FromSegments(prefix string, seg ...string) (Path, error) {
return ParsePath(prefix + strings.Join(seg, "/"))
}