Commit 2b9bff75 authored by Łukasz Magiera's avatar Łukasz Magiera

path: drop error from ParsePath

parent c140b0fa
package iface package iface
import ( import (
"github.com/ipfs/go-cid" "strings"
cid "github.com/ipfs/go-cid"
ipfspath "github.com/ipfs/go-path" ipfspath "github.com/ipfs/go-path"
) )
...@@ -23,6 +25,9 @@ type Path interface { ...@@ -23,6 +25,9 @@ type Path interface {
// Namespace returns the first component of the path. // Namespace returns the first component of the path.
// //
// For example path "/ipfs/QmHash", calling Namespace() will return "ipfs" // For example path "/ipfs/QmHash", calling Namespace() will return "ipfs"
//
// Calling this method on invalid paths (IsValid() != nil) will result in
// empty string
Namespace() string Namespace() string
// Mutable returns false if the data pointed to by this path in guaranteed // Mutable returns false if the data pointed to by this path in guaranteed
...@@ -30,9 +35,14 @@ type Path interface { ...@@ -30,9 +35,14 @@ type Path interface {
// //
// Note that resolved mutable path can be immutable. // Note that resolved mutable path can be immutable.
Mutable() bool Mutable() bool
// IsValid checks if this path is a valid ipfs Path, returning nil iff it is
// valid
IsValid() error
} }
// ResolvedPath is a path which was resolved to the last resolvable node // ResolvedPath is a path which was resolved to the last resolvable node.
// ResolvedPaths are guaranteed to return nil from `IsValid`
type ResolvedPath interface { type ResolvedPath interface {
// Cid returns the CID of the node referenced by the path. Remainder of the // Cid returns the CID of the node referenced by the path. Remainder of the
// path is guaranteed to be within the node. // path is guaranteed to be within the node.
...@@ -94,7 +104,7 @@ type ResolvedPath interface { ...@@ -94,7 +104,7 @@ type ResolvedPath interface {
// path implements coreiface.Path // path implements coreiface.Path
type path struct { type path struct {
path ipfspath.Path path string
} }
// resolvedPath implements coreiface.resolvedPath // resolvedPath implements coreiface.resolvedPath
...@@ -107,14 +117,14 @@ type resolvedPath struct { ...@@ -107,14 +117,14 @@ type resolvedPath struct {
// Join appends provided segments to the base path // Join appends provided segments to the base path
func Join(base Path, a ...string) Path { func Join(base Path, a ...string) Path {
s := ipfspath.Join(append([]string{base.String()}, a...)) s := strings.Join(append([]string{base.String()}, a...), "/")
return &path{path: ipfspath.FromString(s)} return &path{path: s}
} }
// IpfsPath creates new /ipfs path from the provided CID // IpfsPath creates new /ipfs path from the provided CID
func IpfsPath(c cid.Cid) ResolvedPath { func IpfsPath(c cid.Cid) ResolvedPath {
return &resolvedPath{ return &resolvedPath{
path: path{ipfspath.Path("/ipfs/" + c.String())}, path: path{"/ipfs/" + c.String()},
cid: c, cid: c,
root: c, root: c,
remainder: "", remainder: "",
...@@ -124,7 +134,7 @@ func IpfsPath(c cid.Cid) ResolvedPath { ...@@ -124,7 +134,7 @@ func IpfsPath(c cid.Cid) ResolvedPath {
// IpldPath creates new /ipld path from the provided CID // IpldPath creates new /ipld path from the provided CID
func IpldPath(c cid.Cid) ResolvedPath { func IpldPath(c cid.Cid) ResolvedPath {
return &resolvedPath{ return &resolvedPath{
path: path{ipfspath.Path("/ipld/" + c.String())}, path: path{"/ipld/" + c.String()},
cid: c, cid: c,
root: c, root: c,
remainder: "", remainder: "",
...@@ -132,13 +142,12 @@ func IpldPath(c cid.Cid) ResolvedPath { ...@@ -132,13 +142,12 @@ func IpldPath(c cid.Cid) ResolvedPath {
} }
// ParsePath parses string path to a Path // ParsePath parses string path to a Path
func ParsePath(p string) (Path, error) { func ParsePath(p string) Path {
pp, err := ipfspath.ParsePath(p) if pp, err := ipfspath.ParsePath(p); err == nil {
if err != nil { p = pp.String()
return nil, err
} }
return &path{path: pp}, nil return &path{path: p}
} }
// NewResolvedPath creates new ResolvedPath. This function performs no checks // NewResolvedPath creates new ResolvedPath. This function performs no checks
...@@ -146,7 +155,7 @@ func ParsePath(p string) (Path, error) { ...@@ -146,7 +155,7 @@ func ParsePath(p string) (Path, error) {
// cause panics. Handle with care. // cause panics. Handle with care.
func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder string) ResolvedPath { func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder string) ResolvedPath {
return &resolvedPath{ return &resolvedPath{
path: path{ipath}, path: path{ipath.String()},
cid: c, cid: c,
root: root, root: root,
remainder: remainder, remainder: remainder,
...@@ -154,14 +163,19 @@ func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder str ...@@ -154,14 +163,19 @@ func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder str
} }
func (p *path) String() string { func (p *path) String() string {
return p.path.String() return p.path
} }
func (p *path) Namespace() string { func (p *path) Namespace() string {
if len(p.path.Segments()) < 1 { ip, err := ipfspath.ParsePath(p.path)
if err != nil {
return ""
}
if len(ip.Segments()) < 1 {
panic("path without namespace") //this shouldn't happen under any scenario panic("path without namespace") //this shouldn't happen under any scenario
} }
return p.path.Segments()[0] return ip.Segments()[0]
} }
func (p *path) Mutable() bool { func (p *path) Mutable() bool {
...@@ -169,6 +183,11 @@ func (p *path) Mutable() bool { ...@@ -169,6 +183,11 @@ func (p *path) Mutable() bool {
return p.Namespace() == "ipns" return p.Namespace() == "ipns"
} }
func (p *path) IsValid() error {
_, err := ipfspath.ParsePath(p.path)
return err
}
func (p *resolvedPath) Cid() cid.Cid { func (p *resolvedPath) Cid() cid.Cid {
return p.cid return p.cid
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment