aboutsummaryrefslogtreecommitdiff
path: root/pathrouter/variable.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-06-27 22:34:36 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-06-27 22:34:36 +0200
commit8e9889d3e5b2874854832d1d882d124132546f49 (patch)
tree80ab6bf53dac3090630ad58d7ebfbbf2c97019ca /pathrouter/variable.go
parent8804729561d8e5edd7179f494d89c620459d319f (diff)
downloadgo-gg-8e9889d3e5b2874854832d1d882d124132546f49.tar.gz
add package pathrouter
Diffstat (limited to 'pathrouter/variable.go')
-rw-r--r--pathrouter/variable.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/pathrouter/variable.go b/pathrouter/variable.go
new file mode 100644
index 0000000..e712f46
--- /dev/null
+++ b/pathrouter/variable.go
@@ -0,0 +1,31 @@
+// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
+// SPDX-License-Identifier: Apache-2.0
+
+package pathrouter
+
+import "go.xyrillian.de/gg/options"
+
+// Variable is a [Matcher] that accepts subpaths with at least one path element.
+// The value of the first path element will be collected into vars[name],
+// and the remaining subpath will have to be accepted by the next matcher.
+func Variable(name string, matcher Matcher) Matcher {
+ return variable(name, matcher.downcast())
+}
+
+func variable(name string, matcher realMatcher) Matcher {
+ 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] == "" {
+ return nil
+ }
+ handlerFunc := matcher.accept(path[1:], vars)
+ if handlerFunc == nil {
+ return nil
+ }
+ vars[name] = pathUnescape(path[0])
+ return handlerFunc
+ },
+ }
+}