nestedset

package module
v0.0.0-...-3049a82 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2021 License: MIT Imports: 4 Imported by: 0

README

nestedset

Package for manage nested sets in golang projects.

Install
go get github.com/juggleru/nestedset
Usage

To manage in nested set your data types add to your type *Node and init it.

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())
    }
}
Documentation

https://godoc.org/github.com/juggleru/nestedset

TODO

Add implementation moving node up/down in same branch.

Support

Ether: 0x33c9d1A034DA2a19CAD113cBd8ebE4Ba0a835e39

Buy Me A Coffee

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

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

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

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

func NewNode

func NewNode() *Node

NewNode returns a new Node instance

func (Node) Id

func (n Node) Id() int64

func (Node) Left

func (n Node) Left() int64

func (Node) Level

func (n Node) Level() int64

func (Node) Name

func (n Node) Name() string

func (Node) Right

func (n Node) Right() int64

func (*Node) SetId

func (n *Node) SetId(id int64)

func (*Node) SetLeft

func (n *Node) SetLeft(left int64)

func (*Node) SetLevel

func (n *Node) SetLevel(level int64)

func (*Node) SetName

func (n *Node) SetName(name string)

func (*Node) SetRight

func (n *Node) SetRight(right int64)

func (Node) Type

func (n Node) Type() string

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

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)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL