nestedset: github.com/juggleru/nestedset Index | Files

package nestedset

import "github.com/juggleru/nestedset"

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

Package Files

node.go ns.go

type NestedSet Uses

type NestedSet struct {
    // contains filtered or unexported fields
}

NestedSet represents a nested set management type.

func NewNestedSet Uses

func NewNestedSet() *NestedSet

NewNestedSet creates and returns a new instance of NestedSet with root node.

func (*NestedSet) Add Uses

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 Uses

func (s *NestedSet) Branch(node NodeInterface) []NodeInterface

Returns branch for node, including itself.

func (*NestedSet) Delete Uses

func (s *NestedSet) Delete(node NodeInterface) error

Deletes node from nested set.

func (*NestedSet) FindById Uses

func (s *NestedSet) FindById(id int64) NodeInterface

Finds and returns node by id.

func (NestedSet) MarshalJSON Uses

func (s NestedSet) MarshalJSON() ([]byte, error)

Overrides json.Marshaller.MarshalJSON().

func (*NestedSet) Move Uses

func (s *NestedSet) Move(node, parent NodeInterface) error

Moves node and its branch to another parent node.

func (*NestedSet) Parent Uses

func (s *NestedSet) Parent(node NodeInterface) NodeInterface

Returns parent for node.

type Node Uses

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

func NewNode Uses

func NewNode() *Node

NewNode returns a new Node instance

func (Node) Id Uses

func (n Node) Id() int64

func (Node) Left Uses

func (n Node) Left() int64

func (Node) Level Uses

func (n Node) Level() int64

func (Node) Name Uses

func (n Node) Name() string

func (Node) Right Uses

func (n Node) Right() int64

func (*Node) SetId Uses

func (n *Node) SetId(id int64)

func (*Node) SetLeft Uses

func (n *Node) SetLeft(left int64)

func (*Node) SetLevel Uses

func (n *Node) SetLevel(level int64)

func (*Node) SetName Uses

func (n *Node) SetName(name string)

func (*Node) SetRight Uses

func (n *Node) SetRight(right int64)

func (Node) Type Uses

func (n Node) Type() string

Type implements NodeInterface.Type() and returns "generic" type

type NodeInterface Uses

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 Uses

type SortedNodes []NodeInterface

SortedNodes represent nodes array sorted by left value.

func (SortedNodes) Len Uses

func (sn SortedNodes) Len() int

func (SortedNodes) Less Uses

func (sn SortedNodes) Less(i, j int) bool

func (SortedNodes) Swap Uses

func (sn SortedNodes) Swap(i, j int)

Package nestedset imports 4 packages (graph). Updated 2018-10-08. Refresh now. Tools for package owners.