tree

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aggregate

func Aggregate[T Named, V any](t *MapTree[T], values map[string]V, zero V, fn func(a, b V) V)

Aggregate aggregates values over the tree

func FormatCmdTree

func FormatCmdTree(cmdTree *CmdTree, indent int) (string, error)

FormatCmdTree creates a tree-like representation of a command and its sub-commands

Types

type CmdNode

type CmdNode = MapNode[CmdWrapper]

CmdNode is a tree of cobra commands

type CmdTree

type CmdTree = MapTree[CmdWrapper]

CmdTree is a tree of cobra commands

func NewCmdTree

func NewCmdTree(command *cobra.Command) (*CmdTree, error)

NewCmdTree creates a new project tree

type CmdWrapper

type CmdWrapper struct {
	*cobra.Command
}

CmdWrapper wraps *cobra.Command to implement the Named interface

func (CmdWrapper) GetName

func (cmd CmdWrapper) GetName() string

GetName implements the Named interface required for the MapTree

type MapNode

type MapNode[T Named] struct {
	Parent   *MapNode[T]
	Children orderedMap[*MapNode[T]]
	Value    T
	Depth    int
}

MapNode is a node in the tree data structure

func NewNode

func NewNode[T Named](value T, depth int) *MapNode[T]

NewNode creates a new tree node

type MapTree

type MapTree[T Named] struct {
	Root  *MapNode[T]
	Nodes orderedMap[*MapNode[T]]
}

MapTree is a tree data structure

func NewTree

func NewTree[T Named](value T) *MapTree[T]

NewTree creates a new tree node

func (*MapTree[T]) Add

func (t *MapTree[T]) Add(parent *MapNode[T], child T) (*MapNode[T], error)

AddTree adds a sub-tree without children

func (*MapTree[T]) AddNode

func (t *MapTree[T]) AddNode(parent *MapNode[T], child *MapNode[T]) error

AddNode adds a sub-tree

func (*MapTree[T]) Ancestors

func (t *MapTree[T]) Ancestors(name string) ([]*MapNode[T], bool)

Ancestors returns a slice of all ancestors (i.e. recursive parents), and an ok bool whether the requested node was found.

The first ancestor is the direct parent, while the last ancestor is the root node.

func (*MapTree[T]) Descendants

func (t *MapTree[T]) Descendants(name string) ([]*MapNode[T], bool)

Descendants returns a slice of all descendants (i.e. recursive children), and an ok bool whether the requested node was found.

Descendants in the returned slice have undefined order.

type Named

type Named interface {
	GetName() string
}

Named is an interface for stuff that has a name

type TreeFormatter

type TreeFormatter[T Named] struct {
	NameFunc func(t *MapNode[T], indent int) string
	Indent   int
	// contains filtered or unexported fields
}

TreeFormatter formats trees

func NewTreeFormatter

func NewTreeFormatter[T Named](
	nameFunc func(t *MapNode[T], indent int) string,
	indent int,
) TreeFormatter[T]

NewTreeFormatter creates a new TreeFormatter

func (*TreeFormatter[T]) FormatTree

func (f *TreeFormatter[T]) FormatTree(t *MapTree[T]) string

FormatTree formats a tree

Jump to

Keyboard shortcuts

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