-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pattern aliases #675
base: main
Are you sure you want to change the base?
pattern aliases #675
Changes from 6 commits
27f0c4a
cddaa96
446cd97
95b356a
54dd42c
9047e6a
306e54f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mux_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gorilla/mux" | ||
"net/http" | ||
) | ||
|
||
// This example demonstrates alias pattern registration on router | ||
func ExampleRouter_RegisterPattern() { | ||
|
||
r := mux.NewRouter().RegisterPattern("uuid", "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}") | ||
route := r.Path("/category/{id:uuid}") | ||
|
||
yes, _ := http.NewRequest("GET", "example.co/category/abe193ed-e0bc-4e1b-8e3c-736d5b381b60", nil) | ||
no, _ := http.NewRequest("GET", "example.co/category/42", nil) | ||
|
||
mathInfo := &mux.RouteMatch{} | ||
fmt.Printf("Match: %v %q\n", route.Match(yes, mathInfo), yes.URL.Path) | ||
fmt.Printf("Match: %v %q\n", route.Match(no, mathInfo), no.URL.Path) | ||
|
||
// Output | ||
// Match: true /category/abe193ed-e0bc-4e1b-8e3c-736d5b381b60 | ||
// Match: false /category/42 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,8 @@ type routeConf struct { | |
buildScheme string | ||
|
||
buildVarsFunc BuildVarsFunc | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, can we add comment here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added comments |
||
registeredPatterns map[string]string | ||
} | ||
|
||
// returns an effective deep copy of `routeConf` | ||
|
@@ -124,6 +126,14 @@ func copyRouteRegexp(r *routeRegexp) *routeRegexp { | |
return &c | ||
} | ||
|
||
func (r *Router) RegisterPattern(alias string, pattern string) *Router { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add comments here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added comments |
||
if r.registeredPatterns == nil { | ||
r.registeredPatterns = map[string]string{} | ||
} | ||
r.registeredPatterns[alias] = pattern | ||
return r | ||
} | ||
|
||
// Match attempts to match the given request against the router's registered routes. | ||
// | ||
// If the request matches a route of this router or one of its subrouters the Route, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,16 @@ func TestHost(t *testing.T) { | |
hostTemplate: `{v-1:[a-z]{3}}.{v-2:[a-z]{3}}.{v-3:[a-z]{3}}`, | ||
shouldMatch: true, | ||
}, | ||
{ | ||
title: "Host route with alias patterns", | ||
route: new(Route).RegisterPattern("version", "[a-z]{3}").Host("{v-1:version}.{v-2:version}.{v-3:version}"), | ||
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"), | ||
vars: map[string]string{"v-1": "aaa", "v-2": "bbb", "v-3": "ccc"}, | ||
host: "aaa.bbb.ccc", | ||
path: "", | ||
hostTemplate: `{v-1:version}.{v-2:version}.{v-3:version}`, | ||
shouldMatch: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add negative test case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added negative test case |
||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.title, func(t *testing.T) { | ||
|
@@ -449,6 +459,36 @@ func TestPath(t *testing.T) { | |
pathTemplate: `/{category:a|b/c}/{product}/{id:[0-9]+}`, | ||
shouldMatch: true, | ||
}, | ||
{ | ||
title: "Path route with regexp alias patterns", | ||
route: new(Route).RegisterPattern("digits", "[0-9]+").Path("/{id:digits}"), | ||
request: newRequest("GET", "http://localhost/1"), | ||
vars: map[string]string{"id": "1"}, | ||
host: "", | ||
path: "/1", | ||
pathTemplate: `/{id:digits}`, | ||
shouldMatch: true, | ||
}, | ||
{ | ||
title: "Path route with regexp alias patterns", | ||
route: new(Route).RegisterPattern("uuid", "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}").Path("/{category:uuid}/{product:uuid}"), | ||
request: newRequest("GET", "http://localhost/dce51145-5cc3-4b54-bfb0-7bdb64a67e4d/a385ddcb-278e-4234-93dd-4d7b0fcb95c1"), | ||
vars: map[string]string{"category": "dce51145-5cc3-4b54-bfb0-7bdb64a67e4d", "product": "a385ddcb-278e-4234-93dd-4d7b0fcb95c1"}, | ||
host: "", | ||
path: "/dce51145-5cc3-4b54-bfb0-7bdb64a67e4d/a385ddcb-278e-4234-93dd-4d7b0fcb95c1", | ||
pathTemplate: `/{category:uuid}/{product:uuid}`, | ||
shouldMatch: true, | ||
}, | ||
{ | ||
title: "Path route with regexp alias patterns passed through router", | ||
route: NewRouter().RegisterPattern("digits", "[0-9]+").Path("/{id:digits}"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both the test cases look similar to me.
{
title: "Path route with regexp alias patterns",
route: new(Route).RegisterPattern("digits", "[0-9]+").Path("/{id:digits}"),
request: newRequest("GET", "http://localhost/1"),
vars: map[string]string{"id": "1"},
host: "",
path: "/1",
pathTemplate: `/{id:digits}`,
shouldMatch: true,
}
{
title: "Path route with regexp alias patterns passed through router",
route: NewRouter().RegisterPattern("digits", "[0-9]+").Path("/{id:digits}"),
request: newRequest("GET", "http://localhost/1"),
vars: map[string]string{"id": "1"},
host: "",
path: "/1",
pathTemplate: `/{id:digits}`,
shouldMatch: true,
} We should add at least one negative test case. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, seems like this tests was very similar, deleted second test and added negative test case |
||
request: newRequest("GET", "http://localhost/1"), | ||
vars: map[string]string{"id": "1"}, | ||
host: "", | ||
path: "/1", | ||
pathTemplate: `/{id:digits}`, | ||
shouldMatch: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should move this test case in the file:
example_route_test.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
modev example