From 1635adc08aa413bbd7d2cf67e66ad6e9a823169a Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 2 Oct 2026 15:02:43 +0200 Subject: 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. --- pathrouter/context.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 pathrouter/context.go (limited to 'pathrouter/context.go') 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 +// 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] +} -- cgit v1.3.1