Browse Source

Remove context helpers in context.go

pull/525/head
Franklin Harding 6 years ago
parent
commit
51b0355262
  1. 18
      context.go
  2. 19
      mux.go
  3. 2
      test_helpers.go

18
context.go

@ -1,18 +0,0 @@
package mux
import (
"context"
"net/http"
)
func contextGet(r *http.Request, key interface{}) interface{} {
return r.Context().Value(key)
}
func contextSet(r *http.Request, key, val interface{}) *http.Request {
if val == nil {
return r
}
return r.WithContext(context.WithValue(r.Context(), key, val))
}

19
mux.go

@ -5,6 +5,7 @@
package mux package mux
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
@ -195,8 +196,8 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var handler http.Handler var handler http.Handler
if r.Match(req, &match) { if r.Match(req, &match) {
handler = match.Handler handler = match.Handler
req = setVars(req, match.Vars) req = requestWithVars(req, match.Vars)
req = setCurrentRoute(req, match.Route) req = requestWithRoute(req, match.Route)
} }
if handler == nil && match.MatchErr == ErrMethodMismatch { if handler == nil && match.MatchErr == ErrMethodMismatch {
@ -426,7 +427,7 @@ const (
// Vars returns the route variables for the current request, if any. // Vars returns the route variables for the current request, if any.
func Vars(r *http.Request) map[string]string { func Vars(r *http.Request) map[string]string {
if rv := contextGet(r, varsKey); rv != nil { if rv := r.Context().Value(varsKey); rv != nil {
return rv.(map[string]string) return rv.(map[string]string)
} }
return nil return nil
@ -438,18 +439,20 @@ func Vars(r *http.Request) map[string]string {
// after the handler returns, unless the KeepContext option is set on the // after the handler returns, unless the KeepContext option is set on the
// Router. // Router.
func CurrentRoute(r *http.Request) *Route { func CurrentRoute(r *http.Request) *Route {
if rv := contextGet(r, routeKey); rv != nil { if rv := r.Context().Value(routeKey); rv != nil {
return rv.(*Route) return rv.(*Route)
} }
return nil return nil
} }
func setVars(r *http.Request, val interface{}) *http.Request { func requestWithVars(r *http.Request, val interface{}) *http.Request {
return contextSet(r, varsKey, val) ctx := context.WithValue(r.Context(), varsKey, val)
return r.WithContext(ctx)
} }
func setCurrentRoute(r *http.Request, val interface{}) *http.Request { func requestWithRoute(r *http.Request, val interface{}) *http.Request {
return contextSet(r, routeKey, val) ctx := context.WithValue(r.Context(), routeKey, val)
return r.WithContext(ctx)
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

2
test_helpers.go

@ -15,5 +15,5 @@ import "net/http"
// can be set by making a route that captures the required variables, // can be set by making a route that captures the required variables,
// starting a server and sending the request to that server. // starting a server and sending the request to that server.
func SetURLVars(r *http.Request, val map[string]string) *http.Request { func SetURLVars(r *http.Request, val map[string]string) *http.Request {
return setVars(r, val) return requestWithVars(r, val)
} }

Loading…
Cancel
Save