1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 13:30:56 +00:00

golint tree.go

This commit is contained in:
astaxie 2015-09-08 23:49:24 +08:00
parent bcb1db256d
commit 62e528ca4c

31
tree.go
View File

@ -22,6 +22,10 @@ import (
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
) )
// Tree has three elements: FixRouter/wildcard/leaves
// fixRouter sotres Fixed Router
// wildcard stores params
// leaves store the endpoint information
type Tree struct { type Tree struct {
//search fix route first //search fix route first
fixrouters map[string]*Tree fixrouters map[string]*Tree
@ -33,13 +37,14 @@ type Tree struct {
leaves []*leafInfo leaves []*leafInfo
} }
// NewTree return a new Tree
func NewTree() *Tree { func NewTree() *Tree {
return &Tree{ return &Tree{
fixrouters: make(map[string]*Tree), fixrouters: make(map[string]*Tree),
} }
} }
// add Tree to the exist Tree // AddTree will add tree to the exist Tree
// prefix should has no params // prefix should has no params
func (t *Tree) AddTree(prefix string, tree *Tree) { func (t *Tree) AddTree(prefix string, tree *Tree) {
t.addtree(splitPath(prefix), tree, nil, "") t.addtree(splitPath(prefix), tree, nil, "")
@ -187,7 +192,7 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) {
} }
} }
// call addseg function // AddRouter call addseg function
func (t *Tree) AddRouter(pattern string, runObject interface{}) { func (t *Tree) AddRouter(pattern string, runObject interface{}) {
t.addseg(splitPath(pattern), runObject, nil, "") t.addseg(splitPath(pattern), runObject, nil, "")
} }
@ -266,7 +271,7 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string,
} }
} }
// match router to runObject & params // Match router to runObject & params
func (t *Tree) Match(pattern string) (runObject interface{}, params map[string]string) { func (t *Tree) Match(pattern string) (runObject interface{}, params map[string]string) {
if len(pattern) == 0 || pattern[0] != '/' { if len(pattern) == 0 || pattern[0] != '/' {
return nil, nil return nil, nil
@ -349,7 +354,7 @@ func (leaf *leafInfo) match(wildcardValues []string) (ok bool, params map[string
continue continue
} }
params[v] = "" params[v] = ""
j += 1 j++
} }
return true, params return true, params
} }
@ -402,7 +407,7 @@ func (leaf *leafInfo) match(wildcardValues []string) (ok bool, params map[string
return false, nil return false, nil
} }
params[v] = wildcardValues[j] params[v] = wildcardValues[j]
j += 1 j++
} }
if len(params) != len(wildcardValues) { if len(params) != len(wildcardValues) {
return false, nil return false, nil
@ -453,9 +458,8 @@ func splitSegment(key string) (bool, []string, string) {
if strings.HasPrefix(key, "*") { if strings.HasPrefix(key, "*") {
if key == "*.*" { if key == "*.*" {
return true, []string{".", ":path", ":ext"}, "" return true, []string{".", ":path", ":ext"}, ""
} else {
return true, []string{":splat"}, ""
} }
return true, []string{":splat"}, ""
} }
if strings.ContainsAny(key, ":") { if strings.ContainsAny(key, ":") {
var paramsNum int var paramsNum int
@ -469,7 +473,7 @@ func splitSegment(key string) (bool, []string, string) {
reg := regexp.MustCompile(`[a-zA-Z0-9_]+`) reg := regexp.MustCompile(`[a-zA-Z0-9_]+`)
for i, v := range key { for i, v := range key {
if skipnum > 0 { if skipnum > 0 {
skipnum -= 1 skipnum--
continue continue
} }
if start { if start {
@ -483,7 +487,7 @@ func splitSegment(key string) (bool, []string, string) {
startexp = false startexp = false
skipnum = 3 skipnum = 3
param = make([]rune, 0) param = make([]rune, 0)
paramsNum += 1 paramsNum++
continue continue
} }
} }
@ -491,7 +495,7 @@ func splitSegment(key string) (bool, []string, string) {
if key[i+1:i+7] == "string" { if key[i+1:i+7] == "string" {
out = append(out, []rune(`([\w]+)`)...) out = append(out, []rune(`([\w]+)`)...)
params = append(params, ":"+string(param)) params = append(params, ":"+string(param))
paramsNum += 1 paramsNum++
start = false start = false
startexp = false startexp = false
skipnum = 6 skipnum = 6
@ -509,7 +513,7 @@ func splitSegment(key string) (bool, []string, string) {
out = append(out, []rune(`(.+)`)...) out = append(out, []rune(`(.+)`)...)
params = append(params, ":"+string(param)) params = append(params, ":"+string(param))
param = make([]rune, 0) param = make([]rune, 0)
paramsNum += 1 paramsNum++
start = false start = false
startexp = false startexp = false
} }
@ -527,7 +531,7 @@ func splitSegment(key string) (bool, []string, string) {
startexp = true startexp = true
start = false start = false
params = append(params, ":"+string(param)) params = append(params, ":"+string(param))
paramsNum += 1 paramsNum++
expt = make([]rune, 0) expt = make([]rune, 0)
expt = append(expt, '(') expt = append(expt, '(')
} else if v == ')' { } else if v == ')' {
@ -548,7 +552,6 @@ func splitSegment(key string) (bool, []string, string) {
params = append(params, ":"+string(param)) params = append(params, ":"+string(param))
} }
return true, params, string(out) return true, params, string(out)
} else {
return false, nil, ""
} }
return false, nil, ""
} }