Skip to content

Commit

Permalink
coreapi: object API tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Jan 5, 2018
1 parent 0b95ab1 commit a65317d
Show file tree
Hide file tree
Showing 4 changed files with 524 additions and 15 deletions.
18 changes: 13 additions & 5 deletions core/coreapi/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ type ObjectAPI interface {
// * "json"
WithInputEnc(e string) options.ObjectPutOption

// WithDataType specifies the encoding of data field when using Josn or XML
// input encoding.
//
// Supported types:
// * "text" (default)
// * "base64"
WithDataType(t string) options.ObjectPutOption

// Get returns the node for the path
Get(context.Context, Path) (Node, error)

Expand All @@ -221,20 +229,20 @@ type ObjectAPI interface {
// AddLink adds a link under the specified path. child path can point to a
// subdirectory within the patent which must be present (can be overridden
// with WithCreate option).
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Node, error)
AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Path, error)

// WithCreate is an option for AddLink which specifies whether create required
// directories for the child
WithCreate(create bool) options.ObjectAddLinkOption

// RmLink removes a link from the node
RmLink(ctx context.Context, base Path, link string) (Node, error)
RmLink(ctx context.Context, base Path, link string) (Path, error)

// AppendData appends data to the node
AppendData(context.Context, Path, io.Reader) (Node, error)
AppendData(context.Context, Path, io.Reader) (Path, error)

// SetData sets the data contained in the node
SetData(context.Context, Path, io.Reader) (Node, error)
SetData(context.Context, Path, io.Reader) (Path, error)
}

// ObjectStat provides information about dag nodes
Expand All @@ -254,7 +262,7 @@ type ObjectStat struct {
// DataSize is the size of data block section
DataSize int

// CumulativeSize is size of node
// CumulativeSize is size of the tree (BlockSize + link sizes)
CumulativeSize int
}

Expand Down
9 changes: 9 additions & 0 deletions core/coreapi/interface/options/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type ObjectNewSettings struct {

type ObjectPutSettings struct {
InputEnc string
DataType string
}

type ObjectAddLinkSettings struct {
Expand Down Expand Up @@ -33,6 +34,7 @@ func ObjectNewOptions(opts ...ObjectNewOption) (*ObjectNewSettings, error) {
func ObjectPutOptions(opts ...ObjectPutOption) (*ObjectPutSettings, error) {
options := &ObjectPutSettings{
InputEnc: "json",
DataType: "text",
}

for _, opt := range opts {
Expand Down Expand Up @@ -74,6 +76,13 @@ func (api *ObjectOptions) WithInputEnc(e string) ObjectPutOption {
}
}

func (api *ObjectOptions) WithDataType(t string) ObjectPutOption {
return func(settings *ObjectPutSettings) error {
settings.DataType = t
return nil
}
}

func (api *ObjectOptions) WithCreate(create bool) ObjectAddLinkOption {
return func(settings *ObjectAddLinkSettings) error {
settings.Create = create
Expand Down
127 changes: 117 additions & 10 deletions core/coreapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@ import (
dag "github.com/ipfs/go-ipfs/merkledag"
ft "github.com/ipfs/go-ipfs/unixfs"

"encoding/base64"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
node "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
)

const inputLimit = 2 << 20

type ObjectAPI struct {
*CoreAPI
*caopts.ObjectOptions
}

type Link struct {
Name, Hash string
Size uint64
}

type Node struct {
Links []Link
Data string
}

func (api *ObjectAPI) New(ctx context.Context, opts ...caopts.ObjectNewOption) (coreiface.Node, error) {
options, err := caopts.ObjectNewOptions(opts...)
if err != nil {
Expand Down Expand Up @@ -49,8 +66,66 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
return nil, err
}

dagApi := api.Dag()
return dagApi.Put(ctx, src, dagApi.WithInputEnc(options.InputEnc), dagApi.WithCodec(cid.DagProtobuf))
data, err := ioutil.ReadAll(io.LimitReader(src, inputLimit+10))
if err != nil {
return nil, err
}

var dagnode *dag.ProtoNode
switch options.InputEnc {
case "json":
node := new(Node)
err = json.Unmarshal(data, node)
if err != nil {
return nil, err
}

// check that we have data in the Node to add
// otherwise we will add the empty object without raising an error
if nodeEmpty(node) {
return nil, errors.New("no data or links in this node")
}

dagnode, err = deserializeNode(node, options.DataType)
if err != nil {
return nil, err
}

case "protobuf":
dagnode, err = dag.DecodeProtobuf(data)

case "xml":
node := new(Node)
err = xml.Unmarshal(data, node)
if err != nil {
return nil, err
}

// check that we have data in the Node to add
// otherwise we will add the empty object without raising an error
if nodeEmpty(node) {
return nil, errors.New("no data or links in this node")
}

dagnode, err = deserializeNode(node, options.DataType)
if err != nil {
return nil, err
}

default:
return nil, errors.New("unknown object encoding")
}

if err != nil {
return nil, err
}

_, err = api.node.DAG.Add(dagnode)
if err != nil {
return nil, err
}

return ParseCid(dagnode.Cid()), nil
}

func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (coreiface.Node, error) {
Expand Down Expand Up @@ -109,7 +184,7 @@ func (api *ObjectAPI) Stat(ctx context.Context, path coreiface.Path) (*coreiface
return out, nil
}

func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.Node, error) {
func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name string, child coreiface.Path, opts ...caopts.ObjectAddLinkOption) (coreiface.Path, error) {
options, err := caopts.ObjectAddLinkOptions(opts...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -147,10 +222,10 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
return nil, err
}

return nnode, nil
return ParseCid(nnode.Cid()), nil
}

func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Node, error) {
func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Path, error) {
baseNd, err := api.core().ResolveNode(ctx, base)
if err != nil {
return nil, err
Expand All @@ -173,18 +248,18 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri
return nil, err
}

return nnode, nil
return ParseCid(nnode.Cid()), nil
}

func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Node, error) {
func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
return api.patchData(ctx, path, r, true)
}

func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Node, error) {
func (api *ObjectAPI) SetData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
return api.patchData(ctx, path, r, false)
}

func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.Node, error) {
func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.Reader, appendData bool) (coreiface.Path, error) {
nd, err := api.core().ResolveNode(ctx, path)
if err != nil {
return nil, err
Expand All @@ -210,9 +285,41 @@ func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.R
return nil, err
}

return pbnd, nil
return ParseCid(pbnd.Cid()), nil
}

func (api *ObjectAPI) core() coreiface.CoreAPI {
return api.CoreAPI
}

func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) {
dagnode := new(dag.ProtoNode)
switch dataFieldEncoding {
case "text":
dagnode.SetData([]byte(nd.Data))
case "base64":
data, _ := base64.StdEncoding.DecodeString(nd.Data)
dagnode.SetData(data)
default:
return nil, fmt.Errorf("Unkown data field encoding")
}

dagnode.SetLinks(make([]*node.Link, len(nd.Links)))
for i, link := range nd.Links {
c, err := cid.Decode(link.Hash)
if err != nil {
return nil, err
}
dagnode.Links()[i] = &node.Link{
Name: link.Name,
Size: link.Size,
Cid: c,
}
}

return dagnode, nil
}

func nodeEmpty(node *Node) bool {
return node.Data == "" && len(node.Links) == 0
}
Loading

0 comments on commit a65317d

Please sign in to comment.