Documentation
¶
Index ¶
- func Clean(path string) string
- func CompileAndMatch(templatePattern string, path string, opts ...MatchOption) (matched bool, vars map[string]string, err error)
- func Join(segments ...string) string
- func Match(template *pathmatchpb.PathTemplate, path string, opts ...MatchOption) (matched bool, vars map[string]string, err error)
- func ParseTemplate(s string) (*pmpb.PathTemplate, error)
- func Split(path string) []string
- type MatchOption
- type Walker
- func (w *Walker) Depth() int
- func (w *Walker) IsComplete() bool
- func (w *Walker) Remaining() string
- func (w *Walker) Reset()
- func (w *Walker) Step(template *pathmatchpb.PathTemplate) (stepVars map[string]string, matched bool, err error)
- func (w *Walker) StepBack() bool
- func (w *Walker) Variables() map[string]string
- type WalkerBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clean ¶
Clean normalizes a path string by removing redundant slashes and any trailing slash (unless it's the root path "/"). For example, Clean("/users//alice///") returns "/users/alice". Clean("/") returns "/".
func CompileAndMatch ¶
func CompileAndMatch(templatePattern string, path string, opts ...MatchOption) (matched bool, vars map[string]string, err error)
CompileAndMatch parses the templatePattern string and then matches it against the given path. It's a convenience wrapper around ParseTemplate and Match.
func Join ¶
Join combines path segments into a single path string. It ensures segments are joined by a single slash and prepends a leading slash. If no segments are provided, it returns "/". Example: Join("users", "alice", "profile") returns "/users/alice/profile".
func Match ¶
func Match(template *pathmatchpb.PathTemplate, path string, opts ...MatchOption) (matched bool, vars map[string]string, err error)
Matches path to a parsed template path path cant contain wildcards or variables, only literal segments
/path/*/to matches /path/any/to /path/{var} matches /path/to and returns map[string]string{"var": "to"} /path/{var=**} matches /path/to/with/more and returns map[string]string{"var": "to/with/more"}
func ParseTemplate ¶
func ParseTemplate(s string) (*pmpb.PathTemplate, error)
ParseTemplate parses a path template string into a structured PathTemplate object.
The template string must start with a '/' and may contain:
- Literal segments (e.g., "/users")
- Wildcard segments: '*' matches any single path segment
- Double wildcard: '**' matches zero or more segments, but only as a full segment and only at the end
- Variables: '{name}' for a single segment, or '{name=pattern}' where pattern is a sequence of segments
Types ¶
type MatchOption ¶ added in v0.1.1
type MatchOption func(*match.MatchOptions)
func WithCaseInsensitive ¶ added in v0.1.1
func WithCaseInsensitive() MatchOption
WithCaseInsensitive sets the match options to be case-insensitive.
func WithKeepFirstVariable ¶ added in v0.1.3
func WithKeepFirstVariable() MatchOption
WithKeepFirstVariable sets the variable merging policy. If true, when a variable name is encountered more than once, the value from the first match is kept. If false (default), the last match overwrites previous values.
type Walker ¶
type Walker struct {
// contains filtered or unexported fields
}
Walker facilitates step-by-step traversal and matching of a concrete path against a series of path templates. It is designed for scenarios such as evaluating hierarchical configurations, where a specific path (e.g., "/users/alice/settings/profile") is incrementally matched against templates (e.g., "/users/{userID}", then "/settings/{section}").
The Walker maintains its current position within the concrete path, accumulates variables extracted from successful template matches, and tracks the depth of traversal. It supports stepping forward through matches, stepping back to previous states, and resetting to the initial state.
A Walker is typically initialized with a full concrete path using NewWalker. Subsequent calls to Step attempt to consume parts of this path according to the provided PathTemplates.
func NewWalker ¶
NewWalker creates and initializes a new Walker for the given concretePath. The walker starts at the beginning of the path with no variables captured and a depth of 0.
Example:
walker := NewWalker("/users/alice/settings/profile")
func (*Walker) Depth ¶
Depth returns the number of successful Step operations performed, effectively the current "level" of matching within the path. It starts at 0 and increments with each successful Step.
func (*Walker) IsComplete ¶
IsComplete checks if the entire concretePath has been consumed by Step operations. It is a convenience method equivalent to checking if Remaining() returns an empty string.
func (*Walker) Remaining ¶
Remaining returns the portion of the original concretePath that has not yet been consumed by successful Step operations.
Example:
walker := NewWalker("/a/b/c")
walker.Step(templateForA) // Assuming templateForA matches "/a"
fmt.Println(walker.Remaining()) // Output: "/b/c"
func (*Walker) Reset ¶
func (w *Walker) Reset()
Reset returns the Walker to its initial state, as if it were newly created with the original concretePath. All captured variables are cleared, the remaining path is reset to the full concrete path, and depth is set to 0. The history for StepBack is also cleared.
func (*Walker) Step ¶
func (w *Walker) Step(template *pathmatchpb.PathTemplate) (stepVars map[string]string, matched bool, err error)
Step attempts to match the provided PathTemplate against the current beginning of the Remaining path.
If the template matches:
- The walker's internal position advances past the matched segment(s).
- Variables captured by this specific template match are returned in stepVars.
- These stepVars are also merged into the walker's total Variables().
- The walker's Depth is incremented.
- matched is true.
If the template does not match:
- The walker's state remains unchanged.
- stepVars is nil.
- matched is false.
Example:
walker := NewWalker("/users/alice/settings/profile")
userTemplate, _ := pathmatch.ParseTemplate("/users/{id}")
vars, ok := walker.Step(userTemplate)
// vars: map[string]string{"id": "alice"}, ok: true
// walker.Remaining(): "/settings/profile"
// walker.Variables(): map[string]string{"id": "alice"}
// walker.Depth(): 1
func (*Walker) StepBack ¶
StepBack reverts the Walker to the state it was in before the last successful Step operation. This effectively "undoes" the last match.
If a StepBack is possible (i.e., Depth > 0):
- The walker's Remaining path, Variables, and Depth are restored.
- It returns true.
If no previous steps exist (Depth == 0), the state is unchanged, and it returns false.
Example:
walker.Step(template1) // depth becomes 1 walker.Step(template2) // depth becomes 2 ok := walker.StepBack() // ok: true, depth becomes 1 ok = walker.StepBack() // ok: true, depth becomes 0 ok = walker.StepBack() // ok: false, depth remains 0
func (*Walker) Variables ¶
Variables returns a map of all variables accumulated from all successful Step operations up to the current point. The keys are variable names from the path templates, and values are the matched segments from the concrete path. The returned map is a copy; modifications to it will not affect the walker's internal state.
type WalkerBuilder ¶ added in v0.1.1
type WalkerBuilder struct {
// contains filtered or unexported fields
}
WalkerBuilder is a helper struct for constructing a Walker instance. It allows setting options before building the Walker.
Example:
walkerBuilder := pathmatch.NewWalkerBuilder("/users/alice/settings/profile")
walker := walkerBuilder.WithCaseIncensitive().Build()
func NewWalkerBuilder ¶ added in v0.1.1
func NewWalkerBuilder(concretePath string) *WalkerBuilder
NewWalkerBuilder initializes a new WalkerBuilder with the given concrete path. The builder allows customization of match options before creating the Walker.
func (*WalkerBuilder) Build ¶ added in v0.1.1
func (b *WalkerBuilder) Build() (*Walker, error)
Build creates a new Walker instance using the concrete path and match options specified in the builder. It initializes the Walker to start at the beginning of the concrete path with no variables captured and a depth of 0.
func (*WalkerBuilder) WithCaseIncensitive ¶ added in v0.1.1
func (b *WalkerBuilder) WithCaseIncensitive() *WalkerBuilder
WithCaseIncensitive sets the match options to be case-insensitive. This modifies the Walker's behavior to ignore case when matching path segments.
func (*WalkerBuilder) WithKeepFirstVariable ¶ added in v0.1.3
func (b *WalkerBuilder) WithKeepFirstVariable() *WalkerBuilder
WithKeepFirstVariable sets the variable merging policy. If true, when a variable name is encountered more than once, the value from the first match is kept. If false (default), the last match overwrites previous values.