diff options
Diffstat (limited to 'pathrouter/context.go')
| -rw-r--r-- | pathrouter/context.go | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/pathrouter/context.go b/pathrouter/context.go index d47e302..7408960 100644 --- a/pathrouter/context.go +++ b/pathrouter/context.go @@ -3,21 +3,61 @@ 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 { +import ( + "context" + "maps" + "net/http" +) + +type routingContextKeyType struct{} + +var routingContextKey = any(routingContextKeyType{}) + +// routingContext contains information about an HTTP request that has been processed by matchers from this package. +// It is stored in `r.Body` of the HTTP request by wrapping the original response body's behavior. +type routingContext struct { vars map[string]string } -func newContext() Context { - return Context{ +func injectContext(r *http.Request) (*http.Request, routingContext) { + // NOTE: Using context.WithValue() and thus extending the chain within r.Context by another element + // incurs a performance penalty whenever `r.Context` is used, but it's the option that sucks the least. + // We initially had `type HandlerFunc = func(http.ResponseWriter, *http.Request, pathrouter.Context)` + // to avoid this performance penalty, but having another thing that's called "Context" is confusing. + // The final API implementation accepts a slight performance decrease to prioritize developer ergonomics. + // + // The only other option that does not add an extra argument to the HandlerFunc signature is to stash + // `type routingContext` within `r.Body`, but that would incur the performance penalty on every use of + // `r.Body` instead of `r.Context()`, which is usually much worse. + + rc := routingContext{ vars: make(map[string]string), } + ctx := context.WithValue(r.Context(), routingContextKey, rc) + return r.WithContext(ctx), rc } -// Variable returns the path element(s) collected by a [Variable] or [CatchAllVariable] matcher with the given name, +// VariableValue returns the path element(s) collected by a [Variable] or [CatchAllVariable] matcher of the same 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 { +// +// Panics if r is not a request that was routed by a [Matcher] from this package. +func VariableValue(r *http.Request, name string) string { + rc, ok := r.Context().Value(routingContextKey).(routingContext) + if !ok { + panic("request was not routed by gg/pathrouter") + } return rc.vars[name] } + +// VariableValues returns all path element(s) collected by [Variable] or [CatchAllVariable] matchers, keyed on the name of the matcher. +// The result is the set of all possible values that [VariableValue] can return. +// +// Panics if r is not a request that was routed by a [Matcher] from this package. +func VariableValues(r *http.Request) map[string]string { + rc, ok := r.Context().Value(routingContextKey).(routingContext) + if !ok { + panic("request was not routed by gg/pathrouter") + } + return maps.Clone(rc.vars) +} |
