Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion gee-web/day3-router/gee/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
21 changes: 13 additions & 8 deletions gee-web/day3-router/gee/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
25 changes: 24 additions & 1 deletion gee-web/day4-group/gee/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
21 changes: 13 additions & 8 deletions gee-web/day4-group/gee/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
25 changes: 24 additions & 1 deletion gee-web/day5-middleware/gee/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
21 changes: 13 additions & 8 deletions gee-web/day5-middleware/gee/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
25 changes: 24 additions & 1 deletion gee-web/day6-template/gee/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
21 changes: 13 additions & 8 deletions gee-web/day6-template/gee/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
25 changes: 24 additions & 1 deletion gee-web/day7-panic-recover/gee/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
Loading