pathtree

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package pathree provides Node[T] and generic functions used for storing arbitrary values in a hierarchical data structure following filesystem naming conventions.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPath = errors.New("invalid path")
	ErrNotFound    = errors.New("node not found")
	ErrNotDir      = errors.New("not a directory node")
	ErrRelation    = errors.New("cannot add descendant node that is already a descendant or ancestor")
)
View Source
var ErrSkipDir error

Functions

func Walk

func Walk[T any](node *Node[T], fn WalkFunc[T]) error

Walk runs fn on every node in the tree

Types

type DirEntries

type DirEntries []DirEntry

func (DirEntries) Len

func (a DirEntries) Len() int

func (DirEntries) Less

func (a DirEntries) Less(i, j int) bool

func (DirEntries) Swap

func (a DirEntries) Swap(i, j int)

type DirEntry

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

func (DirEntry) IsDir

func (de DirEntry) IsDir() bool

func (DirEntry) Name

func (de DirEntry) Name() string

type Node

type Node[T any] struct {
	Val T
	// contains filtered or unexported fields
}

Node is the primary type for pathtree: it includes a value, Val, of generic type T and map of named references to direct descendants, Children. Descendant nodes are mapped by their name, which cannot include '/'. If Children is nil, the node is considered a directory node. Otherwise, it is considered a file node.

func Map

func Map[T1 any, T2 any](root *Node[T1], fn func(T1) (T2, error)) (*Node[T2], error)

Map maps values of type T1 in root to values of type T2 in a new root node.

func NewDir

func NewDir[T any]() *Node[T]

NewDir returns a new directory node (IsDir() returns true) with no value and an empty list of children.

func NewFile

func NewFile[T any](val T) *Node[T]

NewFile returns a new file node with the value val. The returned node has no children and IsDir returns false.

func (*Node[T]) AllPaths

func (node *Node[T]) AllPaths() []string

AllPaths returns slice of all path names that are descendants of node

func (Node[T]) Child

func (node Node[T]) Child(name string) *Node[T]

Child returns the node's direct child with the given name. If the the node has no child with the given name, or if the node is not a directory node, nil is returned.

func (*Node[T]) Copy

func (node *Node[T]) Copy() *Node[T]

func (Node[T]) DirEntries

func (node Node[T]) DirEntries() []DirEntry

DirEntries returns a sorted DirEntry Sslice representing the children in node. If node is not a directory, nil is returned.

func (*Node[T]) Get

func (node *Node[T]) Get(p string) (*Node[T], error)

Get returns the node corresponding to the path p, relative to node n. The path p must be fs.ValidPath. If p is ".", node is returned.

func (Node[T]) IsDir

func (node Node[T]) IsDir() bool

IsDir indicates whether the node is directory node.

func (*Node[T]) IsParentOf

func (parent *Node[T]) IsParentOf(child *Node[T]) bool

IsParentOf returns true if child is a descendant of parent.

func (*Node[T]) Len

func (node *Node[T]) Len() int

Let returns the number of file nodes under node.

func (*Node[T]) MkdirAll

func (node *Node[T]) MkdirAll(p string) (*Node[T], error)

MkdirALL creates a directory node named p, along with any necessary parents.

func (*Node[T]) Remove

func (node *Node[T]) Remove(p string) (*Node[T], error)

Remove removes the node for path p and returns it. If p is ".", the node value is set to the zero value (of T) and children (if node is a directory) are cleared; a copy of the node's former value is returned. An error is returned if p is not a ValidPath, or if no node exists for p.

func (*Node[T]) RemoveEmptyDirs

func (node *Node[T]) RemoveEmptyDirs()

Remove any directories that do not have file nodes as descendants.

func (*Node[T]) Rename

func (node *Node[T]) Rename(from, to string) error

Rename moves the node with path from to path to. If replace is true and the path to exists, it is overwritten.

func (*Node[T]) Set

func (node *Node[T]) Set(p string, child *Node[T]) error

Set sets child as the Node for path p under node, creating any directory nodes if necessary. If p is ".", node's value is set to the same as child. An errir is returned if child is a descendant or ancestor of node.

func (*Node[T]) SetFile

func (node *Node[T]) SetFile(p string, val T) error

type WalkFunc

type WalkFunc[T any] func(name string, node *Node[T]) error

Jump to

Keyboard shortcuts

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