Skip to content

Commit

Permalink
Refactor methods naming
Browse files Browse the repository at this point in the history
  • Loading branch information
vardius committed Feb 26, 2018
1 parent 36afa72 commit 38258c0
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ language: go
go:
- 1.8
- 1.9
- "1.10"
- tip
script:
- go build
- go test -v -cover -race -coverprofile=coverage.txt -covermode=atomic
- go test -v -race -cover -coverprofile=coverage.txt -covermode=atomic
after_script:
- bash <(curl -s https://codecov.io/bash)
22 changes: 11 additions & 11 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (n *node) setChildren(children *tree) {

func (n *node) addChild(ids []string) *node {
if len(ids) > 0 && ids[0] != "" {
node := n.children.byID(ids[0])
node := n.children.getByID(ids[0])

if node == nil {
node = newNode(n, ids[0])
Expand All @@ -63,14 +63,14 @@ func (n *node) addChild(ids []string) *node {
return n
}

func (n *node) child(ids []string) (*node, Params) {
func (n *node) getChild(ids []string) (*node, Params) {
if len(ids) == 0 {
return n, make(Params, n.params)
}

child := n.children.byID(ids[0])
child := n.children.getByID(ids[0])
if child != nil {
n, params := child.child(ids[1:])
n, params := child.getChild(ids[1:])

if child.isWildcard && params != nil {
params[child.params-1].Value = ids[0]
Expand All @@ -87,11 +87,11 @@ func (n *node) child(ids []string) (*node, Params) {
return nil, nil
}

// childByPath accepts string path then returns:
// getChildByPath accepts string path then returns:
// child node as a first arg,
// parameters built from wildcards,
// and part of path (this is used to strip request path for sub routers)
func (n *node) childByPath(path string) (*node, Params, string) {
func (n *node) getChildByPath(path string) (*node, Params, string) {
pathLen := len(path)
if pathLen > 0 && path[0] == '/' {
path = path[1:]
Expand All @@ -102,21 +102,21 @@ func (n *node) childByPath(path string) (*node, Params, string) {
return n, make(Params, n.params), ""
}

child, part, path := n.children.byPath(path)
child, part, path := n.children.getByPath(path)

if child != nil {
n, params, _ := child.childByPath(path)
grandChild, params, _ := child.getChildByPath(path)

if part != "" && params != nil {
params[child.params-1].Value = part
params[child.params-1].Key = child.id
}

if n == nil && child.isSubrouter {
return child, params, part
if grandChild == nil && child.isSubrouter {
return child, params, path
}

return n, params, ""
return grandChild, params, ""
}

return nil, nil, ""
Expand Down
2 changes: 1 addition & 1 deletion node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestRegexNode(t *testing.T) {
func TestUnknownNodesChild(t *testing.T) {
n := newRoot("")

node, params := n.child([]string{"a", "b", "c"})
node, params := n.getChild([]string{"a", "b", "c"})

if node != nil || params != nil {
t.Error("Node should return nil values for unknown path")
Expand Down
2 changes: 1 addition & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type route struct {
handler http.Handler
}

func (r *route) chain() http.Handler {
func (r *route) getHandler() http.Handler {
if r.handler != nil {
return r.middleware.handle(r.handler)
}
Expand Down
4 changes: 2 additions & 2 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestRouter(t *testing.T) {
r := newRoute(fn)
r.appendMiddleware(newMiddleware(m1, m2, m3))

h := r.chain()
h := r.getHandler()

w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestInvalidParams(t *testing.T) {

func TestNilHandler(t *testing.T) {
r := newRoute(nil)
if h := r.chain(); h != nil {
if h := r.getHandler(); h != nil {
t.Error("Handler hould be equal nil")
}
}
15 changes: 8 additions & 7 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gorouter

import (
"net/http"
"strings"
)

// HTTP methods constants
Expand Down Expand Up @@ -175,16 +176,16 @@ func (r *router) ServeFiles(root http.FileSystem, path string, strip bool) {
}

func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
root := r.routes.byID(req.Method)
root := r.routes.getByID(req.Method)
if root != nil {
node, params, subPath := root.childByPath(req.URL.Path)
node, params, subPath := root.getChildByPath(req.URL.Path)

if node != nil && node.route != nil {
if h := node.route.chain(); h != nil {
if h := node.route.getHandler(); h != nil {
req = req.WithContext(newContext(req, params))

if subPath != "" {
h = http.StripPrefix("/"+subPath, h)
h = http.StripPrefix(strings.TrimSuffix(req.URL.Path, subPath), h)
}

h.ServeHTTP(w, req)
Expand Down Expand Up @@ -236,7 +237,7 @@ func (r *router) serveNotAllowed(w http.ResponseWriter, req *http.Request) {
}

func (r *router) addRoute(method, path string, f http.Handler) *node {
root := r.routes.byID(method)
root := r.routes.getByID(method)
if root == nil {
root = newRoot(method)
r.routes.insert(root)
Expand Down Expand Up @@ -274,7 +275,7 @@ func (r *router) addMiddleware(method, path string, fs ...MiddlewareFunc) {
// routes tree roots should be http method nodes only
for _, root := range r.routes.statics {
if method == "" || method == root.id {
node, _ := root.child(paths)
node, _ := root.getChild(paths)
if node != nil {
c(c, node, fs)
}
Expand Down Expand Up @@ -302,7 +303,7 @@ func (r *router) allowed(method, path string) (allow string) {
continue
}

n, _, _ := root.childByPath(path)
n, _, _ := root.getChildByPath(path)
if n != nil && n.route != nil {
if len(allow) == 0 {
allow = root.id
Expand Down
2 changes: 1 addition & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func mockServeHTTP(h http.Handler, method, path string) error {
}

func checkIfHasRootRoute(t *testing.T, router *router, method string) {
if rootRoute := router.routes.byID(method); rootRoute == nil {
if rootRoute := router.routes.getByID(method); rootRoute == nil {
t.Error("Route not found")
}
}
Expand Down
4 changes: 2 additions & 2 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (t *tree) insert(n *node) {
}
}

func (t *tree) byID(id string) *node {
func (t *tree) getByID(id string) *node {
if id != "" {
if t.idsLen > 0 {
for i, cID := range t.ids {
Expand All @@ -76,7 +76,7 @@ func (t *tree) byID(id string) *node {
return nil
}

func (t *tree) byPath(path string) (*node, string, string) {
func (t *tree) getByPath(path string) (*node, string, string) {
if len(path) == 0 {
return nil, "", ""
}
Expand Down
4 changes: 2 additions & 2 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestTreeGetRegexNodeById(t *testing.T) {
tree := newTree()
tree.insert(n)

c := tree.byID("rego")
c := tree.getByID("rego")

if c != n {
t.Error("Tree should match regex node by ID")
Expand All @@ -45,7 +45,7 @@ func TestTreeGetRegexNodeById(t *testing.T) {

func TestGetTreeNodeByEmptyPath(t *testing.T) {
tree := newTree()
n, _, _ := tree.byPath("")
n, _, _ := tree.getByPath("")

if n != nil {
t.Error("Tree should return nil node for empty path")
Expand Down

0 comments on commit 38258c0

Please sign in to comment.