Skip to content

Commit

Permalink
coreapi: move path utils to interface
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Jul 17, 2018
1 parent 082498d commit 7adf1cb
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 120 deletions.
4 changes: 2 additions & 2 deletions core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
return nil, err
}

return api.core().IpldPath(b.Cid()), nil
return coreiface.IpldPath(b.Cid()), nil
}

func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {
Expand Down Expand Up @@ -132,7 +132,7 @@ func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.Bloc
}

return &BlockStat{
path: api.core().IpldPath(b.Cid()),
path: coreiface.IpldPath(b.Cid()),
size: len(b.RawData()),
}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
return nil, err
}

return api.core().IpldPath(nds[0].Cid()), nil
return coreiface.IpldPath(nds[0].Cid()), nil
}

// Get resolves `path` using Unixfs resolver, returns the resolved Node.
Expand All @@ -66,7 +66,7 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.Da
paths := n.Tree("", settings.Depth)
out := make([]coreiface.Path, len(paths))
for n, p2 := range paths {
out[n], err = api.core().ParsePath(gopath.Join(p.String(), p2))
out[n], err = coreiface.ParsePath(gopath.Join(p.String(), p2))
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions core/coreapi/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"strings"
"testing"

mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"

coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
)

var (
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestPath(t *testing.T) {
t.Error(err)
}

p, err := api.ParsePath(path.Join(res.Cid().String(), "lnk"))
p, err := coreiface.ParsePath(path.Join(res.Cid().String(), "lnk"))
if err != nil {
t.Error(err)
}
Expand Down
10 changes: 0 additions & 10 deletions core/coreapi/interface/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"

ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
)

// CoreAPI defines an unified interface to IPFS for Go programs
Expand Down Expand Up @@ -38,13 +37,4 @@ type CoreAPI interface {
// ResolveNode resolves the path (if not resolved already) using Unixfs
// resolver, gets and returns the resolved Node
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
}
87 changes: 87 additions & 0 deletions core/coreapi/interface/path.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package iface

import (
ipfspath "github.com/ipfs/go-ipfs/path"

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
// to a CID using one of Resolve functions in the API.
//
Expand Down Expand Up @@ -87,3 +91,86 @@ type ResolvedPath interface {

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
}
7 changes: 6 additions & 1 deletion core/coreapi/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ func (k *key) Name() string {

// Path returns the path of the key.
func (k *key) Path() coreiface.Path {
return &path{path: ipfspath.FromString(ipfspath.Join([]string{"/ipns", k.peerId}))}
path, err := coreiface.ParsePath(ipfspath.Join([]string{"/ipns", k.peerId}))
if err != nil {
panic("error parsing path: " + err.Error())
}

return path
}

// Generate generates new key, stores it in the keystore under the specified
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
return nil, err
}

return &path{path: output}, nil
return coreiface.ParsePath(output.String())
}

func keylookup(n *core.IpfsNode, k string) (crypto.PrivKey, error) {
Expand Down
8 changes: 4 additions & 4 deletions core/coreapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
return nil, err
}

return api.core().IpfsPath(dagnode.Cid()), nil
return coreiface.IpfsPath(dagnode.Cid()), nil
}

func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (ipld.Node, error) {
Expand Down Expand Up @@ -218,7 +218,7 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
return nil, err
}

return api.core().IpfsPath(nnode.Cid()), nil
return coreiface.IpfsPath(nnode.Cid()), nil
}

func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.ResolvedPath, error) {
Expand All @@ -244,7 +244,7 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri
return nil, err
}

return api.core().IpfsPath(nnode.Cid()), nil
return coreiface.IpfsPath(nnode.Cid()), nil
}

func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.ResolvedPath, error) {
Expand Down Expand Up @@ -281,7 +281,7 @@ func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.R
return nil, err
}

return api.core().IpfsPath(pbnd.Cid()), nil
return coreiface.IpfsPath(pbnd.Cid()), nil
}

func (api *ObjectAPI) core() coreiface.CoreAPI {
Expand Down
80 changes: 2 additions & 78 deletions core/coreapi/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,6 @@ import (
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
)

// 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 parses the path from `c`, reruns the parsed path.
func (api *CoreAPI) IpfsPath(c *cid.Cid) coreiface.ResolvedPath {
return &resolvedPath{
path: path{ipfspath.Path("/ipfs/" + c.String())},
cid: c,
root: c,
remainder: "",
}
}

// IpldPath parses the path from `c`, reruns the parsed path.
func (api *CoreAPI) IpldPath(c *cid.Cid) coreiface.ResolvedPath {
return &resolvedPath{
path: path{ipfspath.Path("/ipld/" + c.String())},
cid: c,
root: c,
remainder: "",
}
}

// ResolveNode resolves the path `p` using Unixfs resolver, gets and returns the
// resolved Node.
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
Expand Down Expand Up @@ -79,7 +46,7 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
return p.(coreiface.ResolvedPath), nil
}

ipath := p.(*path).path
ipath := ipfspath.Path(p.String())
ipath, err := core.ResolveIPNS(ctx, nsys, ipath)
if err == core.ErrNoNamesys {
return nil, coreiface.ErrOffline
Expand Down Expand Up @@ -113,48 +80,5 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
return nil, err
}

return &resolvedPath{
path: path{ipath},
cid: node.Cid(),
root: root,
remainder: gopath.Join(rest...),
}, nil
}

// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) {
pp, err := ipfspath.ParsePath(p)
if err != nil {
return nil, err
}

return &path{path: pp}, nil
}

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
return coreiface.NewResolvedPath(ipath, node.Cid(), root, gopath.Join(rest...)), nil
}
9 changes: 5 additions & 4 deletions core/coreapi/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)

Expand Down Expand Up @@ -47,7 +48,7 @@ func TestPathRemainder(t *testing.T) {
t.Fatal(err)
}

p1, err := api.ParsePath(obj.String() + "/foo/bar")
p1, err := coreiface.ParsePath(obj.String() + "/foo/bar")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestEmptyPathRemainder(t *testing.T) {
t.Error("expected the resolved path to not have a remainder")
}

p1, err := api.ParsePath(obj.String())
p1, err := coreiface.ParsePath(obj.String())
if err != nil {
t.Error(err)
}
Expand All @@ -105,7 +106,7 @@ func TestInvalidPathRemainder(t *testing.T) {
t.Fatal(err)
}

p1, err := api.ParsePath(obj.String() + "/bar/baz")
p1, err := coreiface.ParsePath(obj.String() + "/bar/baz")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestPathRoot(t *testing.T) {
t.Fatal(err)
}

p1, err := api.ParsePath(obj.String() + "/foo")
p1, err := coreiface.ParsePath(obj.String() + "/foo")
if err != nil {
t.Error(err)
}
Expand Down
Loading

0 comments on commit 7adf1cb

Please sign in to comment.