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/catchallvariable.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'pathrouter/catchallvariable.go') 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 -- cgit v1.3.1