Commit 0a0e69cc authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi: move path utils to interface

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 1806f0f9
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"context" "context"
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format" ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
) )
// CoreAPI defines an unified interface to IPFS for Go programs // CoreAPI defines an unified interface to IPFS for Go programs
...@@ -38,13 +37,4 @@ type CoreAPI interface { ...@@ -38,13 +37,4 @@ type CoreAPI interface {
// ResolveNode resolves the path (if not resolved already) using Unixfs // ResolveNode resolves the path (if not resolved already) using Unixfs
// resolver, gets and returns the resolved Node // resolver, gets and returns the resolved Node
ResolveNode(context.Context, Path) (ipld.Node, error) ResolveNode(context.Context, Path) (ipld.Node, error)
// ParsePath parses string path to a Path
ParsePath(string) (Path, error)
// IpfsPath creates new /ipfs path from the provided CID
IpfsPath(*cid.Cid) ResolvedPath
// IpldPath creates new /ipld path from the provided CID
IpldPath(*cid.Cid) ResolvedPath
} }
package iface package iface
import ( import (
ipfspath "github.com/ipfs/go-ipfs/path"
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid" cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
) )
//TODO: merge with ipfspath so we don't depend on it
// Path is a generic wrapper for paths used in the API. A path can be resolved // Path is a generic wrapper for paths used in the API. A path can be resolved
// to a CID using one of Resolve functions in the API. // to a CID using one of Resolve functions in the API.
// //
...@@ -87,3 +91,86 @@ type ResolvedPath interface { ...@@ -87,3 +91,86 @@ type ResolvedPath interface {
Path Path
} }
// path implements coreiface.Path
type path struct {
path ipfspath.Path
}
// resolvedPath implements coreiface.resolvedPath
type resolvedPath struct {
path
cid *cid.Cid
root *cid.Cid
remainder string
}
// IpfsPath creates new /ipfs path from the provided CID
func IpfsPath(c *cid.Cid) ResolvedPath {
return &resolvedPath{
path: path{ipfspath.Path("/ipfs/" + c.String())},
cid: c,
root: c,
remainder: "",
}
}
// IpldPath creates new /ipld path from the provided CID
func IpldPath(c *cid.Cid) ResolvedPath {
return &resolvedPath{
path: path{ipfspath.Path("/ipld/" + c.String())},
cid: c,
root: c,
remainder: "",
}
}
// ParsePath parses string path to a Path
func ParsePath(p string) (Path, error) {
pp, err := ipfspath.ParsePath(p)
if err != nil {
return nil, err
}
return &path{path: pp}, nil
}
// NewResolvedPath creates new ResolvedPath. This function performs no checks
// and is intended to be used by resolver implementations. Incorrect inputs may
// cause panics. Handle with care.
func NewResolvedPath(ipath ipfspath.Path, c *cid.Cid, root *cid.Cid, remainder string) ResolvedPath {
return &resolvedPath{
path: path{ipath},
cid: c,
root: root,
remainder: remainder,
}
}
func (p *path) String() string {
return p.path.String()
}
func (p *path) Namespace() string {
if len(p.path.Segments()) < 1 {
panic("path without namespace") //this shouldn't happen under any scenario
}
return p.path.Segments()[0]
}
func (p *path) Mutable() bool {
//TODO: MFS: check for /local
return p.Namespace() == "ipns"
}
func (p *resolvedPath) Cid() *cid.Cid {
return p.cid
}
func (p *resolvedPath) Root() *cid.Cid {
return p.root
}
func (p *resolvedPath) Remainder() string {
return p.remainder
}
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