Documentation
¶
Overview ¶
Package pathtree implements a hierarchical key-value tree where keys are slash-separated paths. It supports adding, looking up, and printing nodes with optional labels.
Index ¶
- type Node
- type PrintOption
- type Tree
- func (t *Tree) Add(key, value, label string) error
- func (t *Tree) Contains(key string) bool
- func (t *Tree) Delete(key string) (string, error)
- func (t *Tree) Flatten() []*Node
- func (t *Tree) Fprint(w io.Writer)
- func (t *Tree) Get(key string) (string, error)
- func (t *Tree) SetPrefix(prefix string)
- func (t *Tree) String() string
- func (t *Tree) Update(key, value, label string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PrintOption ¶
type PrintOption int
PrintOption determines what information about each Node is printed. The values of Key, Value, and Label were chosen to be relatively prime to make determining what combination of identifiers to print purely mathematical Actual output may vary depending on available identifiers for each Node.
const ( Key PrintOption = 2 Value PrintOption = 3 Label PrintOption = 5 KeyValue PrintOption = 6 KeyLabel PrintOption = 10 ValueLabel PrintOption = 15 KeyValueLabel PrintOption = 30 )
func (PrintOption) String ¶
func (po PrintOption) String() string
type Tree ¶
type Tree struct {
PrintOption PrintOption
// contains filtered or unexported fields
}
func New ¶
func New() *Tree
New returns a pointer to an initialized Tree
Example ¶
ExampleNew demonstrates creating a new Tree.
package main
import (
"fmt"
"chainguard.dev/sdk/pathtree"
)
func main() {
t := pathtree.New()
fmt.Println(t != nil)
}
Output: true
func (*Tree) Add ¶
Add adds the given Value to the tree at the given Key path. If interior nodes between the root and leaf do not exist, empty nodes without a Value are created along the path. Add cannot be used to overwrite a Value that already exists at the given Key path.
Example ¶
ExampleTree_Add demonstrates adding a value to the tree.
package main
import (
"fmt"
"chainguard.dev/sdk/pathtree"
)
func main() {
t := pathtree.New()
err := t.Add("/foo/bar", "my-value", "my-label")
fmt.Println(err)
}
Output: <nil>
func (*Tree) Contains ¶
Contains returns true if Key is found in the tree, even if the Value is unset.
func (*Tree) Delete ¶
Delete deletes the Node identified by the given Key. Currently, only deleting leaves is supported.
func (*Tree) Get ¶
Get returns the Value in the Tree stored at the given Key path. If no Value was stored but the Key exists, return the local portion of the Key path.
Example ¶
ExampleTree_Get demonstrates looking up a value in the tree.
package main
import (
"fmt"
"chainguard.dev/sdk/pathtree"
)
func main() {
t := pathtree.New()
_ = t.Add("/foo/bar", "my-value", "my-label")
val, err := t.Get("/foo/bar")
fmt.Println(err)
fmt.Println(val)
}
Output: <nil> my-value