Created
February 26, 2012 15:42
-
-
Save jinzhu/1917427 to your computer and use it in GitHub Desktop.
[PATCH form Gorilla] support pass variables from routes to handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From a948c13b7073af9a0af4e3615cf4a245056a42f2 Mon Sep 17 00:00:00 2001 | |
From: Jinzhu <[email protected]> | |
Date: Sun, 26 Feb 2012 23:34:58 +0800 | |
Subject: [PATCH] support pass variables from routes to handler | |
--- | |
route.go | 18 ++++++++++++++++++ | |
1 file changed, 18 insertions(+) | |
diff --git a/route.go b/route.go | |
index 8a4b521..064f338 100644 | |
--- a/route.go | |
+++ b/route.go | |
@@ -29,6 +29,8 @@ type Route struct { | |
name string | |
// Error resulted from building a route. | |
err error | |
+ // Pass Variable to handler | |
+ vars map[string]string | |
} | |
// Match matches the route against the request. | |
@@ -54,6 +56,10 @@ func (r *Route) match(req *http.Request, match *RouteMatch) bool { | |
if match.Vars == nil { | |
match.Vars = make(map[string]string) | |
} | |
+ for i, v := range r.vars { | |
+ match.Vars[i] = v | |
+ } | |
+ | |
// Set variables. | |
if r.regexp != nil { | |
r.regexp.setMatch(req, match, r) | |
@@ -94,6 +100,18 @@ func (r *Route) GetHandler() http.Handler { | |
// Name sets the name for the route, used to build URLs. | |
// If the name was registered already it will be overwritten. | |
+func (r *Route) Params(vars map[string]string) *Route { | |
+ if r.vars == nil { | |
+ r.vars = make(map[string]string) | |
+ } | |
+ | |
+ for i, v := range vars { | |
+ r.vars[i] = v | |
+ } | |
+ | |
+ return r | |
+} | |
+ | |
func (r *Route) Name(name string) *Route { | |
if r.name != "" { | |
r.err = fmt.Errorf("mux: route already has name %q, can't set %q", | |
-- | |
1.7.9.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment