coreapi: implement object.Put
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com> This commit was moved from ipfs/interface-go-ipfs-core@104bc87d13e259e45b68c3d00231e140a7b88fb6 This commit was moved from ipfs/boxo@8162a16f7c173b07fa645d5a20121f1ad29e5198
Łukasz Magiera committed
Jan 4, 2018 at 17:20 UTC
540ec53bf8308b9b45d1ebbfc6e23d775ec779f4
2 files changed
+39
-2
core/coreiface/interface.go
+13
-2
@@ -64,6 +64,9 @@ type CoreAPI interface {
64
// Key returns an implementation of Key API.
65
Key() KeyAPI
66
67
+ // ObjectAPI returns an implementation of Object API
68
+ Object() ObjectAPI
69
+
70
// ResolvePath resolves the path using Unixfs resolver
71
ResolvePath(context.Context, Path) (Path, error)
72
@@ -205,8 +208,16 @@ type ObjectAPI interface {
208
// * 'unixfs-dir' - Empty UnixFS directory
209
WithType(string) options.ObjectNewOption
210
208
- // Put imports the node into merkledag
209
- Put(context.Context, Node) (Path, error)
211
+ // Put imports the data into merkledag
212
+ Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)
213
+
214
+ // WithInputEnc is an option for Put which specifies the input encoding of the
215
+ // data. Default is "json".
216
+ //
217
+ // Supported encodings:
218
+ // * "protobuf"
219
+ // * "json"
220
+ WithInputEnc(e string) options.ObjectPutOption
221
222
// Get returns the node for the path
223
Get(context.Context, Path) (Node, error)
core/coreiface/options/object.go
+26
@@ -4,11 +4,16 @@ type ObjectNewSettings struct {
4
Type string
5
}
6
7
+type ObjectPutSettings struct {
8
+ InputEnc string
9
+}
10
+
11
type ObjectAddLinkSettings struct {
12
Create bool
13
}
14
15
type ObjectNewOption func(*ObjectNewSettings) error
16
+type ObjectPutOption func(*ObjectPutSettings) error
17
type ObjectAddLinkOption func(*ObjectAddLinkSettings) error
18
19
func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) {
@@ -25,6 +30,20 @@ func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) {
30
return options, nil
31
}
32
33
+func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
34
+ options := &ObjectPutSettings{
35
+ InputEnc: "json",
36
+ }
37
+
38
+ for _, opt := range opts {
39
+ err := opt(options)
40
+ if err != nil {
41
+ return nil, err
42
+ }
43
+ }
44
+ return options, nil
45
+}
46
+
47
func ObjectAddLinkOptions(opts ...ObjectAddLinkOption) (*ObjectAddLinkSettings, error) {
48
options := &ObjectAddLinkSettings{
49
Create: false,
@@ -48,6 +67,13 @@ func (api *ObjectOptions) WithType(t string) ObjectNewOption {
67
}
68
}
69
70
+func (api *ObjectOptions) WithInputEnc(e string) ObjectPutOption {
71
+ return func(settings *ObjectPutSettings) error {
72
+ settings.InputEnc = e
73
+ return nil
74
+ }
75
+}
76
+
77
func (api *ObjectOptions) WithCreate(create bool) ObjectAddLinkOption {
78
return func(settings *ObjectAddLinkSettings) error {
79
settings.Create = create