coreapi: Object api review
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Dec 22, 2017 at 17:22 UTC
ae949e3a933187212e65181d124418018e66a325
4 files changed
+104
-22
core/coreapi/coreapi.go
+1
-1
@@ -42,7 +42,7 @@ func (api *CoreAPI) Key() coreiface.KeyAPI {
42
}
43
44
func (api *CoreAPI) Object() coreiface.ObjectAPI {
45
- return (*ObjectAPI)(api)
45
+ return &ObjectAPI{api, nil}
46
}
47
48
// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
core/coreapi/interface/interface.go
+7
-3
@@ -193,14 +193,18 @@ type KeyAPI interface {
193
194
//TODO: Should this use paths instead of cids?
195
type ObjectAPI interface {
196
- New(ctx context.Context) (Node, error)
197
- Put(context.Context, Node) error
196
+ New(context.Context, ...options.ObjectNewOption) (Node, error)
197
+ WithType(string) options.ObjectNewOption
198
+
199
+ Put(context.Context, Node) (Path, error)
200
Get(context.Context, Path) (Node, error)
201
Data(context.Context, Path) (io.Reader, error)
202
Links(context.Context, Path) ([]*Link, error)
203
Stat(context.Context, Path) (*ObjectStat, error)
204
203
- AddLink(ctx context.Context, base Path, name string, child Path, create bool) (Node, error) //TODO: make create optional
205
+ AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Node, error)
206
+ WithCreate(create bool) options.ObjectAddLinkOption
207
+
208
RmLink(context.Context, Path, string) (Node, error)
209
AppendData(context.Context, Path, io.Reader) (Node, error)
210
SetData(context.Context, Path, io.Reader) (Node, error)
core/coreapi/interface/options/object.go
new
+56
@@ -0,0 +1,56 @@
1
+package options
2
+
3
+type ObjectNewSettings struct {
4
+ Type string
5
+}
6
+
7
+type ObjectAddLinkSettings struct {
8
+ Create bool
9
+}
10
+
11
+type ObjectNewOption func(*ObjectNewSettings) error
12
+type ObjectAddLinkOption func(*ObjectAddLinkSettings) error
13
+
14
+func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) {
15
+ options := &ObjectNewSettings{
16
+ Type: "empty",
17
+ }
18
+
19
+ for _, opt := range opts {
20
+ err := opt(options)
21
+ if err != nil {
22
+ return nil, err
23
+ }
24
+ }
25
+ return options, nil
26
+}
27
+
28
+func ObjectAddLinkOptions(opts ...ObjectAddLinkOption) (*ObjectAddLinkSettings, error) {
29
+ options := &ObjectAddLinkSettings{
30
+ Create: false,
31
+ }
32
+
33
+ for _, opt := range opts {
34
+ err := opt(options)
35
+ if err != nil {
36
+ return nil, err
37
+ }
38
+ }
39
+ return options, nil
40
+}
41
+
42
+type ObjectOptions struct{}
43
+
44
+func (api *ObjectOptions) WithType(t string) ObjectNewOption {
45
+ return func(settings *ObjectNewSettings) error {
46
+ settings.Type = t
47
+ return nil
48
+ }
49
+}
50
+
51
+func (api *ObjectOptions) WithCreate(create bool) ObjectAddLinkOption {
52
+ return func(settings *ObjectAddLinkSettings) error {
53
+ settings.Create = create
54
+ return nil
55
+ }
56
+}
core/coreapi/object.go
+40
-18
@@ -7,27 +7,44 @@ import (
7
"io"
8
"io/ioutil"
9
10
- "github.com/ipfs/go-ipfs/merkledag/utils"
10
+ caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11
+ dagutils "github.com/ipfs/go-ipfs/merkledag/utils"
12
13
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
14
dag "github.com/ipfs/go-ipfs/merkledag"
15
ft "github.com/ipfs/go-ipfs/unixfs"
16
+
17
+ node "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
18
)
19
17
-type ObjectAPI CoreAPI
20
+type ObjectAPI struct {
21
+ *CoreAPI
22
+ *caopts.ObjectOptions
23
+}
24
19
-func (api *ObjectAPI) New(ctx context.Context) (coreiface.Node, error) {
20
- node := new(dag.ProtoNode)
25
+func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (coreiface.Node, error) {
26
+ options, err := caopts.ObjectNewOptions(opts...)
27
+ if err != nil {
28
+ return nil, err
29
+ }
30
22
- _, err := api.node.DAG.Add(node)
31
+ var n node.Node
32
+ switch options.Type {
33
+ case "empty":
34
+ n = new(dag.ProtoNode)
35
+ case "unixfs-dir":
36
+ n = ft.EmptyDirNode()
37
+ }
38
+
39
+ _, err = api.node.DAG.Add(n)
40
if err != nil {
41
return nil, err
42
}
26
- return node, nil
43
+ return n, nil
44
}
45
29
-func (api *ObjectAPI) Put(context.Context, coreiface.Node) error {
30
- return errors.New("todo") // TODO: what should this method take? Should we just redir to dag-put?f
46
+func (api *ObjectAPI) Put(context.Context, coreiface.Node) (coreiface.Path, error) {
47
+ return nil, errors.New("todo") // TODO: implement using dag api.
48
}
49
50
func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
@@ -86,8 +103,13 @@ func (api *ObjectAPI) Stat(ctx context.Context, path coreiface.Path) (*coreiface
103
return out, nil
104
}
105
89
-func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, create bool) (coreiface.Node, error) {
90
- rootNd, err := api.core().ResolveNode(ctx, base)
106
+func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.Node, error) {
107
+ options, err := caopts.ObjectAddLinkOptions(opts...)
108
+ if err != nil {
109
+ return nil, err
110
+ }
111
+
112
+ baseNd, err := api.core().ResolveNode(ctx, base)
113
if err != nil {
114
return nil, err
115
}
@@ -97,17 +119,17 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
119
return nil, err
120
}
121
100
- rootPb, ok := rootNd.(*dag.ProtoNode)
122
+ basePb, ok := baseNd.(*dag.ProtoNode)
123
if !ok {
124
return nil, dag.ErrNotProtobuf
125
}
126
127
var createfunc func() *dag.ProtoNode
106
- if create {
128
+ if options.Create {
129
createfunc = ft.EmptyDirNode
130
}
131
110
- e := dagutils.NewDagEditor(rootPb, api.node.DAG)
132
+ e := dagutils.NewDagEditor(basePb, api.node.DAG)
133
134
err = e.InsertNodeAtPath(ctx, name, childNd, createfunc)
135
if err != nil {
@@ -122,18 +144,18 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
144
return nnode, nil
145
}
146
125
-func (api *ObjectAPI) RmLink(ctx context.Context, root coreiface.Path, link string) (coreiface.Node, error) {
126
- rootNd, err := api.core().ResolveNode(ctx, root)
147
+func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Node, error) {
148
+ baseNd, err := api.core().ResolveNode(ctx, base)
149
if err != nil {
150
return nil, err
151
}
152
131
- rootPb, ok := rootNd.(*dag.ProtoNode)
153
+ basePb, ok := baseNd.(*dag.ProtoNode)
154
if !ok {
155
return nil, dag.ErrNotProtobuf
156
}
157
136
- e := dagutils.NewDagEditor(rootPb, api.node.DAG)
158
+ e := dagutils.NewDagEditor(basePb, api.node.DAG)
159
160
err = e.RmLink(ctx, link)
161
if err != nil {
@@ -186,5 +208,5 @@ func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.R
208
}
209
210
func (api *ObjectAPI) core() coreiface.CoreAPI {
189
- return (*CoreAPI)(api)
211
+ return api.CoreAPI
212
}