path

package
v0.0.0-...-a8d7af0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

Package path contains methods for dealing with key.Paths.

Index

Constants

This section is empty.

Variables

View Source
var Wildcard = key.New(WildcardType{})

Wildcard is a special element in a path that is used by Map and the Match* functions to match any other element.

Functions

func Append

func Append(path key.Path, elements ...interface{}) key.Path

Append appends a variable number of elements to a path. Each element may either be a key.Key or a value that can be wrapped by a key.Key. Note that calling Append on a single path returns that same path, whereas in all other cases a new path is returned.

func Base

func Base(path key.Path) key.Key

Base returns the last element of the path. If the path is empty, Base returns nil.

func Clone

func Clone(path key.Path) key.Path

Clone returns a new path with the same elements as in the provided path.

func Equal

func Equal(a, b key.Path) bool

Equal returns whether path a and path b are the same length and whether each element in b corresponds to the same element in a.

func FromString

func FromString(str string) key.Path

FromString constructs a path from the elements resulting from a split of the input string by "/". Strings that do not lead with a '/' are accepted but not reconstructable with key.Path.String. Both "" and "/" are treated as a key.Path{}.

func HasElement

func HasElement(a key.Path, b key.Key) bool

HasElement returns whether element b exists in path a.

func HasPrefix

func HasPrefix(a, b key.Path) bool

HasPrefix returns whether path b is a prefix of path a. It checks that b is at most the length of path a and whether each element in b corresponds to the same element in a from the first element.

func Join

func Join(paths ...key.Path) key.Path

Join joins a variable number of paths together. Each path in the joining is treated as a subpath of its predecessor. Calling Join with no or only empty paths returns nil.

func Match

func Match(a, b key.Path) bool

Match returns whether path a and path b are the same length and whether each element in b corresponds to the same element or a wildcard in a.

func MatchPrefix

func MatchPrefix(a, b key.Path) bool

MatchPrefix returns whether path b is a prefix of path a where path a may contain wildcards. It checks that b is at most the length of path a and whether each element in b corresponds to the same element or a wildcard in a from the first element.

func New

func New(elements ...interface{}) key.Path

New constructs a path from a variable number of elements. Each element may either be a key.Key or a value that can be wrapped by a key.Key.

func Parent

func Parent(path key.Path) key.Path

Parent returns all but the last element of the path. If the path is empty, Parent returns nil.

Types

type Map

type Map = MapOf[any]

Map associates paths to values. It allows wildcards. A Map is primarily used to register handlers with paths that can be easily looked up each time a path is updated.

type MapOf

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

MapOf associates paths to values of type T. It allows wildcards. A Map is primarily used to register handlers with paths that can be easily looked up each time a path is updated.

func (*MapOf[T]) Delete

func (m *MapOf[T]) Delete(p key.Path) bool

Delete unregisters the value registered with a path. It returns true if a value was deleted and false otherwise.

func (*MapOf[T]) Get

func (m *MapOf[T]) Get(p key.Path) (T, bool)

Get returns the value registered with an exact match of a path p. If there is no exact match for p, Get returns the zero value and false. If p has an exact match and it is set to true, Get returns its value and true.

func (*MapOf[T]) GetLongestPrefix

func (m *MapOf[T]) GetLongestPrefix(p key.Path) (key.Path, T, bool)

GetLongestPrefix determines the longest prefix of p for which an entry exists within the path map. If such a prefix exists, this function returns the prefix path, its associated value, and true. Otherwise, this functions returns the empty path, the zero value of T, and false.

func (*MapOf[T]) IsEmpty

func (m *MapOf[T]) IsEmpty() bool

IsEmpty returns true if no paths have been registered, false otherwise.

func (*MapOf[T]) Set

func (m *MapOf[T]) Set(p key.Path, v T) bool

Set registers a path p with a value. If the path was already registered with a value it returns false and true otherwise.

func (*MapOf[T]) String

func (m *MapOf[T]) String() string

func (*MapOf[T]) Visit

func (m *MapOf[T]) Visit(p key.Path, fn func(v T) error) error

Visit calls a function fn for every value in the Map that is registered with a match of a path p. In the general case, time complexity is linear with respect to the length of p but it can be as bad as O(2^len(p)) if there are a lot of paths with wildcards registered.

func (*MapOf[T]) VisitChildren

func (m *MapOf[T]) VisitChildren(p key.Path, fn func(v T) error) error

VisitChildren calls fn for every child for every node that matches the provided path.

func (*MapOf[T]) VisitPrefixed

func (m *MapOf[T]) VisitPrefixed(p key.Path, fn func(v T) error) error

VisitPrefixed calls fn for every value in the map that is registerd with a path that is prefixed by p. This method can be used to visit every registered path if p is the empty path (or root path) which prefixes all paths.

func (*MapOf[T]) VisitPrefixes

func (m *MapOf[T]) VisitPrefixes(p key.Path, fn func(v T) error) error

VisitPrefixes calls a function fn for every value in the Map that is registered with a prefix of a path p.

type VisitorFunc

type VisitorFunc func(v any) error

VisitorFunc is a function that handles the value associated with a path in a Map. Note that only the value is passed in as an argument since the path can be stored inside the value if needed.

type WildcardType

type WildcardType struct{}

WildcardType is the type used to construct a Wildcard. It implements the value.Value interface so it can be used as a key.Key.

func (WildcardType) Equal

func (w WildcardType) Equal(other interface{}) bool

Equal implements the key.Comparable interface.

func (WildcardType) MarshalJSON

func (w WildcardType) MarshalJSON() ([]byte, error)

MarshalJSON implements the value.Value interface.

func (WildcardType) String

func (w WildcardType) String() string

func (WildcardType) ToBuiltin

func (w WildcardType) ToBuiltin() interface{}

ToBuiltin implements the value.Value interface.

Jump to

Keyboard shortcuts

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