File size: 2,448 Bytes
530729e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
// Copyright 2019 GoAdmin Core Team. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.
package context
import "fmt"
type node struct {
children []*node
value string
method []string
handle [][]Handler
}
func tree() *node {
return &node{
children: make([]*node, 0),
value: "/",
handle: nil,
}
}
func (n *node) hasMethod(method string) int {
for k, m := range n.method {
if m == method {
return k
}
}
return -1
}
func (n *node) addMethodAndHandler(method string, handler []Handler) {
n.method = append(n.method, method)
n.handle = append(n.handle, handler)
}
func (n *node) addChild(child *node) {
n.children = append(n.children, child)
}
func (n *node) addContent(value string) *node {
var child = n.search(value)
if child == nil {
child = &node{
children: make([]*node, 0),
value: value,
}
n.addChild(child)
}
return child
}
func (n *node) search(value string) *node {
for _, child := range n.children {
if child.value == value || child.value == "*" {
return child
}
}
return nil
}
func (n *node) addPath(paths []string, method string, handler []Handler) {
child := n
for i := 0; i < len(paths); i++ {
child = child.addContent(paths[i])
}
child.addMethodAndHandler(method, handler)
}
func (n *node) findPath(paths []string, method string) []Handler {
child := n
for i := 0; i < len(paths); i++ {
child = child.search(paths[i])
if child == nil {
return nil
}
}
methodIndex := child.hasMethod(method)
if methodIndex == -1 {
return nil
}
return child.handle[methodIndex]
}
func (n *node) print() {
fmt.Println(n)
}
func (n *node) printChildren() {
n.print()
for _, child := range n.children {
child.printChildren()
}
}
func stringToArr(path string) []string {
var (
paths = make([]string, 0)
start = 0
end int
isWildcard = false
)
for i := 0; i < len(path); i++ {
if i == 0 && path[0] == '/' {
start = 1
continue
}
if path[i] == ':' {
isWildcard = true
}
if i == len(path)-1 {
end = i + 1
if isWildcard {
paths = append(paths, "*")
} else {
paths = append(paths, path[start:end])
}
}
if path[i] == '/' {
end = i
if isWildcard {
paths = append(paths, "*")
} else {
paths = append(paths, path[start:end])
}
start = i + 1
isWildcard = false
}
}
return paths
}
|