summaryrefslogtreecommitdiff
path: root/pathrouter
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
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')
-rw-r--r--pathrouter/catchallvariable.go11
-rw-r--r--pathrouter/choice.go4
-rw-r--r--pathrouter/context.go23
-rw-r--r--pathrouter/element.go4
-rw-r--r--pathrouter/handler.go8
-rw-r--r--pathrouter/here.go4
-rw-r--r--pathrouter/matcher.go12
-rw-r--r--pathrouter/pathrouter_test.go6
-rw-r--r--pathrouter/variable.go9
9 files changed, 53 insertions, 28 deletions
diff --git a/pathrouter/catchallvariable.go b/pathrouter/catchallvariable.go
index 1b98672..d1d3e2c 100644
--- a/pathrouter/catchallvariable.go
+++ b/pathrouter/catchallvariable.go
@@ -10,9 +10,10 @@ import (
)
// CatchAllVariable is a [Matcher] that accepts subpaths with an arbitrary number of path elements.
-// The value of any amount of leading path elements will be collected into vars[name],
+// The value of any amount of leading path elements will be collected into the [Context],
// such that the remainder is accepted by the next matcher.
-// At least one element must be collected into vars[name].
+// At least one element must be collected.
+// The full sequence of collected path elements can be retrieved with [Context.Variable].
//
// CatchAllVariable() may appear at any point within the routing tree,
// but it may not contain another CatchAllVariable() anywhere within it.
@@ -32,7 +33,7 @@ func catchAllVariable(name string, matcher realMatcher) Matcher {
panic("matcher within CatchAllVariable() may not accept unlimited path lengths")
}
- accept := func(path []string, vars map[string]string) HandlerFunc {
+ accept := func(path []string, rc Context) HandlerFunc {
for length := innerMinLength; length <= innerMaxLength; length++ {
if length > len(path) {
break
@@ -42,11 +43,11 @@ func catchAllVariable(name string, matcher realMatcher) Matcher {
continue
}
- handlerFunc := matcher.accept(subpath, vars)
+ handlerFunc := matcher.accept(subpath, rc)
if handlerFunc == nil {
continue
}
- vars[name] = pathUnescape(strings.Join(caughtPath, "/"))
+ rc.vars[name] = pathUnescape(strings.Join(caughtPath, "/"))
return handlerFunc
}
return nil
diff --git a/pathrouter/choice.go b/pathrouter/choice.go
index 09026b8..d107785 100644
--- a/pathrouter/choice.go
+++ b/pathrouter/choice.go
@@ -48,9 +48,9 @@ func choice(matchers []realMatcher) Matcher {
return realMatcher{
minLength: slices.Min(minLengths),
maxLength: maxLength,
- accept: func(path []string, vars map[string]string) HandlerFunc {
+ accept: func(path []string, rc Context) HandlerFunc {
for _, m := range matchers {
- hf := m.accept(path, vars)
+ hf := m.accept(path, rc)
if hf != nil {
return hf
}
diff --git a/pathrouter/context.go b/pathrouter/context.go
new file mode 100644
index 0000000..d47e302
--- /dev/null
+++ b/pathrouter/context.go
@@ -0,0 +1,23 @@
+// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
+// SPDX-License-Identifier: Apache-2.0
+
+package pathrouter
+
+// Context contains information about an HTTP request that has been processed by matchers from this package.
+// It appears as an argument in [HandlerFunc].
+type Context struct {
+ vars map[string]string
+}
+
+func newContext() Context {
+ return Context{
+ vars: make(map[string]string),
+ }
+}
+
+// Variable returns the path element(s) collected by a [Variable] or [CatchAllVariable] matcher with the given name,
+// with any URL escaping removed.
+// If no matcher with this name appeared in the path leading up to the handler, the empty string is returned.
+func (rc Context) Variable(name string) string {
+ return rc.vars[name]
+}
diff --git a/pathrouter/element.go b/pathrouter/element.go
index 9512924..395fb40 100644
--- a/pathrouter/element.go
+++ b/pathrouter/element.go
@@ -24,11 +24,11 @@ func element(value 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] != value {
return nil
}
- return matcher.accept(path[1:], vars)
+ return matcher.accept(path[1:], rc)
},
}
}
diff --git a/pathrouter/handler.go b/pathrouter/handler.go
index ca493be..1ecabdd 100644
--- a/pathrouter/handler.go
+++ b/pathrouter/handler.go
@@ -16,7 +16,7 @@ import (
//
// Package pathrouter passes variables explicitly like this, instead of via [context.Context.WithValue],
// because doing so imposes a performance penalty for every usage of the respective context.
-type HandlerFunc = func(w http.ResponseWriter, r *http.Request, vars map[string]string)
+type HandlerFunc = func(w http.ResponseWriter, r *http.Request, rc Context)
// ByMethod is a set of request handlers matching the same request path, keyed on request method.
// It is commonly constructed as a literal using the respective constants from the net/http package, such as [http.MethodGet].
@@ -62,10 +62,10 @@ func Handlers(m ByMethod) Matcher {
// precomputations for accept()
allowHeader := m.buildAllowHeader()
- serve := func(w http.ResponseWriter, r *http.Request, vars map[string]string) {
+ serve := func(w http.ResponseWriter, r *http.Request, rc Context) {
handler, ok := m[r.Method]
if ok {
- handler(w, r, vars)
+ handler(w, r, rc)
return
}
@@ -80,7 +80,7 @@ func Handlers(m ByMethod) Matcher {
return realMatcher{
minLength: 0,
maxLength: Some(0),
- accept: func(path []string, vars map[string]string) HandlerFunc {
+ accept: func(path []string, rc Context) HandlerFunc {
if len(path) != 0 {
return nil
}
diff --git a/pathrouter/here.go b/pathrouter/here.go
index a59cdf0..bf21016 100644
--- a/pathrouter/here.go
+++ b/pathrouter/here.go
@@ -43,9 +43,9 @@ func here(matcher realMatcher) Matcher {
return realMatcher{
minLength: 0,
maxLength: Some(1),
- accept: func(path []string, vars map[string]string) HandlerFunc {
+ accept: func(path []string, rc Context) HandlerFunc {
if len(path) == 0 || (len(path) == 1 && path[0] == "") {
- return matcher.accept(nil, vars)
+ return matcher.accept(nil, rc)
}
return nil
},
diff --git a/pathrouter/matcher.go b/pathrouter/matcher.go
index f0f98ec..45ba818 100644
--- a/pathrouter/matcher.go
+++ b/pathrouter/matcher.go
@@ -51,8 +51,8 @@
// so any slashes that were encoded as %2F in the URL path will be considered part of a path element instead of a boundary.
//
// For instance, using the example above, the URL path "/v1/objects/42/23" would not match and generate a 404 response,
-// but the URL path "/v1/objects/42%2F23" would match and invoke (e.g. with method GET) the GetObject handler with vars["id"] = "42/23".
-// Like in this example, [HandlerFunc] will receive unescaped values in its vars argument.
+// but the URL path "/v1/objects/42%2F23" would match and invoke (e.g. with method GET) the GetObject handler with rc.Variable("id") = "42/23".
+// Like in this example, [HandlerFunc] will receive a [Context] in which [Context.Variable] calls returns unescaped values.
//
// # Implicit normalizations
//
@@ -97,7 +97,7 @@ type Matcher interface {
}
type realMatcher struct {
- accept func(path []string, vars map[string]string) HandlerFunc
+ accept func(path []string, rc Context) HandlerFunc
// The smallest len(path) that accept() can accept.
minLength int
@@ -115,12 +115,12 @@ func (m realMatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// TryServeHTTP implements the [Matcher] interface.
func (m realMatcher) TryServeHTTP(w http.ResponseWriter, r *http.Request) bool {
path := extractPath(r.URL)
- vars := make(map[string]string)
- handlerFunc := m.accept(path, vars)
+ rc := newContext()
+ handlerFunc := m.accept(path, rc)
if handlerFunc == nil {
return false
} else {
- handlerFunc(w, r, vars)
+ handlerFunc(w, r, rc)
return true
}
}
diff --git a/pathrouter/pathrouter_test.go b/pathrouter/pathrouter_test.go
index 54108ac..96083ed 100644
--- a/pathrouter/pathrouter_test.go
+++ b/pathrouter/pathrouter_test.go
@@ -19,10 +19,10 @@ import (
func TestRouting(t *testing.T) {
// helper methods
varRx := regexp.MustCompile(`\$\w+`)
- h := func(status int, msgBase string) func(w http.ResponseWriter, r *http.Request, vars map[string]string) {
- return func(w http.ResponseWriter, r *http.Request, vars map[string]string) {
+ h := func(status int, msgBase string) pr.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request, rc pr.Context) {
msg := varRx.ReplaceAllStringFunc(msgBase, func(match string) string {
- return vars[strings.TrimPrefix(match, "$")]
+ return rc.Variable(strings.TrimPrefix(match, "$"))
})
if r.Method == http.MethodHead {
w.WriteHeader(status)
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
},
}