Documentation ¶
Overview ¶
Package path contains methods for dealing with key.Paths.
Index ¶
- Variables
- func Append(path key.Path, elements ...interface{}) key.Path
- func Base(path key.Path) key.Key
- func Clone(path key.Path) key.Path
- func Equal(a, b key.Path) bool
- func FromString(str string) key.Path
- func HasElement(a key.Path, b key.Key) bool
- func HasPrefix(a, b key.Path) bool
- func Join(paths ...key.Path) key.Path
- func Match(a, b key.Path) bool
- func MatchPrefix(a, b key.Path) bool
- func New(elements ...interface{}) key.Path
- func Parent(path key.Path) key.Path
- type Map
- func (m *Map) Delete(p key.Path) bool
- func (m *Map) Get(p key.Path) (interface{}, bool)
- func (m *Map) IsEmpty() bool
- func (m *Map) Set(p key.Path, v interface{}) bool
- func (m *Map) String() string
- func (m *Map) Visit(p key.Path, fn VisitorFunc) error
- func (m *Map) VisitPrefixed(p key.Path, fn VisitorFunc) error
- func (m *Map) VisitPrefixes(p key.Path, fn VisitorFunc) error
- type VisitorFunc
- type WildcardType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 Equal ¶
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 ¶
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 ¶
HasElement returns whether element b exists in path a.
func HasPrefix ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
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.
func (*Map) Delete ¶
Delete unregisters the value registered with a path. It returns true if a value was deleted and false otherwise.
Example ¶
package main import ( "fmt" "github.com/aristanetworks/goarista/path" ) func main() { var m path.Map p := path.New("foo", "bar") m.Set(p, 0) fmt.Println(m.Delete(p)) fmt.Println(m.Delete(p)) }
Output: true false
func (*Map) Get ¶
Get returns the value registered with an exact match of a path p. If there is no exact match for p, Get returns nil and false. If p has an exact match and it is set to true, Get returns nil and true.
Example ¶
package main import ( "fmt" "github.com/aristanetworks/goarista/path" ) func main() { var m path.Map m.Set(path.New("foo", "bar"), 1) m.Set(path.New("baz", "qux"), nil) a, ok := m.Get(path.New("foo", "bar")) fmt.Printf("a = %v, ok = %t\n", a, ok) b, ok := m.Get(path.New("foo", path.Wildcard)) fmt.Printf("b = %v, ok = %t\n", b, ok) c, ok := m.Get(path.New("baz", "qux")) fmt.Printf("c = %v, ok = %t\n", c, ok) }
Output: a = 1, ok = true b = <nil>, ok = false c = <nil>, ok = true
func (*Map) Set ¶
Set registers a path p with a value. If the path was already registered with a value it returns false and true otherwise.
Example ¶
package main import ( "fmt" "github.com/aristanetworks/goarista/path" ) func main() { var m path.Map p := path.New("foo", "bar") fmt.Println(m.Set(p, 0)) fmt.Println(m.Set(p, 1)) a, ok := m.Get(p) fmt.Printf("a = %v, ok = %t\n", a, ok) }
Output: true false a = 1, ok = true
func (*Map) Visit ¶
func (m *Map) Visit(p key.Path, fn VisitorFunc) 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.
Example ¶
package main import ( "fmt" "sort" "github.com/aristanetworks/goarista/path" ) func main() { var m path.Map m.Set(path.New("foo", "bar", "baz"), 1) m.Set(path.New("foo", path.Wildcard, "baz"), 2) m.Set(path.New(path.Wildcard, "bar", "baz"), 3) m.Set(path.New("foo", "bar", path.Wildcard), 4) m.Set(path.New(path.Wildcard, path.Wildcard, "baz"), 5) m.Set(path.New(path.Wildcard, "bar", path.Wildcard), 6) m.Set(path.New("foo", path.Wildcard, path.Wildcard), 7) m.Set(path.New(path.Wildcard, path.Wildcard, path.Wildcard), 8) p := path.New("foo", "bar", "baz") var nums []int m.Visit(p, func(v interface{}) error { nums = append(nums, v.(int)) return nil }) sort.Ints(nums) fmt.Println(nums) }
Output: [1 2 3 4 5 6 7 8]
func (*Map) VisitPrefixed ¶
func (m *Map) VisitPrefixed(p key.Path, fn VisitorFunc) 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.
Example ¶
package main import ( "fmt" "sort" "github.com/aristanetworks/goarista/path" ) func main() { var m path.Map m.Set(path.New("foo"), 1) m.Set(path.New("foo", "bar"), 2) m.Set(path.New("foo", "bar", "baz"), 3) m.Set(path.New("foo", path.Wildcard), 4) p := path.New("foo", "bar") var nums []int m.VisitPrefixed(p, func(v interface{}) error { nums = append(nums, v.(int)) return nil }) sort.Ints(nums) fmt.Println(nums) // Ouput: [2 3 4] }
Output:
func (*Map) VisitPrefixes ¶
func (m *Map) VisitPrefixes(p key.Path, fn VisitorFunc) error
VisitPrefixes calls a function fn for every value in the Map that is registered with a prefix of a path p.
Example ¶
package main import ( "fmt" "sort" "github.com/aristanetworks/goarista/path" ) func main() { var m path.Map m.Set(path.New(), 1) m.Set(path.New("foo"), 2) m.Set(path.New("foo", "bar"), 3) m.Set(path.New("foo", "baz"), 4) m.Set(path.New(path.Wildcard, "bar"), 5) p := path.New("foo", "bar", "baz") var nums []int m.VisitPrefixes(p, func(v interface{}) error { nums = append(nums, v.(int)) return nil }) sort.Ints(nums) fmt.Println(nums) }
Output: [1 2 3 5]
type VisitorFunc ¶
type VisitorFunc func(v interface{}) 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.