mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 09:52:20 +08:00
Add additional link manipulation functions
// AddRawLink adds a link to this node AddRawLink(name string, lnk *Link) error // Return a copy of the link with given name GetNodeLink(name string) (*Link, error)
This commit is contained in:
@ -88,18 +88,21 @@ func (l *Link) GetNode(serv DAGService) (*Node, error) {
|
||||
// AddNodeLink adds a link to another node.
|
||||
func (n *Node) AddNodeLink(name string, that *Node) error {
|
||||
n.encoded = nil
|
||||
|
||||
lnk, err := MakeLink(that)
|
||||
|
||||
lnk.Name = name
|
||||
lnk.Node = that
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lnk.Name = name
|
||||
lnk.Node = that
|
||||
|
||||
n.Links = append(n.Links, lnk)
|
||||
n.AddRawLink(name, lnk)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddNodeLink adds a link to another node. without keeping a reference to
|
||||
// AddNodeLinkClean adds a link to another node. without keeping a reference to
|
||||
// the child node
|
||||
func (n *Node) AddNodeLinkClean(name string, that *Node) error {
|
||||
n.encoded = nil
|
||||
@ -107,9 +110,21 @@ func (n *Node) AddNodeLinkClean(name string, that *Node) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lnk.Name = name
|
||||
n.AddRawLink(name, lnk)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRawLink adds a copy of a link to this node
|
||||
func (n *Node) AddRawLink(name string, l *Link) error {
|
||||
n.encoded = nil
|
||||
n.Links = append(n.Links, &Link{
|
||||
Name: name,
|
||||
Size: l.Size,
|
||||
Hash: l.Hash,
|
||||
Node: l.Node,
|
||||
})
|
||||
|
||||
n.Links = append(n.Links, lnk)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -125,6 +140,21 @@ func (n *Node) RemoveNodeLink(name string) error {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
// Return a copy of the link with given name
|
||||
func (n *Node) GetNodeLink(name string) (*Link, error) {
|
||||
for _, l := range n.Links {
|
||||
if l.Name == name {
|
||||
return &Link{
|
||||
Name: l.Name,
|
||||
Size: l.Size,
|
||||
Hash: l.Hash,
|
||||
Node: l.Node,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
// Copy returns a copy of the node.
|
||||
// NOTE: does not make copies of Node objects in the links.
|
||||
func (n *Node) Copy() *Node {
|
||||
|
Reference in New Issue
Block a user