gonode

package module
v0.0.0-...-8075ef7 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: MIT Imports: 3 Imported by: 0

README

gonode

Node-like/Tree-like data structures for Go

Nodebase?

Nodes in gonode are all pointers *Node, thus you don't have to live in fear of making massive copies of many depths of Nodes.

Nodes in gonode are designed to cover most applications/uses with a Data() and SetData() using go 1.19's any feature.

Nodes can be tagged for easier view of your data and easier accessing/retrieving a particular Node.

Nodes have children Nodes, and a parent Node (except for the "root" Node, which has a parent Node of nil).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayDelete

func ArrayDelete[T any](stack *[]T, pos int) (T, bool)

Delete an item from an array, returning item and true on success.

func ArrayPop

func ArrayPop[T any](stack *[]T, count int) bool

Pop items from head of array, return true on success.

Types

type Node

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

Recursive Node structure

func NewNode

func NewNode() *Node

Constructs an empty Node

func NewNodeWithData

func NewNodeWithData(d any) *Node

Constructs a Node with data

func NewNodeWithDataAndName

func NewNodeWithDataAndName(d any, name string) *Node

Alias for NewNodeWithNameAndData

Constructs a Node with a name and data

func NewNodeWithName

func NewNodeWithName(name string) *Node

Constructs a Node with a name

func NewNodeWithNameAndData

func NewNodeWithNameAndData(name string, d any) *Node

Constructs a Node with a name and data

func (*Node) AddChild

func (n *Node) AddChild(k *Node) bool

Adds a given node, only if the child isn't already a child

func (*Node) Child

func (n *Node) Child(idx int) *Node

Obtains a given child by it's index

func (*Node) ChildByName

func (n *Node) ChildByName(name string, recurse ...bool) *Node

Finds the child by name, if recurse then called recursively too

func (*Node) Children

func (n *Node) Children() []*Node

Obtain a list of children "child" nodes below this node

func (*Node) DToMap

func (n *Node) DToMap() map[string]any

This forms a map to represent the node (it recursively is called on children)

This is the Debug version, it explicitly indicates Data, Name, Children (You most-likely want ToMap)

func (*Node) Data

func (n *Node) Data(data ...any) any

Obtain or Assign the data associated with this node

func (*Node) Depth

func (n *Node) Depth() int

Obtain the number of nodes above this one till reaching the "root" node

func (*Node) Destroy

func (n *Node) Destroy()

Erases this node and ALL nodes below it

func (*Node) Detach

func (n *Node) Detach() bool

Detaches this node from the parent

func (*Node) Index

func (n *Node) Index() int

Obtains the index number for this node from the parent

Returns -1 to indicate error (either there is no parent or the parent doesn't have this node as a child)

func (*Node) IsMapLike

func (n *Node) IsMapLike() bool

Checks if the children of this node are better represented as a map or an array

This is determined by:

* Totaling number of children without names

* Comparing the percent of children without names to total children (if less than 50% use a map, if more than 50% use an array)

func (*Node) Len

func (n *Node) Len() int

The number of children "child" nodes below this node

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

Marshals the results from ToMap

func (*Node) Name

func (n *Node) Name(name ...string) string

Obtain or Assign the name associated with this node

Used for parsing JSON (this is the field name)

func (*Node) NewChild

func (n *Node) NewChild() *Node

Creates a new child node below this node

func (*Node) NewChildWithData

func (n *Node) NewChildWithData(d any) *Node

Creates a new child node with data below this node

func (*Node) NewChildWithDataAndName

func (n *Node) NewChildWithDataAndName(d any, name string) *Node

Alias function of NewChildWithNameAndData

Creates a new child node with a name and data below with this node

func (*Node) NewChildWithName

func (n *Node) NewChildWithName(name string) *Node

Creates a new child node with a name below this node

func (*Node) NewChildWithNameAndData

func (n *Node) NewChildWithNameAndData(name string, d any) *Node

Creates a new child node with a name and data below this node

func (*Node) Parent

func (n *Node) Parent() *Node

Obtain the parent of this node

Returning nil for the "root" node (as there is no parent)

func (*Node) RmAllChildren

func (n *Node) RmAllChildren()

Removes all children from this node

func (*Node) RmChild

func (n *Node) RmChild(idx int) bool

Removes the child from this node by index

func (*Node) Root

func (n *Node) Root() *Node

Obtain the top-most "root" node

It may include this node if this node is in fact "root"

func (*Node) ToMap

func (n *Node) ToMap() any

Forms a minimal map to represent this node (and is recursively called on children)

Unlike DToMap, this version doesn't explicitly state the fields

func (*Node) UnmarshalJSON

func (n *Node) UnmarshalJSON(pay []byte) error

Unmarshal function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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