aboutsummaryrefslogtreecommitdiff
path: root/pathrouter/context.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/context.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/context.go')
-rw-r--r--pathrouter/context.go23
1 files changed, 23 insertions, 0 deletions
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]
+}