Documentation ¶
Overview ¶
Package nestedset provides types and functions for manage nested sets.
Usage:
package main import ( "github.com/juggleru/nestedset" "fmt" "strings" ) type MySomeType struct { *nestedset.Node // add it to your any type // type vars MyId string MyName string } // Init it in instance creation func NewMySomeType() *MySomeType { return &MySomeType{ Node: nestedset.NewNode(), } } // You can redefine NodeInterface functions // Return your type func (t *MySomeType) Type() string { return "mysometype" } // Return your inner id func (t *MySomeType) Id() string { return t.MyId } // Return your inner name func (t *MySomeType) Name() string { return t.MyName } // Set your inner id or generate it func (t *MySomeType) SetId(id int) { t.MyId = id // or t.MyId = getSomeNewId() } // Set your inner name func (t *MySomeType) SetName(name string) { t.MyName = name } func main() { ns := nestedset.NewNestedSet() // create 3 new nodes node1 := NewMySomeType() node1.MyName = "Node 1" node2 := NewMySomeType() node2.MyName = "Node 2" node3 := NewMySomeType() node3.MyName = "Node 3" ns.Add(node1, nil) // add node to root ns.Add(node2, nil) // add node to root ns.Add(node3, node1) // add node to node1 ns.Move(node3, node2) // move node3 from node1 to node2 branch := ns.Branch(nil) // get full tree // print tree with indents for _, n := range branch { fmt.Print(strings.Repeat("..", n.Level())) fmt.Printf("%s lvl:%d, left:%d, right:%d\n", n.Name(), n.Level(), n.Left(), n.Right()) } }
Index ¶
- type NestedSet
- func (s *NestedSet) Add(newNode, parent NodeInterface) error
- func (s *NestedSet) Branch(node NodeInterface) []NodeInterface
- func (s *NestedSet) Delete(node NodeInterface) error
- func (s *NestedSet) FindById(id int64) NodeInterface
- func (s NestedSet) MarshalJSON() ([]byte, error)
- func (s *NestedSet) Move(node, parent NodeInterface) error
- func (s *NestedSet) Parent(node NodeInterface) NodeInterface
- type Node
- func (n Node) Id() int64
- func (n Node) Left() int64
- func (n Node) Level() int64
- func (n Node) Name() string
- func (n Node) Right() int64
- func (n *Node) SetId(id int64)
- func (n *Node) SetLeft(left int64)
- func (n *Node) SetLevel(level int64)
- func (n *Node) SetName(name string)
- func (n *Node) SetRight(right int64)
- func (n Node) Type() string
- type NodeInterface
- type SortedNodes
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NestedSet ¶
type NestedSet struct {
// contains filtered or unexported fields
}
NestedSet represents a nested set management type.
func NewNestedSet ¶
func NewNestedSet() *NestedSet
NewNestedSet creates and returns a new instance of NestedSet with root node.
func (*NestedSet) Add ¶
func (s *NestedSet) Add(newNode, parent NodeInterface) error
Adds new node to nested set. If `parent` nil, add node to root node.
func (*NestedSet) Branch ¶
func (s *NestedSet) Branch(node NodeInterface) []NodeInterface
Returns branch for node, including itself.
func (*NestedSet) Delete ¶
func (s *NestedSet) Delete(node NodeInterface) error
Deletes node from nested set.
func (*NestedSet) FindById ¶
func (s *NestedSet) FindById(id int64) NodeInterface
Finds and returns node by id.
func (NestedSet) MarshalJSON ¶
Overrides json.Marshaller.MarshalJSON().
func (*NestedSet) Move ¶
func (s *NestedSet) Move(node, parent NodeInterface) error
Moves node and its branch to another parent node.
func (*NestedSet) Parent ¶
func (s *NestedSet) Parent(node NodeInterface) NodeInterface
Returns parent for node.
type Node ¶
type Node struct { NodeId int64 `json:"id"` NodeName string `json:"node_name"` NodeLevel int64 `json:"level"` NodeLeft int64 `json:"left"` NodeRight int64 `json:"right"` }
Node represents generic node type with NodeInterface implementation
type NodeInterface ¶
type NodeInterface interface { Type() string // Returns type of node Name() string // Returns name of node Id() int64 // Returns id of node Level() int64 // Returns level of node Left() int64 // Returns left of node Right() int64 // Returns right of node SetId(int64) // Sets node id SetName(string) // Sets node name SetLevel(int64) // Sets node level SetLeft(int64) // Sets node left SetRight(int64) // Sets node right }
NodeInterface is the interface implemented by types that can be used by nodes in nested set
type SortedNodes ¶
type SortedNodes []NodeInterface
SortedNodes represent nodes array sorted by left value.
func (SortedNodes) Len ¶
func (sn SortedNodes) Len() int
func (SortedNodes) Less ¶
func (sn SortedNodes) Less(i, j int) bool
func (SortedNodes) Swap ¶
func (sn SortedNodes) Swap(i, j int)