tree

package
v0.0.0-...-cc73b8b Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2019 License: MIT Imports: 2 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MetaData

type MetaData struct {
	Name  string `json:"name,omitempty"`
	Title string `json:"title,omitempty"`
}

MetaData is the business-relevant information for a Node

type Node

type Node struct {
	ID       string   `json:"id"`
	Children []*Node  `json:"children,omitempty"`
	Height   int      `json:"height,omitempty"`
	Data     MetaData `json:"metadata,omitempty"`
	ParentID string   `json:"parentId,omitempty"`
	RootID   *string  `json:"rootId,omitempty"`
}

Node is the entity that is the building block of a tree it may or may not contain a `parentId`, that points to another node ID

func (*Node) GetDescendants

func (node *Node) GetDescendants() SubordinatesResponse

GetDescendants returns a count and a hierarchy of the subtree (excluding the current node)

type SubordinatesCount

type SubordinatesCount struct {
	Direct int `json:"direct"`
	Total  int `json:"total"`
}

SubordinatesCount contains a `direct` and a `total` count of subordinates

type SubordinatesInfo

type SubordinatesInfo struct {
	Count     SubordinatesCount `json:"count"`
	Hierarchy []*Node           `json:"hierarchy,omitempty"`
}

SubordinatesInfo contains a `count` of subordinates and a full `hierarchy` of nodes

type SubordinatesResponse

type SubordinatesResponse struct {
	Subordinates SubordinatesInfo `json:"subordinates"`
}

SubordinatesResponse is the base response for the GET subordinates route

type Tree

type Tree struct {
	Id   string `json:"id"`
	Root *Node  `json:"root"`
}

Tree is the basic struct for any tree hierarchy

func FromJSON

func FromJSON(value string) (*Tree, error)

FromJSON receives JSON data for a tree and returns the unmarshalled struct of a Tree

func New

func New(id string) *Tree

New will create a new tree given an id by default will also create a node with id `root`

func (Tree) AttachNode

func (tree Tree) AttachNode(newNode *Node, parent *Node) error

AttachNode will receive two node pointers in a tree, and will attach the first one as a subordinate of the foremost this will update the entire subtree of the node being attached

func (Tree) DetachNode

func (tree Tree) DetachNode(node *Node) (*Node, error)

DetachNode will remove a `node` from its parent and will return a pointer to the detached node

func (Tree) FindNode

func (tree Tree) FindNode(id string, start *Node) (*Node, error)

FindNode will find a node with `id` in the tree in O(n) operations by default, `start` node will be the Root node it is possible to define the `start` node to speed up operations

func (Tree) GetRoot

func (tree Tree) GetRoot() (*Node, error)

GetRoot will retrieve the Root Node of a tree

func (Tree) MoveNode

func (tree Tree) MoveNode(node *Node, newParent *Node) error

MoveNode will detach a the subtree of a given `node` and then attach it as a descendant of `newParent`

func (Tree) ToJSON

func (tree Tree) ToJSON() (string, error)

ToJSON returna a JSON representation of a given Tree

Jump to

Keyboard shortcuts

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