Add matching routes with path parameters.
This commit is contained in:
66
router.go
66
router.go
@ -33,7 +33,8 @@ type segment struct {
|
|||||||
children map[string]*segment
|
children map[string]*segment
|
||||||
methods map[string]http.HandlerFunc
|
methods map[string]http.HandlerFunc
|
||||||
path string
|
path string
|
||||||
variable string
|
parameter *segment
|
||||||
|
parameterName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFoundHandler is the default function for handling routes that are not found. If you wish to
|
// NotFoundHandler is the default function for handling routes that are not found. If you wish to
|
||||||
@ -64,8 +65,7 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if child, ok := curr.children[key]; !ok {
|
if child, ok := curr.children[key]; !ok {
|
||||||
seg := newSegment(curr.path, key)
|
seg := addSegment(curr, key)
|
||||||
curr.children[key] = seg
|
|
||||||
curr = seg
|
curr = seg
|
||||||
} else {
|
} else {
|
||||||
curr = child
|
curr = child
|
||||||
@ -114,11 +114,13 @@ func (r *Router) Handler(req *http.Request) (h http.Handler, pattern string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if seg, ok := curr.children[v]; ok {
|
seg := getChild(v, curr)
|
||||||
curr = seg
|
|
||||||
} else {
|
if seg == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
curr = seg
|
||||||
}
|
}
|
||||||
|
|
||||||
if cb, ok := curr.methods[method]; ok {
|
if cb, ok := curr.methods[method]; ok {
|
||||||
@ -142,21 +144,45 @@ func (r Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func addSegment(curr *segment, keys []string) (seg *segment) {
|
func addSegment(curr *segment, key string) (seg *segment) {
|
||||||
for _, key := range keys {
|
if curr.parameterName == key {
|
||||||
if child, ok := curr.children[key]; !ok {
|
seg = curr.parameter
|
||||||
seg = newSegment(curr.path, key)
|
|
||||||
curr.children[key] = seg
|
} else if child, ok := curr.children[key]; !ok { // child does not match...
|
||||||
curr = seg
|
var isParam bool
|
||||||
|
|
||||||
|
seg, isParam = newSegment(curr.path, key)
|
||||||
|
|
||||||
|
if isParam {
|
||||||
|
curr.parameter = seg
|
||||||
|
curr.parameterName = key[2:]
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
curr = child
|
curr.children[key] = seg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
} else { // child matches...
|
||||||
|
seg = child
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSegment(parentPath string, key string) (seg *segment) {
|
func getChild(key string, curr *segment) (child *segment) {
|
||||||
|
if seg, ok := curr.children[key]; ok { // is there an exact match?
|
||||||
|
|
||||||
|
child = seg
|
||||||
|
} else if curr.parameter != nil { // could this be a parameter?
|
||||||
|
child = curr.parameter
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSegment(parentPath string, key string) (seg *segment, isParam bool) {
|
||||||
var path string
|
var path string
|
||||||
|
|
||||||
if parentPath == "/" {
|
if parentPath == "/" {
|
||||||
@ -172,8 +198,8 @@ func newSegment(parentPath string, key string) (seg *segment) {
|
|||||||
seg.methods = map[string]http.HandlerFunc{}
|
seg.methods = map[string]http.HandlerFunc{}
|
||||||
seg.path = path
|
seg.path = path
|
||||||
|
|
||||||
if isVariable(key) {
|
if isParameter(key) {
|
||||||
seg.variable = key[1:]
|
isParam = true
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@ -190,13 +216,13 @@ func setupKeys(slice []string) (keys []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// isVariable returns true if the key is more than one character long and starts with a ':'
|
// isParameter returns true if the key is more than one character long and starts with a ':'
|
||||||
func isVariable(key string) (isVar bool) {
|
func isParameter(key string) (isVar bool) {
|
||||||
if len(key) <= 1 {
|
if len([]rune(key)) <= 2 {
|
||||||
return // avoid empty variables, i.e. /somepath/:/someotherpath
|
return // avoid empty variables, i.e. /somepath/:/someotherpath
|
||||||
}
|
}
|
||||||
|
|
||||||
if key[0] != ':' {
|
if key[1] != ':' {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +49,8 @@ func TestServeHTTP(t *testing.T) {
|
|||||||
defer testOutcome("can find correct callback function", t)
|
defer testOutcome("can find correct callback function", t)
|
||||||
|
|
||||||
router := Router{}
|
router := Router{}
|
||||||
path := "/items"
|
path := "/items/things/stuff"
|
||||||
expectedBody := "I am /items"
|
expectedBody := "I am /items/things/stuff"
|
||||||
expectedCode := 200
|
expectedCode := 200
|
||||||
|
|
||||||
router.AddRoute(http.MethodGet, path, func(w http.ResponseWriter, r *http.Request) {
|
router.AddRoute(http.MethodGet, path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -66,6 +66,39 @@ func TestServeHTTP(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
path = "/"
|
||||||
|
expectedBody = "I am /"
|
||||||
|
|
||||||
|
router.AddRoute(http.MethodGet, path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(expectedCode)
|
||||||
|
w.Write([]byte(expectedBody))
|
||||||
|
})
|
||||||
|
|
||||||
|
err = matchAndCheckRoute(&router, http.MethodGet, path, expectedBody, expectedCode)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Did not find the expected callback handler", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
path = "/items/:itemid/edit"
|
||||||
|
expectedBody = "I have a path param"
|
||||||
|
reqPath := "/items/this-is-an-id/edit"
|
||||||
|
|
||||||
|
router.AddRoute(http.MethodGet, path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(expectedCode)
|
||||||
|
w.Write([]byte(expectedBody))
|
||||||
|
})
|
||||||
|
|
||||||
|
err = matchAndCheckRoute(&router, http.MethodGet, reqPath, expectedBody, expectedCode)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Did not find the expected callback handler", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchAndCheckRoute(r *Router, method string, path string, expectedBody string, expectedCode int) (err error) {
|
func matchAndCheckRoute(r *Router, method string, path string, expectedBody string, expectedCode int) (err error) {
|
||||||
@ -156,10 +189,14 @@ func testOutcome(message string, t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkLookup prints out the various saved routes. It's not needed for any test, but is a helpful debugging tool.
|
// checkLookup prints out the various saved routes. It's not needed for any test, but is a helpful debugging tool.
|
||||||
// func checkLookup(curr *segment) {
|
func checkLookup(curr *segment) {
|
||||||
// fmt.Printf("%p { path: %s, methods: %v, children: %v}\n", curr, curr.path, curr.methods, curr.children)
|
fmt.Printf("%p { path: \"%s\", methods: %v, children: %v, parameter: %v, parameterName: \"%s\"}\n", curr, curr.path, curr.methods, curr.children, curr.parameter, curr.parameterName)
|
||||||
|
|
||||||
// for _, v := range curr.children {
|
for _, v := range curr.children {
|
||||||
// checkLookup(v)
|
checkLookup(v)
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
if curr.parameter != nil {
|
||||||
|
checkLookup(curr.parameter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user