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

2
test_helpers.go

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

Loading…
Cancel
Save