coreapi: Object api review
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@3b4b26deb1bc75aefe85df809afca5e3e09f71c6 This commit was moved from ipfs/boxo@85688ca1e91bcfa3cc7441142e5391b760b2b43c
Łukasz Magiera committed
Dec 22, 2017 at 17:22 UTC
b031da2463b9041ca7d09064746067a69b8ae7f5
2 files changed
+63
-3
core/coreiface/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/coreiface/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
+}