Commit 79875b01 authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi: separate path into two types

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent ad6db2fc
......@@ -13,13 +13,13 @@ type BlockStat interface {
Size() int
// Path returns path to the block
Path() Path
Path() ResolvedPath
}
// BlockAPI specifies the interface to the block layer
type BlockAPI interface {
// Put imports raw block data, hashing it using specified settings.
Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
Put(context.Context, io.Reader, ...options.BlockPutOption) (ResolvedPath, error)
// Get attempts to resolve the path and return a reader for data in the block
Get(context.Context, Path) (io.Reader, error)
......
......@@ -5,10 +5,8 @@ package iface
import (
"context"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
)
// CoreAPI defines an unified interface to IPFS for Go programs
......@@ -35,19 +33,15 @@ type CoreAPI interface {
Object() ObjectAPI
// ResolvePath resolves the path using Unixfs resolver
ResolvePath(context.Context, Path) (Path, error)
ResolvePath(context.Context, Path) (ResolvedPath, 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)
// ParsePath parses string path to a Path
ParsePath(context.Context, string, ...options.ParsePathOption) (Path, error)
// WithResolve is an option for ParsePath which when set to true tells
// ParsePath to also resolve the path
WithResolve(bool) options.ParsePathOption
ParsePath(context.Context, string) (Path, error)
// ParseCid creates new path from the provided CID
ParseCid(*cid.Cid) Path
ParseCid(*cid.Cid) ResolvedPath
}
......@@ -14,7 +14,7 @@ type DagAPI interface {
// Put inserts data using specified format and input encoding.
// Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
// "sha256" are used.
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error)
Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (ResolvedPath, error)
// Get attempts to resolve and get the node specified by the path
Get(ctx context.Context, path Path) (ipld.Node, error)
......
......@@ -38,7 +38,7 @@ type ObjectAPI interface {
New(context.Context, ...options.ObjectNewOption) (ipld.Node, error)
// Put imports the data into merkledag
Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)
Put(context.Context, io.Reader, ...options.ObjectPutOption) (ResolvedPath, error)
// Get returns the node for the path
Get(context.Context, Path) (ipld.Node, error)
......@@ -55,14 +55,14 @@ type ObjectAPI interface {
// 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) (Path, error)
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (ResolvedPath, error)
// RmLink removes a link from the node
RmLink(ctx context.Context, base Path, link string) (Path, error)
RmLink(ctx context.Context, base Path, link string) (ResolvedPath, error)
// AppendData appends data to the node
AppendData(context.Context, Path, io.Reader) (Path, error)
AppendData(context.Context, Path, io.Reader) (ResolvedPath, error)
// SetData sets the data contained in the node
SetData(context.Context, Path, io.Reader) (Path, error)
SetData(context.Context, Path, io.Reader) (ResolvedPath, error)
}
package options
type ParsePathSettings struct {
Resolve bool
}
type ParsePathOption func(*ParsePathSettings) error
func ParsePathOptions(opts ...ParsePathOption) (*ParsePathSettings, error) {
options := &ParsePathSettings{
Resolve: false,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type ApiOptions struct{}
func (api *ApiOptions) WithResolve(r bool) ParsePathOption {
return func(settings *ParsePathSettings) error {
settings.Resolve = r
return nil
}
}
......@@ -6,13 +6,24 @@ import (
// 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.
// TODO: figure out/explain namespaces
type Path interface {
// String returns the path as a string.
String() string
// Namespace returns the first component of the path
Namespace() string
}
// ResolvedPath is a resolved Path
type ResolvedPath interface {
// Cid returns cid referred to by path
Cid() *cid.Cid
// Root returns cid of root path
Root() *cid.Cid
// Resolved returns whether path has been fully resolved
Resolved() bool
//TODO: Path remainder
Path
}
......@@ -9,7 +9,7 @@ import (
// Pin holds information about pinned resource
type Pin interface {
// Path to the pinned object
Path() Path
Path() ResolvedPath
// Type of the pin
Type() string
......@@ -27,7 +27,7 @@ type PinStatus interface {
// BadPinNode is a node that has been marked as bad by Pin.Verify
type BadPinNode interface {
// Path is the path of the node
Path() Path
Path() ResolvedPath
// Err is the reason why the node has been marked as bad
Err() error
......
......@@ -10,7 +10,7 @@ import (
// UnixfsAPI is the basic interface to immutable files in IPFS
type UnixfsAPI interface {
// Add imports the data from the reader into merkledag file
Add(context.Context, io.Reader) (Path, error)
Add(context.Context, io.Reader) (ResolvedPath, error)
// Cat returns a reader for the file
Cat(context.Context, Path) (Reader, error)
......
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