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 ¶
- Variables
- func Walk[T any](node *Node[T], fn WalkFunc[T]) error
- type DirEntries
- type DirEntry
- type Node
- func (node *Node[T]) AllPaths() []string
- func (node Node[T]) Child(name string) *Node[T]
- func (node *Node[T]) Copy() *Node[T]
- func (node Node[T]) DirEntries() []DirEntry
- func (node *Node[T]) Get(p string) (*Node[T], error)
- func (node Node[T]) IsDir() bool
- func (parent *Node[T]) IsParentOf(child *Node[T]) bool
- func (node *Node[T]) Len() int
- func (node *Node[T]) MkdirAll(p string) (*Node[T], error)
- func (node *Node[T]) Remove(p string) (*Node[T], error)
- func (node *Node[T]) RemoveEmptyDirs()
- func (node *Node[T]) Rename(from, to string) error
- func (node *Node[T]) Set(p string, child *Node[T]) error
- func (node *Node[T]) SetFile(p string, val T) error
- type WalkFunc
Constants ¶
This section is empty.
Variables ¶
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") )
var ErrSkipDir error
Functions ¶
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 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 NewDir ¶
NewDir returns a new directory node (IsDir() returns true) with no value and an empty list of children.
func NewFile ¶
NewFile returns a new file node with the value val. The returned node has no children and IsDir returns false.
func (Node[T]) Child ¶
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]) DirEntries ¶
DirEntries returns a sorted DirEntry Sslice representing the children in node. If node is not a directory, nil is returned.
func (*Node[T]) Get ¶
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]) IsParentOf ¶
IsParentOf returns true if child is a descendant of parent.
func (*Node[T]) MkdirAll ¶
MkdirALL creates a directory node named p, along with any necessary parents.
func (*Node[T]) Remove ¶
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 ¶
Rename moves the node with path from to path to. If replace is true and the path to exists, it is overwritten.