diff --git a/gee-web/day3-router/gee/router_test.go b/gee-web/day3-router/gee/router_test.go index f5d6da3..39fabd2 100644 --- a/gee-web/day3-router/gee/router_test.go +++ b/gee-web/day3-router/gee/router_test.go @@ -9,8 +9,10 @@ import ( func newTestRouter() *router { r := newRouter() r.addRoute("GET", "/", nil) + r.addRoute("GET", "/hello/bc", nil) r.addRoute("GET", "/hello/:name", nil) r.addRoute("GET", "/hello/b/c", nil) + r.addRoute("GET", "/hello/bcb", nil) r.addRoute("GET", "/hi/:name", nil) r.addRoute("GET", "/assets/*filepath", nil) return r @@ -27,7 +29,28 @@ func TestParsePattern(t *testing.T) { func TestGetRoute(t *testing.T) { r := newTestRouter() - n, ps := r.getRoute("GET", "/hello/geektutu") + + n, ps := r.getRoute("GET", "/hello/bcb") + if n == nil || n.part != "bcb" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bc") + if n == nil || n.part != "bc" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/b/c") + if n == nil || n.part != "c" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bvv/c") + if n != nil { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/geektutu") if n == nil { t.Fatal("nil shouldn't be returned") diff --git a/gee-web/day3-router/gee/trie.go b/gee-web/day3-router/gee/trie.go index 043775c..c1e41f2 100644 --- a/gee-web/day3-router/gee/trie.go +++ b/gee-web/day3-router/gee/trie.go @@ -40,9 +40,9 @@ func (n *node) search(parts []string, height int) *node { } part := parts[height] - children := n.matchChildren(part) + child := n.matchChildren(part) - for _, child := range children { + if child != nil { result := child.search(parts, height+1) if result != nil { return result @@ -61,21 +61,26 @@ func (n *node) travel(list *([]*node)) { } } +// Find a matching child node func (n *node) matchChild(part string) *node { for _, child := range n.children { - if child.part == part || child.isWild { + if child.part == part { return child } } return nil } -func (n *node) matchChildren(part string) []*node { - nodes := make([]*node, 0) +// Find all eligible child nodes +func (n *node) matchChildren(part string) *node { + //var nodeMatch *node + var nodeWild *node for _, child := range n.children { - if child.part == part || child.isWild { - nodes = append(nodes, child) + if child.part == part { + return child + } else if child.isWild { + nodeWild = child } } - return nodes + return nodeWild } diff --git a/gee-web/day4-group/gee/router_test.go b/gee-web/day4-group/gee/router_test.go index f5d6da3..39fabd2 100644 --- a/gee-web/day4-group/gee/router_test.go +++ b/gee-web/day4-group/gee/router_test.go @@ -9,8 +9,10 @@ import ( func newTestRouter() *router { r := newRouter() r.addRoute("GET", "/", nil) + r.addRoute("GET", "/hello/bc", nil) r.addRoute("GET", "/hello/:name", nil) r.addRoute("GET", "/hello/b/c", nil) + r.addRoute("GET", "/hello/bcb", nil) r.addRoute("GET", "/hi/:name", nil) r.addRoute("GET", "/assets/*filepath", nil) return r @@ -27,7 +29,28 @@ func TestParsePattern(t *testing.T) { func TestGetRoute(t *testing.T) { r := newTestRouter() - n, ps := r.getRoute("GET", "/hello/geektutu") + + n, ps := r.getRoute("GET", "/hello/bcb") + if n == nil || n.part != "bcb" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bc") + if n == nil || n.part != "bc" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/b/c") + if n == nil || n.part != "c" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bvv/c") + if n != nil { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/geektutu") if n == nil { t.Fatal("nil shouldn't be returned") diff --git a/gee-web/day4-group/gee/trie.go b/gee-web/day4-group/gee/trie.go index 043775c..c1e41f2 100644 --- a/gee-web/day4-group/gee/trie.go +++ b/gee-web/day4-group/gee/trie.go @@ -40,9 +40,9 @@ func (n *node) search(parts []string, height int) *node { } part := parts[height] - children := n.matchChildren(part) + child := n.matchChildren(part) - for _, child := range children { + if child != nil { result := child.search(parts, height+1) if result != nil { return result @@ -61,21 +61,26 @@ func (n *node) travel(list *([]*node)) { } } +// Find a matching child node func (n *node) matchChild(part string) *node { for _, child := range n.children { - if child.part == part || child.isWild { + if child.part == part { return child } } return nil } -func (n *node) matchChildren(part string) []*node { - nodes := make([]*node, 0) +// Find all eligible child nodes +func (n *node) matchChildren(part string) *node { + //var nodeMatch *node + var nodeWild *node for _, child := range n.children { - if child.part == part || child.isWild { - nodes = append(nodes, child) + if child.part == part { + return child + } else if child.isWild { + nodeWild = child } } - return nodes + return nodeWild } diff --git a/gee-web/day5-middleware/gee/router_test.go b/gee-web/day5-middleware/gee/router_test.go index f5d6da3..39fabd2 100644 --- a/gee-web/day5-middleware/gee/router_test.go +++ b/gee-web/day5-middleware/gee/router_test.go @@ -9,8 +9,10 @@ import ( func newTestRouter() *router { r := newRouter() r.addRoute("GET", "/", nil) + r.addRoute("GET", "/hello/bc", nil) r.addRoute("GET", "/hello/:name", nil) r.addRoute("GET", "/hello/b/c", nil) + r.addRoute("GET", "/hello/bcb", nil) r.addRoute("GET", "/hi/:name", nil) r.addRoute("GET", "/assets/*filepath", nil) return r @@ -27,7 +29,28 @@ func TestParsePattern(t *testing.T) { func TestGetRoute(t *testing.T) { r := newTestRouter() - n, ps := r.getRoute("GET", "/hello/geektutu") + + n, ps := r.getRoute("GET", "/hello/bcb") + if n == nil || n.part != "bcb" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bc") + if n == nil || n.part != "bc" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/b/c") + if n == nil || n.part != "c" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bvv/c") + if n != nil { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/geektutu") if n == nil { t.Fatal("nil shouldn't be returned") diff --git a/gee-web/day5-middleware/gee/trie.go b/gee-web/day5-middleware/gee/trie.go index 043775c..c1e41f2 100644 --- a/gee-web/day5-middleware/gee/trie.go +++ b/gee-web/day5-middleware/gee/trie.go @@ -40,9 +40,9 @@ func (n *node) search(parts []string, height int) *node { } part := parts[height] - children := n.matchChildren(part) + child := n.matchChildren(part) - for _, child := range children { + if child != nil { result := child.search(parts, height+1) if result != nil { return result @@ -61,21 +61,26 @@ func (n *node) travel(list *([]*node)) { } } +// Find a matching child node func (n *node) matchChild(part string) *node { for _, child := range n.children { - if child.part == part || child.isWild { + if child.part == part { return child } } return nil } -func (n *node) matchChildren(part string) []*node { - nodes := make([]*node, 0) +// Find all eligible child nodes +func (n *node) matchChildren(part string) *node { + //var nodeMatch *node + var nodeWild *node for _, child := range n.children { - if child.part == part || child.isWild { - nodes = append(nodes, child) + if child.part == part { + return child + } else if child.isWild { + nodeWild = child } } - return nodes + return nodeWild } diff --git a/gee-web/day6-template/gee/router_test.go b/gee-web/day6-template/gee/router_test.go index f5d6da3..39fabd2 100644 --- a/gee-web/day6-template/gee/router_test.go +++ b/gee-web/day6-template/gee/router_test.go @@ -9,8 +9,10 @@ import ( func newTestRouter() *router { r := newRouter() r.addRoute("GET", "/", nil) + r.addRoute("GET", "/hello/bc", nil) r.addRoute("GET", "/hello/:name", nil) r.addRoute("GET", "/hello/b/c", nil) + r.addRoute("GET", "/hello/bcb", nil) r.addRoute("GET", "/hi/:name", nil) r.addRoute("GET", "/assets/*filepath", nil) return r @@ -27,7 +29,28 @@ func TestParsePattern(t *testing.T) { func TestGetRoute(t *testing.T) { r := newTestRouter() - n, ps := r.getRoute("GET", "/hello/geektutu") + + n, ps := r.getRoute("GET", "/hello/bcb") + if n == nil || n.part != "bcb" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bc") + if n == nil || n.part != "bc" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/b/c") + if n == nil || n.part != "c" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bvv/c") + if n != nil { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/geektutu") if n == nil { t.Fatal("nil shouldn't be returned") diff --git a/gee-web/day6-template/gee/trie.go b/gee-web/day6-template/gee/trie.go index 043775c..c1e41f2 100644 --- a/gee-web/day6-template/gee/trie.go +++ b/gee-web/day6-template/gee/trie.go @@ -40,9 +40,9 @@ func (n *node) search(parts []string, height int) *node { } part := parts[height] - children := n.matchChildren(part) + child := n.matchChildren(part) - for _, child := range children { + if child != nil { result := child.search(parts, height+1) if result != nil { return result @@ -61,21 +61,26 @@ func (n *node) travel(list *([]*node)) { } } +// Find a matching child node func (n *node) matchChild(part string) *node { for _, child := range n.children { - if child.part == part || child.isWild { + if child.part == part { return child } } return nil } -func (n *node) matchChildren(part string) []*node { - nodes := make([]*node, 0) +// Find all eligible child nodes +func (n *node) matchChildren(part string) *node { + //var nodeMatch *node + var nodeWild *node for _, child := range n.children { - if child.part == part || child.isWild { - nodes = append(nodes, child) + if child.part == part { + return child + } else if child.isWild { + nodeWild = child } } - return nodes + return nodeWild } diff --git a/gee-web/day7-panic-recover/gee/router_test.go b/gee-web/day7-panic-recover/gee/router_test.go index f5d6da3..39fabd2 100644 --- a/gee-web/day7-panic-recover/gee/router_test.go +++ b/gee-web/day7-panic-recover/gee/router_test.go @@ -9,8 +9,10 @@ import ( func newTestRouter() *router { r := newRouter() r.addRoute("GET", "/", nil) + r.addRoute("GET", "/hello/bc", nil) r.addRoute("GET", "/hello/:name", nil) r.addRoute("GET", "/hello/b/c", nil) + r.addRoute("GET", "/hello/bcb", nil) r.addRoute("GET", "/hi/:name", nil) r.addRoute("GET", "/assets/*filepath", nil) return r @@ -27,7 +29,28 @@ func TestParsePattern(t *testing.T) { func TestGetRoute(t *testing.T) { r := newTestRouter() - n, ps := r.getRoute("GET", "/hello/geektutu") + + n, ps := r.getRoute("GET", "/hello/bcb") + if n == nil || n.part != "bcb" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bc") + if n == nil || n.part != "bc" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/b/c") + if n == nil || n.part != "c" || len(ps) > 0 { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/bvv/c") + if n != nil { + t.Fatal("nil shouldn't be returned") + } + + n, ps = r.getRoute("GET", "/hello/geektutu") if n == nil { t.Fatal("nil shouldn't be returned") diff --git a/gee-web/day7-panic-recover/gee/trie.go b/gee-web/day7-panic-recover/gee/trie.go index 043775c..c1e41f2 100644 --- a/gee-web/day7-panic-recover/gee/trie.go +++ b/gee-web/day7-panic-recover/gee/trie.go @@ -40,9 +40,9 @@ func (n *node) search(parts []string, height int) *node { } part := parts[height] - children := n.matchChildren(part) + child := n.matchChildren(part) - for _, child := range children { + if child != nil { result := child.search(parts, height+1) if result != nil { return result @@ -61,21 +61,26 @@ func (n *node) travel(list *([]*node)) { } } +// Find a matching child node func (n *node) matchChild(part string) *node { for _, child := range n.children { - if child.part == part || child.isWild { + if child.part == part { return child } } return nil } -func (n *node) matchChildren(part string) []*node { - nodes := make([]*node, 0) +// Find all eligible child nodes +func (n *node) matchChildren(part string) *node { + //var nodeMatch *node + var nodeWild *node for _, child := range n.children { - if child.part == part || child.isWild { - nodes = append(nodes, child) + if child.part == part { + return child + } else if child.isWild { + nodeWild = child } } - return nodes + return nodeWild }