This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.go
135 lines (108 loc) · 3.22 KB
/
node.go
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
package trie
import (
"github.com/ronaksoft/rony/internal/gateway"
"sort"
"strings"
)
/*
Creation Time: 2021 - Mar - 02
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
// Node is the trie's node which path patterns with their data like an HTTP handler are saved to.
// See `Trie` too.
type Node struct {
parent *Node
children map[string]*Node
hasDynamicChild bool // does one of the children contains a parameter or wildcard?
childNamedParameter bool // is the child a named parameter (single segmnet)
childWildcardParameter bool // or it is a wildcard (can be more than one path segments) ?
paramKeys []string // the param keys without : or *.
end bool // it is a complete node, here we stop and we can say that the node is valid.
key string // if end == true then key is filled with the original value of the insertion's key.
// if key != "" && its parent has childWildcardParameter == true,
// we need it to track the static part for the closest-wildcard's parameter storage.
staticKey string
// insert main data relative to http and a tag for things like route names.
Proxy gateway.ProxyFactory
Tag string
// other insert data.
Data interface{}
}
// NewNode returns a new, empty, Node.
func NewNode() *Node {
n := new(Node)
return n
}
func (n *Node) addChild(s string, child *Node) {
if n.children == nil {
n.children = make(map[string]*Node)
}
if _, exists := n.children[s]; exists {
return
}
child.parent = n
n.children[s] = child
}
func (n *Node) getChild(s string) *Node {
if n.children == nil {
return nil
}
return n.children[s]
}
func (n *Node) hasChild(s string) bool {
return n.getChild(s) != nil
}
func (n *Node) findClosestParentWildcardNode() *Node {
n = n.parent
for n != nil {
if n.childWildcardParameter {
return n.getChild(WildcardParamStart)
}
n = n.parent
}
return nil
}
// NodeKeysSorter is the type definition for the sorting logic
// that caller can pass on `GetKeys` and `Autocomplete`.
type NodeKeysSorter = func(list []string) func(i, j int) bool
// DefaultKeysSorter sorts as: first the "key (the path)" with the lowest number of slashes.
var DefaultKeysSorter = func(list []string) func(i, j int) bool {
return func(i, j int) bool {
return len(strings.Split(list[i], pathSep)) < len(strings.Split(list[j], pathSep))
}
}
// Keys returns this node's key (if it's a final path segment)
// and its children's node's key. The "sorter" can be optionally used to sort the result.
func (n *Node) Keys(sorter NodeKeysSorter) (list []string) {
if n == nil {
return
}
if n.end {
list = append(list, n.key)
}
if n.children != nil {
for _, child := range n.children {
list = append(list, child.Keys(sorter)...)
}
}
if sorter != nil {
sort.Slice(list, sorter(list))
}
return
}
// Parent returns the parent of that node, can return nil if this is the root node.
func (n *Node) Parent() *Node {
return n.parent
}
// String returns the key, which is the path pattern for the HTTP Mux.
func (n *Node) String() string {
return n.key
}
// IsEnd returns true if this Node is a final path, has a key.
func (n *Node) IsEnd() bool {
return n.end
}