Simplify Object.New, remove ipldnode.go

This commit was moved from ipfs/go-ipfs-http-client@0752a6ee63
This commit is contained in:
Łukasz Magiera 2019-02-14 19:15:21 +01:00
parent 93f684617a
commit cc964b4ab8
3 changed files with 18 additions and 130 deletions

View File

@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io/ioutil"
"sync"
"github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"

View File

@ -1,113 +0,0 @@
package httpapi
import (
"context"
"errors"
"io/ioutil"
"strconv"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
ipfspath "github.com/ipfs/go-path"
"github.com/ipfs/interface-go-ipfs-core"
)
type ipldNode struct {
ctx context.Context //TODO: should we re-consider adding ctx to ipld interfaces?
path iface.ResolvedPath
api *HttpApi
}
func (api *HttpApi) nodeFromPath(ctx context.Context, p iface.ResolvedPath) ipld.Node {
return &ipldNode{
ctx: ctx,
path: p,
api: api,
}
}
func (n *ipldNode) RawData() []byte {
r, err := n.api.Block().Get(n.ctx, n.path)
if err != nil {
panic(err) // TODO: eww, should we add errors too / better ideas?
}
b, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
return b
}
func (n *ipldNode) Cid() cid.Cid {
return n.path.Cid()
}
func (n *ipldNode) String() string {
return n.Cid().String()
}
func (n *ipldNode) Loggable() map[string]interface{} {
return nil //TODO: we can't really do better here, can we?
}
// TODO: should we use 'full'/real ipld codecs for this? js-ipfs-api does that.
// We can also give people a choice
func (n *ipldNode) Resolve(path []string) (interface{}, []string, error) {
p := ipfspath.Join([]string{n.path.String(), ipfspath.Join(path)})
var out interface{}
n.api.request("dag/get", p).Exec(n.ctx, &out)
// TODO: this is more than likely wrong, fix if we decide to stick with this 'http-ipld-node' hack
for len(path) > 0 {
switch o := out.(type) {
case map[string]interface{}:
v, ok := o[path[0]]
if !ok {
// TODO: ipld links
return nil, nil, errors.New("no element under this path")
}
out = v
case []interface{}:
n, err := strconv.ParseUint(path[0], 10, 32)
if err != nil {
return nil, nil, err
}
if len(o) < int(n) {
return nil, nil, errors.New("no element under this path")
}
out = o[n]
}
path = path[1:]
}
return out, path, nil
}
func (n *ipldNode) Tree(path string, depth int) []string {
panic("implement me")
}
func (n *ipldNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
panic("implement me")
}
func (n *ipldNode) Copy() ipld.Node {
panic("implement me")
}
func (n *ipldNode) Links() []*ipld.Link {
panic("implement me")
}
func (n *ipldNode) Stat() (*ipld.NodeStat, error) {
panic("implement me")
}
func (n *ipldNode) Size() (uint64, error) {
panic("implement me")
}
var _ ipld.Node = &ipldNode{}

View File

@ -3,12 +3,15 @@ package httpapi
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipld-format"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
dag "github.com/ipfs/go-merkledag"
ft "github.com/ipfs/go-unixfs"
"github.com/ipfs/interface-go-ipfs-core"
caopts "github.com/ipfs/interface-go-ipfs-core/options"
)
@ -19,24 +22,23 @@ type objectOut struct {
Hash string
}
func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (format.Node, error) {
func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (ipld.Node, error) {
options, err := caopts.ObjectNewOptions(opts...)
if err != nil {
return nil, err
}
var out objectOut
err = api.core().request("object/new", options.Type).Exec(ctx, &out)
if err != nil {
return nil, err
var n ipld.Node
switch options.Type {
case "empty":
n = new(dag.ProtoNode)
case "unixfs-dir":
n = ft.EmptyDirNode()
default:
return nil, fmt.Errorf("unknown object type: %s", options.Type)
}
c, err := cid.Parse(out.Hash)
if err != nil {
return nil, err
}
return api.core().nodeFromPath(ctx, iface.IpfsPath(c)), nil
return n, nil
}
func (api *ObjectAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.ObjectPutOption) (iface.ResolvedPath, error) {
@ -64,7 +66,7 @@ func (api *ObjectAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.Objec
return iface.IpfsPath(c), nil
}
func (api *ObjectAPI) Get(ctx context.Context, p iface.Path) (format.Node, error) {
func (api *ObjectAPI) Get(ctx context.Context, p iface.Path) (ipld.Node, error) {
r, err := api.core().Block().Get(ctx, p)
if err != nil {
return nil, err
@ -96,7 +98,7 @@ func (api *ObjectAPI) Data(ctx context.Context, p iface.Path) (io.Reader, error)
return b, nil
}
func (api *ObjectAPI) Links(ctx context.Context, p iface.Path) ([]*format.Link, error) {
func (api *ObjectAPI) Links(ctx context.Context, p iface.Path) ([]*ipld.Link, error) {
var out struct {
Links []struct {
Name string
@ -107,14 +109,14 @@ func (api *ObjectAPI) Links(ctx context.Context, p iface.Path) ([]*format.Link,
if err := api.core().request("object/links", p.String()).Exec(ctx, &out); err != nil {
return nil, err
}
res := make([]*format.Link, len(out.Links))
res := make([]*ipld.Link, len(out.Links))
for i, l := range out.Links {
c, err := cid.Parse(l.Hash)
if err != nil {
return nil, err
}
res[i] = &format.Link{
res[i] = &ipld.Link{
Cid: c,
Name: l.Name,
Size: l.Size,