From 296afc7b9d43ae6d41a4d49bf387fd85698dd0e8 Mon Sep 17 00:00:00 2001 From: cry <1046707850@qq.com> Date: Thu, 30 May 2024 10:28:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8Dtrie=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E6=A0=91=E4=B8=AD=E6=9F=A5=E6=89=BEnode?= =?UTF-8?q?=E6=A8=A1=E7=B3=8A=E5=8C=B9=E9=85=8D=E7=9A=84bug=20bug:?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0"/hello/:name"=E8=B7=AF=E5=BE=84=E5=90=8E?= =?UTF-8?q?=E5=86=8D=E6=B7=BB=E5=8A=A0"/hello/b/c"=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=EF=BC=8C=E5=8C=B9=E9=85=8D"/hello/bvv/c"=E8=AF=A5=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=97=B6=EF=BC=8C=E6=9C=AC=E6=9D=A5=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E8=B7=AF=E5=BE=84=E4=BC=9A=E8=A2=AB=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E4=B8=BA"/hello/b/c"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gee-web/day3-router/gee/router_test.go | 25 ++++++++++++++++++- gee-web/day3-router/gee/trie.go | 21 ++++++++++------ gee-web/day4-group/gee/router_test.go | 25 ++++++++++++++++++- gee-web/day4-group/gee/trie.go | 21 ++++++++++------ gee-web/day5-middleware/gee/router_test.go | 25 ++++++++++++++++++- gee-web/day5-middleware/gee/trie.go | 21 ++++++++++------ gee-web/day6-template/gee/router_test.go | 25 ++++++++++++++++++- gee-web/day6-template/gee/trie.go | 21 ++++++++++------ gee-web/day7-panic-recover/gee/router_test.go | 25 ++++++++++++++++++- gee-web/day7-panic-recover/gee/trie.go | 21 ++++++++++------ 10 files changed, 185 insertions(+), 45 deletions(-) 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 }