Documentation
¶
Overview ¶
SPDX-License-Identifier: MIT
SPDX-License-Identifier: MIT
SPDX-License-Identifier: MIT
SPDX-License-Identifier: MIT
SPDX-License-Identifier: MIT
Index ¶
- Variables
- type BuildOption
- type BuildSource
- type Builder
- type Config
- type Constraint
- type DefaultBuilder
- type Hierarchy
- func (h *Hierarchy[T]) AddChild(ctx context.Context, child *Hierarchy[T], parent ...T) (err error)
- func (h *Hierarchy[T]) AllChildren(ctx context.Context, parentValue ...T) (children List[T], err error)
- func (h *Hierarchy[T]) AllChildrenByLevel(ctx context.Context, parentValue ...T) (children LevelList[T], err error)
- func (h *Hierarchy[T]) Child(_ context.Context, childValue T) (child *Hierarchy[T], ok bool)
- func (h *Hierarchy[T]) Children(ctx context.Context) (children List[T])
- func (h *Hierarchy[T]) Config() *Config
- func (h *Hierarchy[T]) Leaves(ctx context.Context) (termNodes List[T], err error)
- func (h *Hierarchy[T]) Locate(ctx context.Context, childValue T, parentValue ...T) (child *Hierarchy[T], err error)
- func (h *Hierarchy[T]) Parent() *Hierarchy[T]
- func (h *Hierarchy[T]) PopChild(ctx context.Context, childValue T, parentValue ...T) (child *Hierarchy[T], err error)
- func (h *Hierarchy[T]) Serialize(ctx context.Context, cfg *lexer.Config) (output string, err error)
- func (h *Hierarchy[T]) SetParent(parent *Hierarchy[T])
- func (h *Hierarchy[T]) Value() T
- func (h *Hierarchy[T]) Walk(ctx context.Context, traverseChan chan TraverseComm[T])
- type LevelList
- type List
- type Option
- type TraverseComm
Constants ¶
This section is empty.
Variables ¶
var ( ErrBuildHierarchy = errors.New("failed to build hierarchy") ErrMissingRootNode = errors.New("missing root node") ErrMultipleRootNodes = errors.New("hierarchy has multiple root nodes") ErrEmptyHierarchySrc = errors.New("empty hierarchy source") ErrInvalidHierarchySrc = errors.New("invalid hierarchy source") ErrInconsistentBuildCache = errors.New("inconsistency between the hierarchy and build cache") ErrLocateParents = errors.New("unable to locate parents(s)") ErrPanicked = errors.New("recovery from panic") )
Hierarchy building errors.
var ( ErrExcessiveValues = errors.New("the deserialization source has excessive values") ErrExcessiveEndMarkers = fmt.Errorf("the deserialization source has excessive end markers") )
Deserialization errors.
var ( ErrNotFound = errors.New("not found") ErrNoLeaves = errors.New("lacks leaves; tree is cyclic") ErrAlreadyChild = errors.New("is a child of") ErrNoChildren = errors.New("lacks children ") ErrNotChild = errors.New("is not a child of") )
Errors encountered when handling a Hierarchy.
Functions ¶
This section is empty.
Types ¶
type BuildOption ¶
type BuildOption[T Constraint] func(*BuildSource[T])
BuildOption defines the BuildSource functional option type.
func IsOrdered ¶
func IsOrdered[T Constraint]() BuildOption[T]
IsOrdered configures the BuildSource as ordered; all parent [Builder.Value] are less than their respective child's [Builder.Value].
Unordered BuildSource's have a Hierarchy build-time performance penalty.
func WithBuildConfig ¶
func WithBuildConfig[T Constraint](cfg *Config) BuildOption[T]
WithBuildConfig configures the BuildSource's Config.
func WithBuilders ¶
func WithBuilders[T Constraint](builders []Builder[T]) BuildOption[T]
WithBuilders configures the underlying list.
type BuildSource ¶
type BuildSource[T Constraint] struct { // contains filtered or unexported fields }
BuildSource is a wrapper type for []Builder used to generate the Hierarchy.
func NewBuildSource ¶
func NewBuildSource[T Constraint](options ...BuildOption[T]) *BuildSource[T]
NewBuildSource instantiates a BuildSource.
func (*BuildSource[T]) Build ¶
func (b *BuildSource[T]) Build(ctx context.Context, options ...Option[T]) (h *Hierarchy[T], err error)
Build generates a Hierarchy from a BuildSource.
func (*BuildSource[T]) Cut ¶
func (b *BuildSource[T]) Cut(index int)
Cut a value at some index from the BuildSource.
func (*BuildSource[T]) Len ¶
func (b *BuildSource[T]) Len() int
Len retrieves the length of the BuildSource.
type Builder ¶
type Builder[T Constraint] interface { // Value obtains the value stored by the Builder. Value() T // Parent obtains the parent stored by the Builder Parent() T }
Builder defines an interface for entities that can be read into a Hierarchy.
type Config ¶
type Config struct { // Logger for [Hierarchy] messages. // // Preferring a public field to allow for sharing. Logger *slog.Logger Debug bool }
Config defines configuration options for the BuildSource & Hierarchy's operations.
type Constraint ¶
type Constraint interface { comparable cmp.Ordered }
Constraint is a wrapper interface constraining the Hierarchy's valid datatypes.
type DefaultBuilder ¶
type DefaultBuilder struct {
// contains filtered or unexported fields
}
DefaultBuilder is a sample Builder interface implementation.
func (*DefaultBuilder) Parent ¶
func (d *DefaultBuilder) Parent() string
Parent obtains the parent stored by the DefaultBuilder
func (*DefaultBuilder) Value ¶
func (d *DefaultBuilder) Value() string
Value obtains the value stored by the DefaultBuilder.
type Hierarchy ¶
type Hierarchy[T Constraint] struct { // contains filtered or unexported fields }
Hierarchy defines an n-array tree to hold hierarchies.
Synchronization is unnecessary, the type is designed for single write multiple read.
func Deserialize ¶
func Deserialize[T Constraint](ctx context.Context, l *lexer.Lexer, options ...Option[T]) (h *Hierarchy[T], err error)
Deserialize transforms a serialized tree into a Hierarchy.
An invalid entry will result in a truncated Hierarchy.
func New ¶
func New[T Constraint](value T, options ...Option[T]) *Hierarchy[T]
New instantiates a Hierarchy.
func (*Hierarchy[T]) AllChildren ¶
func (h *Hierarchy[T]) AllChildren(ctx context.Context, parentValue ...T) (children List[T], err error)
AllChildren lists immediate and children-of children for a Hierarchy.
func (*Hierarchy[T]) AllChildrenByLevel ¶
func (h *Hierarchy[T]) AllChildrenByLevel(ctx context.Context, parentValue ...T) (children LevelList[T], err error)
AllChildrenByLevel lists immediate and children-of children for a Hierarchy by level.
func (*Hierarchy[T]) Leaves ¶
Leaves returns an array of terminal Hierarchy(ies).
An error here indicates a cyclic Hierarchy.
func (*Hierarchy[T]) Locate ¶
func (h *Hierarchy[T]) Locate(ctx context.Context, childValue T, parentValue ...T) (child *Hierarchy[T], err error)
Locate searches for a value & returns it's Hierarchy.
func (*Hierarchy[T]) Parent ¶
Parent retrieves a reference to the Hierarchy's parent.
Value is nil for the root node.
func (*Hierarchy[T]) PopChild ¶
func (h *Hierarchy[T]) PopChild(ctx context.Context, childValue T, parentValue ...T) (child *Hierarchy[T], err error)
PopChild removes an immediate child of the Hierarchy, returning it's reference.
type LevelList ¶
type LevelList[T Constraint] []List[T]
type List ¶
type List[T Constraint] []*Hierarchy[T]
List is a type wrapper for []*Hierarchy.
func (*List[T]) Less ¶
Less reports whether the element with index i must sort before the element with index j.
type Option ¶
type Option[T Constraint] func(*Hierarchy[T])
Option defines the Hierarchy functional option type.
func UseLocateCache ¶
func UseLocateCache[T Constraint]() Option[T]
UseLocateCache enables the usage of a cache for hierarchy.Locate operations.
func WithConfig ¶
func WithConfig[T Constraint](cfg *Config) Option[T]
type TraverseComm ¶
type TraverseComm[T Constraint] struct { // contains filtered or unexported fields }
TraverseComm defines a channel to communicate info between Hierarchy operations & it's callers.