Documentation
¶
Overview ¶
Package keypath provides the KeyPath type and utilities for working with hierarchical configuration key paths.
A KeyPath is a []string representing a hierarchical key. For example, "server/http/port" becomes []string{"server", "http", "port"}.
Constructors ¶
- NewKeyPath — creates a KeyPath by splitting on "/".
- NewKeyPathWithDelim — creates a KeyPath with a custom delimiter.
- NewKeyPathFromSegments — creates a KeyPath from an existing slice.
Methods ¶
KeyPath provides methods for common path operations: KeyPath.String, KeyPath.MakeString, KeyPath.Parent, [KeyPath.Last], KeyPath.Append, [KeyPath.Equal], [KeyPath.HasPrefix], [KeyPath.TrimPrefix], [KeyPath.Len], and [KeyPath.IsEmpty].
Index ¶
- type KeyPath
- func (p KeyPath) Append(segments ...string) KeyPath
- func (p KeyPath) Equals(other KeyPath) bool
- func (p KeyPath) HasEmptySegment() bool
- func (p KeyPath) Leaf() string
- func (p KeyPath) MakeString(delim string) string
- func (p KeyPath) Match(pattern KeyPath) bool
- func (p KeyPath) Parent() KeyPath
- func (p KeyPath) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KeyPath ¶
type KeyPath []string
KeyPath represents a hierarchical key.
For example, for the key "/server/http/port" it will look like []string{"server", "http", "port"}.
func NewKeyPath ¶
NewKeyPath creates a KeyPath from a string, splitting it by "/" slash. This is the main way to create a path from a textual representation.
func NewKeyPathFromSegments ¶
NewKeyPathFromSegments creates a KeyPath from a slice of segments. This is useful when you already have segments as a slice and don't need string parsing.
func NewKeyPathWithDelim ¶
NewKeyPathWithDelim creates a KeyPath from a string, splitting it by the given delimiter. All segments are preserved, including empty ones.
func (KeyPath) Append ¶
Append adds new segment(s) to the path, returning a new KeyPath. The original path is not changed (immutability).
func (KeyPath) HasEmptySegment ¶
HasEmptySegment returns true if the path contains any empty segment ("").
func (KeyPath) Leaf ¶
Leaf returns the last segment of the path. If the path is empty, returns an empty string.
func (KeyPath) MakeString ¶
MakeString returns a textual representation of the path with the given delimiter.
func (KeyPath) Match ¶
Match checks whether the path matches a pattern using wildcards. A wildcard is "*", which matches any single segment. A double wildcard "**" matches zero or more segments. Pattern matches if it is a prefix of the path (pattern length <= path length). For example, pattern "a/*/c" matches path "a/b/c". Pattern "a/*/c" also matches path "a/b/c/d" (since pattern is prefix). Pattern "a/**/c" matches path "a/b/c", "a/x/y/c", and "a/c".