@cryptotaxi247 / kubo / commits / f6663bd05

coreapi: implement object.Put

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Jan 4, 2018 at 17:20 UTC f6663bd05d1db97970a2a10ce32cc3010c272b18
4 files changed +57 -8
core/coreapi/interface/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/coreapi/interface/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
core/coreapi/object.go
+9 -3
@@ -3,7 +3,6 @@ package coreapi
3 import (
4 "bytes"
5 "context"
6 - "errors"
6 "io"
7 "io/ioutil"
8
@@ -15,6 +14,7 @@ import (
14 ft "github.com/ipfs/go-ipfs/unixfs"
15
16 node "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
17 + cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
18 )
19
20 type ObjectAPI struct {
@@ -43,8 +43,14 @@ func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (
43 return n, nil
44 }
45
46 -func (api *ObjectAPI) Put(context.Context, coreiface.Node) (coreiface.Path, error) {
47 - return nil, errors.New("todo") // TODO: implement using dag api.
46 +func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.Path, error) {
47 + options, err := caopts.ObjectPutOptions(opts...)
48 + if err != nil {
49 + return nil, err
50 + }
51 +
52 + dagApi := api.Dag()
53 + return dagApi.Put(ctx, src, dagApi.WithInputEnc(options.InputEnc), dagApi.WithCodec(cid.DagProtobuf))
54 }
55
56 func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
core/coredag/dagtransl.go
+9 -3
@@ -18,9 +18,10 @@ type InputEncParsers map[string]FormatParsers
18
19 // DefaultInputEncParsers is InputEncParser that is used everywhere
20 var DefaultInputEncParsers = InputEncParsers{
21 - "json": defaultJSONParsers,
22 - "raw": defaultRawParsers,
23 - "cbor": defaultCborParsers,
21 + "json": defaultJSONParsers,
22 + "raw": defaultRawParsers,
23 + "cbor": defaultCborParsers,
24 + "protobuf": defaultProtobufParsers,
25 }
26
27 var defaultJSONParsers = FormatParsers{
@@ -46,6 +47,11 @@ var defaultCborParsers = FormatParsers{
47 "dag-cbor": cborRawParser,
48 }
49
50 +var defaultProtobufParsers = FormatParsers{
51 + "protobuf": dagpbRawParser,
52 + "dag-pb": dagpbRawParser,
53 +}
54 +
55 // ParseInputs uses DefaultInputEncParsers to parse io.Reader described by
56 // input encoding and format to an instance of ipld Node
57 func ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {