Commit e787a157 authored by Steven Allen's avatar Steven Allen

make code-climate happier

License: MIT
Signed-off-by: default avatarSteven Allen <steven@stebalien.com>
parent 2fbf40a0
......@@ -124,7 +124,7 @@ func (n *ProtoNode) AddRawLink(name string, l *node.Link) error {
return nil
}
// Remove a link on this node by the given name
// RemoveNodeLink removes a link on this node by the given name.
func (n *ProtoNode) RemoveNodeLink(name string) error {
n.encoded = nil
good := make([]*node.Link, 0, len(n.links))
......@@ -146,7 +146,7 @@ func (n *ProtoNode) RemoveNodeLink(name string) error {
return nil
}
// Return a copy of the link with given name
// GetNodeLink returns a copy of the link with the given name.
func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
for _, l := range n.links {
if l.Name == name {
......@@ -160,6 +160,7 @@ func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
return nil, ErrLinkNotFound
}
// GetLinkedProtoNode returns a copy of the ProtoNode with the given name.
func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService, name string) (*ProtoNode, error) {
nd, err := n.GetLinkedNode(ctx, ds, name)
if err != nil {
......@@ -174,6 +175,7 @@ func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds node.DAGService,
return pbnd, nil
}
// GetLinkedNode returns a copy of the IPLD Node with the given name.
func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds node.DAGService, name string) (node.Node, error) {
lnk, err := n.GetNodeLink(name)
if err != nil {
......
......@@ -11,10 +11,12 @@ import (
node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
)
// Mock returns a new thread-safe, mock DAGService.
func Mock() node.DAGService {
return dag.NewDAGService(Bserv())
}
// Bserv returns a new, thread-safe, mock BlockService.
func Bserv() bsrv.BlockService {
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
return bsrv.New(bstore, offline.Exchange(bstore))
......
......@@ -37,6 +37,7 @@ func (c *Change) String() string {
}
}
// ApplyChange applies the requested changes to the given node in the given dag.
func ApplyChange(ctx context.Context, ds node.DAGService, nd *dag.ProtoNode, cs []*Change) (*dag.ProtoNode, error) {
e := NewDagEditor(nd, ds)
for _, c := range cs {
......
......@@ -27,6 +27,7 @@ type Editor struct {
src node.DAGService
}
// NewMemoryDagService returns a new, thread-safe in-memory DAGService.
func NewMemoryDagService() node.DAGService {
// build mem-datastore for editor's intermediary nodes
bs := bstore.NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
......@@ -34,7 +35,10 @@ func NewMemoryDagService() node.DAGService {
return dag.NewDAGService(bsrv)
}
// root is the node to be modified, source is the dagstore to pull nodes from (optional)
// NewDagEditor returns an ProtoNode editor.
//
// * root is the node to be modified
// * source is the dagstore to pull nodes from (optional)
func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
return &Editor{
root: root,
......@@ -43,17 +47,19 @@ func NewDagEditor(root *dag.ProtoNode, source node.DAGService) *Editor {
}
}
// GetNode returns the a copy of the root node being edited.
func (e *Editor) GetNode() *dag.ProtoNode {
return e.root.Copy().(*dag.ProtoNode)
}
// GetDagService returns the DAGService used by this editor.
func (e *Editor) GetDagService() node.DAGService {
return e.tmp
}
func addLink(ctx context.Context, ds node.DAGService, root *dag.ProtoNode, childname string, childnd node.Node) (*dag.ProtoNode, error) {
if childname == "" {
return nil, errors.New("cannot create link with no name!")
return nil, errors.New("cannot create link with no name")
}
// ensure that the node we are adding is in the dagservice
......@@ -188,6 +194,8 @@ func (e *Editor) rmLink(ctx context.Context, root *dag.ProtoNode, path []string)
return root, nil
}
// Finalize writes the new DAG to the given DAGService and returns the modified
// root node.
func (e *Editor) Finalize(ctx context.Context, ds node.DAGService) (*dag.ProtoNode, error) {
nd := e.GetNode()
err := copyDag(ctx, nd, e.tmp, ds)
......
......@@ -70,8 +70,8 @@ func TestInsertNode(t *testing.T) {
testInsert(t, e, "a/b/c/d/f", "baz", true, "")
testInsert(t, e, "a/b/c/d/f", "bar", true, "")
testInsert(t, e, "", "bar", true, "cannot create link with no name!")
testInsert(t, e, "////", "slashes", true, "cannot create link with no name!")
testInsert(t, e, "", "bar", true, "cannot create link with no name")
testInsert(t, e, "////", "slashes", true, "cannot create link with no name")
c := e.GetNode().Cid()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment