Skip to content

Commit

Permalink
Merge pull request ipfs/interface-go-ipfs-core#22 from ipfs/feat/drop…
Browse files Browse the repository at this point in the history
…-path-err

path: drop error from ParsePath

This commit was moved from ipfs/interface-go-ipfs-core@7786158
  • Loading branch information
Stebalien authored Apr 17, 2019
2 parents d5fa455 + c86550b commit 0f3fb6b
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 139 deletions.
11 changes: 6 additions & 5 deletions coreapi/iface/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package iface

import (
"context"
path "github.com/ipfs/interface-go-ipfs-core/path"
"io"

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

// BlockStat contains information about a block
Expand All @@ -13,7 +14,7 @@ type BlockStat interface {
Size() int

// Path returns path to the block
Path() ResolvedPath
Path() path.Resolved
}

// BlockAPI specifies the interface to the block layer
Expand All @@ -22,15 +23,15 @@ type BlockAPI interface {
Put(context.Context, io.Reader, ...options.BlockPutOption) (BlockStat, error)

// Get attempts to resolve the path and return a reader for data in the block
Get(context.Context, Path) (io.Reader, error)
Get(context.Context, path.Path) (io.Reader, error)

// Rm removes the block specified by the path from local blockstore.
// By default an error will be returned if the block can't be found locally.
//
// NOTE: If the specified block is pinned it won't be removed and no error
// will be returned
Rm(context.Context, Path, ...options.BlockRmOption) error
Rm(context.Context, path.Path, ...options.BlockRmOption) error

// Stat returns information on
Stat(context.Context, Path) (BlockStat, error)
Stat(context.Context, path.Path) (BlockStat, error)
}
5 changes: 3 additions & 2 deletions coreapi/iface/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package iface

import (
"context"
path "github.com/ipfs/interface-go-ipfs-core/path"

"github.com/ipfs/interface-go-ipfs-core/options"

Expand Down Expand Up @@ -43,11 +44,11 @@ type CoreAPI interface {
PubSub() PubSubAPI

// ResolvePath resolves the path using Unixfs resolver
ResolvePath(context.Context, Path) (ResolvedPath, error)
ResolvePath(context.Context, path.Path) (path.Resolved, error)

// ResolveNode resolves the path (if not resolved already) using Unixfs
// resolver, gets and returns the resolved Node
ResolveNode(context.Context, Path) (ipld.Node, error)
ResolveNode(context.Context, path.Path) (ipld.Node, error)

// WithOptions creates new instance of CoreAPI based on this instance with
// a set of options applied
Expand Down
7 changes: 4 additions & 3 deletions coreapi/iface/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package iface

import (
"context"
path "github.com/ipfs/interface-go-ipfs-core/path"

"github.com/ipfs/interface-go-ipfs-core/options"

peer "github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
)

Expand All @@ -19,8 +20,8 @@ type DhtAPI interface {

// FindProviders finds peers in the DHT who can provide a specific value
// given a key.
FindProviders(context.Context, Path, ...options.DhtFindProvidersOption) (<-chan pstore.PeerInfo, error)
FindProviders(context.Context, path.Path, ...options.DhtFindProvidersOption) (<-chan pstore.PeerInfo, error)

// Provide announces to the network that you are providing given values
Provide(context.Context, Path, ...options.DhtProvideOption) error
Provide(context.Context, path.Path, ...options.DhtProvideOption) error
}
5 changes: 3 additions & 2 deletions coreapi/iface/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package iface

import (
"context"
path "github.com/ipfs/interface-go-ipfs-core/path"

options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/options"

"github.com/libp2p/go-libp2p-peer"
)
Expand All @@ -14,7 +15,7 @@ type Key interface {
Name() string

// Path returns key path
Path() Path
Path() path.Path

// ID returns key PeerID
ID() peer.ID
Expand Down
11 changes: 6 additions & 5 deletions coreapi/iface/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package iface
import (
"context"
"errors"
path "github.com/ipfs/interface-go-ipfs-core/path"

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

var ErrResolveFailed = errors.New("could not resolve name")
Expand All @@ -14,11 +15,11 @@ type IpnsEntry interface {
// Name returns IpnsEntry name
Name() string
// Value returns IpnsEntry value
Value() Path
Value() path.Path
}

type IpnsResult struct {
Path
path.Path
Err error
}

Expand All @@ -32,10 +33,10 @@ type IpnsResult struct {
// You can use .Key API to list and generate more names and their respective keys.
type NameAPI interface {
// Publish announces new IPNS name
Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (IpnsEntry, error)
Publish(ctx context.Context, path path.Path, opts ...options.NamePublishOption) (IpnsEntry, error)

// Resolve attempts to resolve the newest version of the specified name
Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)
Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (path.Path, error)

// Search is a version of Resolve which outputs paths as they are discovered,
// reducing the time to first entry
Expand Down
29 changes: 15 additions & 14 deletions coreapi/iface/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package iface

import (
"context"
path "github.com/ipfs/interface-go-ipfs-core/path"
"io"

options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/options"

cid "github.com/ipfs/go-cid"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
)

Expand Down Expand Up @@ -58,11 +59,11 @@ type ObjectChange struct {

// Before holds the link path before the change. Note that when a link is
// added, this will be nil.
Before ResolvedPath
Before path.Resolved

// After holds the link path after the change. Note that when a link is
// removed, this will be nil.
After ResolvedPath
After path.Resolved
}

// ObjectAPI specifies the interface to MerkleDAG and contains useful utilities
Expand All @@ -72,35 +73,35 @@ type ObjectAPI interface {
New(context.Context, ...options.ObjectNewOption) (ipld.Node, error)

// Put imports the data into merkledag
Put(context.Context, io.Reader, ...options.ObjectPutOption) (ResolvedPath, error)
Put(context.Context, io.Reader, ...options.ObjectPutOption) (path.Resolved, error)

// Get returns the node for the path
Get(context.Context, Path) (ipld.Node, error)
Get(context.Context, path.Path) (ipld.Node, error)

// Data returns reader for data of the node
Data(context.Context, Path) (io.Reader, error)
Data(context.Context, path.Path) (io.Reader, error)

// Links returns lint or links the node contains
Links(context.Context, Path) ([]*ipld.Link, error)
Links(context.Context, path.Path) ([]*ipld.Link, error)

// Stat returns information about the node
Stat(context.Context, Path) (*ObjectStat, error)
Stat(context.Context, path.Path) (*ObjectStat, error)

// AddLink adds a link under the specified path. child path can point to a
// subdirectory within the patent which must be present (can be overridden
// with WithCreate option).
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (ResolvedPath, error)
AddLink(ctx context.Context, base path.Path, name string, child path.Path, opts ...options.ObjectAddLinkOption) (path.Resolved, error)

// RmLink removes a link from the node
RmLink(ctx context.Context, base Path, link string) (ResolvedPath, error)
RmLink(ctx context.Context, base path.Path, link string) (path.Resolved, error)

// AppendData appends data to the node
AppendData(context.Context, Path, io.Reader) (ResolvedPath, error)
AppendData(context.Context, path.Path, io.Reader) (path.Resolved, error)

// SetData sets the data contained in the node
SetData(context.Context, Path, io.Reader) (ResolvedPath, error)
SetData(context.Context, path.Path, io.Reader) (path.Resolved, error)

// Diff returns a set of changes needed to transform the first object into the
// second.
Diff(context.Context, Path, Path) ([]ObjectChange, error)
Diff(context.Context, path.Path, path.Path) ([]ObjectChange, error)
}
71 changes: 44 additions & 27 deletions coreapi/iface/path.go → coreapi/iface/path/path.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package iface
package path

import (
"github.com/ipfs/go-cid"
"strings"

cid "github.com/ipfs/go-cid"
ipfspath "github.com/ipfs/go-path"
)

//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 All @@ -23,17 +23,25 @@ type Path interface {
// Namespace returns the first component of the path.
//
// 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

// Mutable returns false if the data pointed to by this path in guaranteed
// to not change.
//
// Note that resolved mutable path can be immutable.
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
type ResolvedPath interface {
// Resolved is a path which was resolved to the last resolvable node.
// ResolvedPaths are guaranteed to return nil from `IsValid`
type Resolved interface {
// Cid returns the CID of the node referenced by the path. Remainder of the
// path is guaranteed to be within the node.
//
Expand Down Expand Up @@ -94,7 +102,7 @@ type ResolvedPath interface {

// path implements coreiface.Path
type path struct {
path ipfspath.Path
path string
}

// resolvedPath implements coreiface.resolvedPath
Expand All @@ -107,68 +115,77 @@ type resolvedPath struct {

// Join appends provided segments to the base path
func Join(base Path, a ...string) Path {
s := ipfspath.Join(append([]string{base.String()}, a...))
return &path{path: ipfspath.FromString(s)}
s := strings.Join(append([]string{base.String()}, a...), "/")
return &path{path: s}
}

// IpfsPath creates new /ipfs path from the provided CID
func IpfsPath(c cid.Cid) ResolvedPath {
func IpfsPath(c cid.Cid) Resolved {
return &resolvedPath{
path: path{ipfspath.Path("/ipfs/" + c.String())},
path: path{"/ipfs/" + c.String()},
cid: c,
root: c,
remainder: "",
}
}

// IpldPath creates new /ipld path from the provided CID
func IpldPath(c cid.Cid) ResolvedPath {
func IpldPath(c cid.Cid) Resolved {
return &resolvedPath{
path: path{ipfspath.Path("/ipld/" + c.String())},
path: 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
// New parses string path to a Path
func New(p string) Path {
if pp, err := ipfspath.ParsePath(p); err == nil {
p = pp.String()
}

return &path{path: pp}, nil
return &path{path: p}
}

// NewResolvedPath creates new ResolvedPath. This function performs no checks
// NewResolvedPath creates new Resolved path. 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 {
func NewResolvedPath(ipath ipfspath.Path, c cid.Cid, root cid.Cid, remainder string) Resolved {
return &resolvedPath{
path: path{ipath},
path: path{ipath.String()},
cid: c,
root: root,
remainder: remainder,
}
}

func (p *path) String() string {
return p.path.String()
return p.path
}

func (p *path) Namespace() string {
if len(p.path.Segments()) < 1 {
panic("path without namespace") //this shouldn't happen under any scenario
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
}
return p.path.Segments()[0]
return ip.Segments()[0]
}

func (p *path) Mutable() bool {
//TODO: MFS: check for /local
// TODO: MFS: check for /local
return p.Namespace() == "ipns"
}

func (p *path) IsValid() error {
_, err := ipfspath.ParsePath(p.path)
return err
}

func (p *resolvedPath) Cid() cid.Cid {
return p.cid
}
Expand Down
Loading

0 comments on commit 0f3fb6b

Please sign in to comment.