Simplify Object.New, remove ipldnode.go
This commit was moved from ipfs/go-ipfs-http-client@0752a6ee63a2ec42aabb6c9acac6ef6c4a94af38
Łukasz Magiera committed
Feb 14, 2019 at 19:15 UTC
cc964b4ab8ea5d059be2c05e64ecdca851dcd8b6
3 files changed
+18
-130
client/httpapi/dag.go
-1
@@ -5,7 +5,6 @@ import (
5
"context"
6
"fmt"
7
"io/ioutil"
8
- "sync"
8
9
"github.com/ipfs/go-block-format"
10
"github.com/ipfs/go-cid"
client/httpapi/ipldnode.go
deleted
-113
@@ -1,113 +0,0 @@
1
-package httpapi
2
-
3
-import (
4
- "context"
5
- "errors"
6
- "io/ioutil"
7
- "strconv"
8
-
9
- "github.com/ipfs/go-cid"
10
- ipld "github.com/ipfs/go-ipld-format"
11
- ipfspath "github.com/ipfs/go-path"
12
- "github.com/ipfs/interface-go-ipfs-core"
13
-)
14
-
15
-type ipldNode struct {
16
- ctx context.Context //TODO: should we re-consider adding ctx to ipld interfaces?
17
- path iface.ResolvedPath
18
- api *HttpApi
19
-}
20
-
21
-func (api *HttpApi) nodeFromPath(ctx context.Context, p iface.ResolvedPath) ipld.Node {
22
- return &ipldNode{
23
- ctx: ctx,
24
- path: p,
25
- api: api,
26
- }
27
-}
28
-
29
-func (n *ipldNode) RawData() []byte {
30
- r, err := n.api.Block().Get(n.ctx, n.path)
31
- if err != nil {
32
- panic(err) // TODO: eww, should we add errors too / better ideas?
33
- }
34
-
35
- b, err := ioutil.ReadAll(r)
36
- if err != nil {
37
- panic(err)
38
- }
39
-
40
- return b
41
-}
42
-
43
-func (n *ipldNode) Cid() cid.Cid {
44
- return n.path.Cid()
45
-}
46
-
47
-func (n *ipldNode) String() string {
48
- return n.Cid().String()
49
-}
50
-
51
-func (n *ipldNode) Loggable() map[string]interface{} {
52
- return nil //TODO: we can't really do better here, can we?
53
-}
54
-
55
-// TODO: should we use 'full'/real ipld codecs for this? js-ipfs-api does that.
56
-// We can also give people a choice
57
-func (n *ipldNode) Resolve(path []string) (interface{}, []string, error) {
58
- p := ipfspath.Join([]string{n.path.String(), ipfspath.Join(path)})
59
-
60
- var out interface{}
61
- n.api.request("dag/get", p).Exec(n.ctx, &out)
62
-
63
- // TODO: this is more than likely wrong, fix if we decide to stick with this 'http-ipld-node' hack
64
- for len(path) > 0 {
65
- switch o := out.(type) {
66
- case map[string]interface{}:
67
- v, ok := o[path[0]]
68
- if !ok {
69
- // TODO: ipld links
70
- return nil, nil, errors.New("no element under this path")
71
- }
72
- out = v
73
- case []interface{}:
74
- n, err := strconv.ParseUint(path[0], 10, 32)
75
- if err != nil {
76
- return nil, nil, err
77
- }
78
- if len(o) < int(n) {
79
- return nil, nil, errors.New("no element under this path")
80
- }
81
- out = o[n]
82
- }
83
- path = path[1:]
84
- }
85
-
86
- return out, path, nil
87
-}
88
-
89
-func (n *ipldNode) Tree(path string, depth int) []string {
90
- panic("implement me")
91
-}
92
-
93
-func (n *ipldNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
94
- panic("implement me")
95
-}
96
-
97
-func (n *ipldNode) Copy() ipld.Node {
98
- panic("implement me")
99
-}
100
-
101
-func (n *ipldNode) Links() []*ipld.Link {
102
- panic("implement me")
103
-}
104
-
105
-func (n *ipldNode) Stat() (*ipld.NodeStat, error) {
106
- panic("implement me")
107
-}
108
-
109
-func (n *ipldNode) Size() (uint64, error) {
110
- panic("implement me")
111
-}
112
-
113
-var _ ipld.Node = &ipldNode{}
client/httpapi/object.go
+18
-16
@@ -3,12 +3,15 @@ package httpapi
3
import (
4
"bytes"
5
"context"
6
+ "fmt"
7
"io"
8
"io/ioutil"
9
10
"github.com/ipfs/go-cid"
10
- "github.com/ipfs/go-ipld-format"
11
+ ipld "github.com/ipfs/go-ipld-format"
12
"github.com/ipfs/go-merkledag"
13
+ dag "github.com/ipfs/go-merkledag"
14
+ ft "github.com/ipfs/go-unixfs"
15
"github.com/ipfs/interface-go-ipfs-core"
16
caopts "github.com/ipfs/interface-go-ipfs-core/options"
17
)
@@ -19,24 +22,23 @@ type objectOut struct {
22
Hash string
23
}
24
22
-func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (format.Node, error) {
25
+func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (ipld.Node, error) {
26
options, err := caopts.ObjectNewOptions(opts...)
27
if err != nil {
28
return nil, err
29
}
30
28
- var out objectOut
29
- err = api.core().request("object/new", options.Type).Exec(ctx, &out)
30
- if err != nil {
31
- return nil, err
32
- }
33
-
34
- c, err := cid.Parse(out.Hash)
35
- if err != nil {
36
- return nil, err
31
+ var n ipld.Node
32
+ switch options.Type {
33
+ case "empty":
34
+ n = new(dag.ProtoNode)
35
+ case "unixfs-dir":
36
+ n = ft.EmptyDirNode()
37
+ default:
38
+ return nil, fmt.Errorf("unknown object type: %s", options.Type)
39
}
40
39
- return api.core().nodeFromPath(ctx, iface.IpfsPath(c)), nil
41
+ return n, nil
42
}
43
44
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
66
return iface.IpfsPath(c), nil
67
}
68
67
-func (api *ObjectAPI) Get(ctx context.Context, p iface.Path) (format.Node, error) {
69
+func (api *ObjectAPI) Get(ctx context.Context, p iface.Path) (ipld.Node, error) {
70
r, err := api.core().Block().Get(ctx, p)
71
if err != nil {
72
return nil, err
@@ -96,7 +98,7 @@ func (api *ObjectAPI) Data(ctx context.Context, p iface.Path) (io.Reader, error)
98
return b, nil
99
}
100
99
-func (api *ObjectAPI) Links(ctx context.Context, p iface.Path) ([]*format.Link, error) {
101
+func (api *ObjectAPI) Links(ctx context.Context, p iface.Path) ([]*ipld.Link, error) {
102
var out struct {
103
Links []struct {
104
Name string
@@ -107,14 +109,14 @@ func (api *ObjectAPI) Links(ctx context.Context, p iface.Path) ([]*format.Link,
109
if err := api.core().request("object/links", p.String()).Exec(ctx, &out); err != nil {
110
return nil, err
111
}
110
- res := make([]*format.Link, len(out.Links))
112
+ res := make([]*ipld.Link, len(out.Links))
113
for i, l := range out.Links {
114
c, err := cid.Parse(l.Hash)
115
if err != nil {
116
return nil, err
117
}
118
117
- res[i] = &format.Link{
119
+ res[i] = &ipld.Link{
120
Cid: c,
121
Name: l.Name,
122
Size: l.Size,