mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
coreapi: implement object.Put
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -64,6 +64,9 @@ type CoreAPI interface {
|
|||||||
// Key returns an implementation of Key API.
|
// Key returns an implementation of Key API.
|
||||||
Key() KeyAPI
|
Key() KeyAPI
|
||||||
|
|
||||||
|
// ObjectAPI returns an implementation of Object API
|
||||||
|
Object() ObjectAPI
|
||||||
|
|
||||||
// ResolvePath resolves the path using Unixfs resolver
|
// ResolvePath resolves the path using Unixfs resolver
|
||||||
ResolvePath(context.Context, Path) (Path, error)
|
ResolvePath(context.Context, Path) (Path, error)
|
||||||
|
|
||||||
@ -205,8 +208,16 @@ type ObjectAPI interface {
|
|||||||
// * 'unixfs-dir' - Empty UnixFS directory
|
// * 'unixfs-dir' - Empty UnixFS directory
|
||||||
WithType(string) options.ObjectNewOption
|
WithType(string) options.ObjectNewOption
|
||||||
|
|
||||||
// Put imports the node into merkledag
|
// Put imports the data into merkledag
|
||||||
Put(context.Context, Node) (Path, error)
|
Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)
|
||||||
|
|
||||||
|
// WithInputEnc is an option for Put which specifies the input encoding of the
|
||||||
|
// data. Default is "json".
|
||||||
|
//
|
||||||
|
// Supported encodings:
|
||||||
|
// * "protobuf"
|
||||||
|
// * "json"
|
||||||
|
WithInputEnc(e string) options.ObjectPutOption
|
||||||
|
|
||||||
// Get returns the node for the path
|
// Get returns the node for the path
|
||||||
Get(context.Context, Path) (Node, error)
|
Get(context.Context, Path) (Node, error)
|
||||||
|
@ -4,11 +4,16 @@ type ObjectNewSettings struct {
|
|||||||
Type string
|
Type string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ObjectPutSettings struct {
|
||||||
|
InputEnc string
|
||||||
|
}
|
||||||
|
|
||||||
type ObjectAddLinkSettings struct {
|
type ObjectAddLinkSettings struct {
|
||||||
Create bool
|
Create bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type ObjectNewOption func(*ObjectNewSettings) error
|
type ObjectNewOption func(*ObjectNewSettings) error
|
||||||
|
type ObjectPutOption func(*ObjectPutSettings) error
|
||||||
type ObjectAddLinkOption func(*ObjectAddLinkSettings) error
|
type ObjectAddLinkOption func(*ObjectAddLinkSettings) error
|
||||||
|
|
||||||
func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) {
|
func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) {
|
||||||
@ -25,6 +30,20 @@ func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) {
|
|||||||
return options, nil
|
return options, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
|
||||||
|
options := &ObjectPutSettings{
|
||||||
|
InputEnc: "json",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
err := opt(options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return options, nil
|
||||||
|
}
|
||||||
|
|
||||||
func ObjectAddLinkOptions(opts ...ObjectAddLinkOption) (*ObjectAddLinkSettings, error) {
|
func ObjectAddLinkOptions(opts ...ObjectAddLinkOption) (*ObjectAddLinkSettings, error) {
|
||||||
options := &ObjectAddLinkSettings{
|
options := &ObjectAddLinkSettings{
|
||||||
Create: false,
|
Create: false,
|
||||||
@ -48,6 +67,13 @@ func (api *ObjectOptions) WithType(t string) ObjectNewOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (api *ObjectOptions) WithInputEnc(e string) ObjectPutOption {
|
||||||
|
return func(settings *ObjectPutSettings) error {
|
||||||
|
settings.InputEnc = e
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (api *ObjectOptions) WithCreate(create bool) ObjectAddLinkOption {
|
func (api *ObjectOptions) WithCreate(create bool) ObjectAddLinkOption {
|
||||||
return func(settings *ObjectAddLinkSettings) error {
|
return func(settings *ObjectAddLinkSettings) error {
|
||||||
settings.Create = create
|
settings.Create = create
|
||||||
|
@ -3,7 +3,6 @@ package coreapi
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
@ -15,6 +14,7 @@ import (
|
|||||||
ft "github.com/ipfs/go-ipfs/unixfs"
|
ft "github.com/ipfs/go-ipfs/unixfs"
|
||||||
|
|
||||||
node "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
|
node "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
|
||||||
|
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ObjectAPI struct {
|
type ObjectAPI struct {
|
||||||
@ -43,8 +43,14 @@ func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (
|
|||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *ObjectAPI) Put(context.Context, coreiface.Node) (coreiface.Path, error) {
|
func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.ObjectPutOption) (coreiface.Path, error) {
|
||||||
return nil, errors.New("todo") // TODO: implement using dag api.
|
options, err := caopts.ObjectPutOptions(opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
dagApi := api.Dag()
|
||||||
|
return dagApi.Put(ctx, src, dagApi.WithInputEnc(options.InputEnc), dagApi.WithCodec(cid.DagProtobuf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
|
func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
|
||||||
|
@ -18,9 +18,10 @@ type InputEncParsers map[string]FormatParsers
|
|||||||
|
|
||||||
// DefaultInputEncParsers is InputEncParser that is used everywhere
|
// DefaultInputEncParsers is InputEncParser that is used everywhere
|
||||||
var DefaultInputEncParsers = InputEncParsers{
|
var DefaultInputEncParsers = InputEncParsers{
|
||||||
"json": defaultJSONParsers,
|
"json": defaultJSONParsers,
|
||||||
"raw": defaultRawParsers,
|
"raw": defaultRawParsers,
|
||||||
"cbor": defaultCborParsers,
|
"cbor": defaultCborParsers,
|
||||||
|
"protobuf": defaultProtobufParsers,
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultJSONParsers = FormatParsers{
|
var defaultJSONParsers = FormatParsers{
|
||||||
@ -46,6 +47,11 @@ var defaultCborParsers = FormatParsers{
|
|||||||
"dag-cbor": cborRawParser,
|
"dag-cbor": cborRawParser,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var defaultProtobufParsers = FormatParsers{
|
||||||
|
"protobuf": dagpbRawParser,
|
||||||
|
"dag-pb": dagpbRawParser,
|
||||||
|
}
|
||||||
|
|
||||||
// ParseInputs uses DefaultInputEncParsers to parse io.Reader described by
|
// ParseInputs uses DefaultInputEncParsers to parse io.Reader described by
|
||||||
// input encoding and format to an instance of ipld Node
|
// input encoding and format to an instance of ipld Node
|
||||||
func ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
|
func ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
|
||||||
|
Reference in New Issue
Block a user