diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-06-27 22:34:36 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-06-27 22:34:36 +0200 |
| commit | 8e9889d3e5b2874854832d1d882d124132546f49 (patch) | |
| tree | 80ab6bf53dac3090630ad58d7ebfbbf2c97019ca /pathrouter/element.go | |
| parent | 8804729561d8e5edd7179f494d89c620459d319f (diff) | |
| download | go-gg-8e9889d3e5b2874854832d1d882d124132546f49.tar.gz | |
add package pathrouter
Diffstat (limited to 'pathrouter/element.go')
| -rw-r--r-- | pathrouter/element.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/pathrouter/element.go b/pathrouter/element.go new file mode 100644 index 0000000..9512924 --- /dev/null +++ b/pathrouter/element.go @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> +// SPDX-License-Identifier: Apache-2.0 + +package pathrouter + +import "go.xyrillian.de/gg/options" + +// Element is a [Matcher] that accepts subpaths with at least one path element. +// The first path element must be equal to the given value, +// and the remaining subpath will have to be accepted by the next matcher. +func Element(value string, matcher Matcher) Matcher { + return element(value, matcher.downcast()) +} + +func element(value string, matcher realMatcher) Matcher { + switch value { + case "": + panic(`Element() called with value = ""`) + case "/": + value = "" // trailing slash will lead to an empty element in `path`, e.g. strings.Split("foo/bar/") == []string{"foo","bar",""} + default: + } + + return realMatcher{ + minLength: matcher.minLength + 1, + maxLength: options.Map(matcher.maxLength, increment), + accept: func(path []string, vars map[string]string) HandlerFunc { + if len(path) == 0 || path[0] != value { + return nil + } + return matcher.accept(path[1:], vars) + }, + } +} |
