aboutsummaryrefslogtreecommitdiff
path: root/pathrouter/variable.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-10-02 15:02:43 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-10-02 15:02:43 +0200
commit1635adc08aa413bbd7d2cf67e66ad6e9a823169a (patch)
treee85a855893ce3632d3d43e58891208653fb70cb8 /pathrouter/variable.go
parent7b1cca50c21a6156824e276e2b6c662d33cc9071 (diff)
downloadgo-gg-1635adc08aa413bbd7d2cf67e66ad6e9a823169a.tar.gz
pathrouter: replace `vars` by new type Context
This is a breaking change, but there are no real users of this package yet. I want to use the opportunity to have an extensible type here, esp. to later add a cheaper alternative to context.Context.WithValue() that is baked by just `map[reflect.Type]any` instead of a stack of contexts.
Diffstat (limited to 'pathrouter/variable.go')
-rw-r--r--pathrouter/variable.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/pathrouter/variable.go b/pathrouter/variable.go
index e712f46..0cb44aa 100644
--- a/pathrouter/variable.go
+++ b/pathrouter/variable.go
@@ -6,8 +6,9 @@ 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],
+// The value of the first path element will be collected into the [Context],
// and the remaining subpath will have to be accepted by the next matcher.
+// The collected path element can be retrieved with [Context.Variable].
func Variable(name string, matcher Matcher) Matcher {
return variable(name, matcher.downcast())
}
@@ -16,15 +17,15 @@ 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 {
+ accept: func(path []string, rc Context) HandlerFunc {
if len(path) == 0 || path[0] == "" {
return nil
}
- handlerFunc := matcher.accept(path[1:], vars)
+ handlerFunc := matcher.accept(path[1:], rc)
if handlerFunc == nil {
return nil
}
- vars[name] = pathUnescape(path[0])
+ rc.vars[name] = pathUnescape(path[0])
return handlerFunc
},
}