Add route working correctly, callbacks worked into method map.

This commit is contained in:
2020-09-01 19:16:08 -07:00
parent d240fa1b0c
commit a791b26248
2 changed files with 24 additions and 11 deletions

View File

@ -2,7 +2,6 @@ package router
import ( import (
"errors" "errors"
"fmt"
"net/http" "net/http"
"strings" "strings"
) )
@ -15,8 +14,7 @@ type route struct {
type segment struct { type segment struct {
path string path string
methods map[string]bool methods map[string]http.HandlerFunc
callback http.HandlerFunc
children map[string]*segment children map[string]*segment
} }
@ -37,15 +35,19 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc)
if r.lookup == nil { if r.lookup == nil {
r.lookup = &segment{} r.lookup = &segment{}
r.lookup.children = map[string]*segment{} r.lookup.children = map[string]*segment{}
r.lookup.methods = map[string]bool{} r.lookup.methods = map[string]http.HandlerFunc{}
r.lookup.path = "/"
} }
curr := r.lookup curr := r.lookup
fmt.Printf("Keys: %v", len(keys)) for i, key := range keys {
if i == 0 {
continue
}
for _, key := range keys {
var seg segment var seg segment
if child, ok := curr.children[key]; !ok { if child, ok := curr.children[key]; !ok {
seg = *newSegment(curr.path, key) seg = *newSegment(curr.path, key)
curr.children[key] = &seg curr.children[key] = &seg
@ -55,13 +57,12 @@ func (r *Router) AddRoute(method string, path string, callback http.HandlerFunc)
} }
} }
if curr.methods[method] { if _, ok := curr.methods[method]; ok {
err = errors.New("path already exists") err = errors.New("path already exists")
} }
if err == nil { if err == nil {
curr.callback = callback curr.methods[method] = callback
curr.methods[method] = true
r.routes = append(r.routes, route{method, path, callback}) r.routes = append(r.routes, route{method, path, callback})
} }
@ -111,7 +112,7 @@ func newSegment(parentPath string, key string) (seg *segment) {
} }
seg = &segment{} seg = &segment{}
seg.children = map[string]*segment{} seg.children = map[string]*segment{}
seg.methods = map[string]bool{} seg.methods = map[string]http.HandlerFunc{}
seg.path = path seg.path = path
return return

View File

@ -28,11 +28,23 @@ func TestAddRouter(t *testing.T) {
t.Error("The route was not correctly added to the router: ", err) t.Error("The route was not correctly added to the router: ", err)
} }
err = addAndCheckRoute(&r, http.MethodDelete, "/items/thing/man/bird/horse/poop", func(http.ResponseWriter, *http.Request) {}, &routeCounter)
if err != nil {
t.Error("The route was not correctly added to the router: ", err)
}
err = addAndCheckRoute(&r, http.MethodDelete, "/items/thing/man/bird/cat/poop", func(http.ResponseWriter, *http.Request) {}, &routeCounter)
if err != nil {
t.Error("The route was not correctly added to the router: ", err)
}
checkLookup(r.lookup) checkLookup(r.lookup)
} }
func checkLookup(curr *segment) { func checkLookup(curr *segment) {
fmt.Printf("%p { path: %s, methods: %v, children: %v, callback: %p }\n", curr, curr.path, curr.methods, curr.children, curr.callback) fmt.Printf("%p { path: %s, methods: %v, children: %v}\n", curr, curr.path, curr.methods, curr.children)
for _, v := range curr.children { for _, v := range curr.children {
checkLookup(v) checkLookup(v)