Update naming, reduce number of files
This commit is contained in:
parent
cfdd687bb7
commit
20089847c2
9
flow.go
9
flow.go
|
@ -1,9 +0,0 @@
|
||||||
package router
|
|
||||||
|
|
||||||
type Flow int
|
|
||||||
|
|
||||||
const (
|
|
||||||
flowStop Flow = iota
|
|
||||||
flowBegin
|
|
||||||
flowNext
|
|
||||||
)
|
|
|
@ -10,12 +10,12 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// A node on our radix tree
|
// A node on our radix tree
|
||||||
type TreeNode[T any] struct {
|
type Node[T any] struct {
|
||||||
prefix string
|
prefix string
|
||||||
data T
|
data T
|
||||||
children []*TreeNode[T]
|
children []*Node[T]
|
||||||
parameter *TreeNode[T]
|
parameter *Node[T]
|
||||||
wildcard *TreeNode[T]
|
wildcard *Node[T]
|
||||||
indexes []uint8
|
indexes []uint8
|
||||||
start uint8
|
start uint8
|
||||||
end uint8
|
end uint8
|
||||||
|
@ -26,7 +26,7 @@ type TreeNode[T any] struct {
|
||||||
// node with the given path and data. If path is empty, it will
|
// node with the given path and data. If path is empty, it will
|
||||||
// not create another child node and instead assign the data
|
// not create another child node and instead assign the data
|
||||||
// directly to the node.
|
// directly to the node.
|
||||||
func (node *TreeNode[T]) split(index int, path string, data T) {
|
func (node *Node[T]) split(index int, path string, data T) {
|
||||||
// Create split node with the remaining string
|
// Create split node with the remaining string
|
||||||
splitNode := node.clone(node.prefix[index:])
|
splitNode := node.clone(node.prefix[index:])
|
||||||
|
|
||||||
|
@ -48,8 +48,8 @@ func (node *TreeNode[T]) split(index int, path string, data T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone the node with a new prefix
|
// Clone the node with a new prefix
|
||||||
func (node *TreeNode[T]) clone(prefix string) *TreeNode[T] {
|
func (node *Node[T]) clone(prefix string) *Node[T] {
|
||||||
return &TreeNode[T]{
|
return &Node[T]{
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
data: node.data,
|
data: node.data,
|
||||||
children: node.children,
|
children: node.children,
|
||||||
|
@ -63,7 +63,7 @@ func (node *TreeNode[T]) clone(prefix string) *TreeNode[T] {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the node, set the prefix
|
// Reset the node, set the prefix
|
||||||
func (node *TreeNode[T]) reset(prefix string) {
|
func (node *Node[T]) reset(prefix string) {
|
||||||
var empty T
|
var empty T
|
||||||
node.prefix = prefix
|
node.prefix = prefix
|
||||||
node.data = empty
|
node.data = empty
|
||||||
|
@ -77,7 +77,7 @@ func (node *TreeNode[T]) reset(prefix string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append the given path to the tree
|
// Append the given path to the tree
|
||||||
func (node *TreeNode[T]) append(path string, data T) {
|
func (node *Node[T]) append(path string, data T) {
|
||||||
// At this point, all we know is that somewhere
|
// At this point, all we know is that somewhere
|
||||||
// in the remaining string we have parameters.
|
// in the remaining string we have parameters.
|
||||||
// node: /user|
|
// node: /user|
|
||||||
|
@ -106,7 +106,7 @@ func (node *TreeNode[T]) append(path string, data T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
child := &TreeNode[T]{
|
child := &Node[T]{
|
||||||
prefix: path,
|
prefix: path,
|
||||||
data: data,
|
data: data,
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ func (node *TreeNode[T]) append(path string, data T) {
|
||||||
paramEnd = len(path)
|
paramEnd = len(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
child := &TreeNode[T]{
|
child := &Node[T]{
|
||||||
prefix: path[1:paramEnd],
|
prefix: path[1:paramEnd],
|
||||||
kind: path[paramStart],
|
kind: path[paramStart],
|
||||||
}
|
}
|
||||||
|
@ -156,7 +156,7 @@ func (node *TreeNode[T]) append(path string, data T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a normal node with the path before the parameter start.
|
// Add a normal node with the path before the parameter start.
|
||||||
child := &TreeNode[T]{
|
child := &Node[T]{
|
||||||
prefix: path[:paramStart],
|
prefix: path[:paramStart],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ func (node *TreeNode[T]) append(path string, data T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a child tree node
|
// Add a child tree node
|
||||||
func (node *TreeNode[T]) addChild(child *TreeNode[T]) {
|
func (node *Node[T]) addChild(child *Node[T]) {
|
||||||
if len(node.children) == 0 {
|
if len(node.children) == 0 {
|
||||||
node.children = append(node.children, nil)
|
node.children = append(node.children, nil)
|
||||||
}
|
}
|
||||||
|
@ -213,19 +213,19 @@ func (node *TreeNode[T]) addChild(child *TreeNode[T]) {
|
||||||
node.children[index] = child
|
node.children[index] = child
|
||||||
}
|
}
|
||||||
|
|
||||||
func (node *TreeNode[T]) addTrailingSlash(data T) {
|
func (node *Node[T]) addTrailingSlash(data T) {
|
||||||
if strings.HasSuffix(node.prefix, "/") || node.kind == wildcard || (separator >= node.start && separator < node.end && node.indexes[separator-node.start] != 0) {
|
if strings.HasSuffix(node.prefix, "/") || node.kind == wildcard || (separator >= node.start && separator < node.end && node.indexes[separator-node.start] != 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
node.addChild(&TreeNode[T]{
|
node.addChild(&Node[T]{
|
||||||
prefix: "/",
|
prefix: "/",
|
||||||
data: data,
|
data: data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Traverses the tree and calls the given function on every node.
|
// Traverses the tree and calls the given function on every node.
|
||||||
func (node *TreeNode[T]) each(callback func(*TreeNode[T])) {
|
func (node *Node[T]) each(callback func(*Node[T])) {
|
||||||
callback(node)
|
callback(node)
|
||||||
|
|
||||||
for _, child := range node.children {
|
for _, child := range node.children {
|
||||||
|
@ -247,7 +247,7 @@ func (node *TreeNode[T]) each(callback func(*TreeNode[T])) {
|
||||||
|
|
||||||
// Called when the node was fully parsed and needs to decide the next control flow.
|
// Called when the node was fully parsed and needs to decide the next control flow.
|
||||||
// finish is only called from `tree.Add`.
|
// finish is only called from `tree.Add`.
|
||||||
func (node *TreeNode[T]) finish(path string, data T, i int, offset int) (*TreeNode[T], int, Flow) {
|
func (node *Node[T]) finish(path string, data T, i int, offset int) (*Node[T], int, Flow) {
|
||||||
char := path[i]
|
char := path[i]
|
||||||
|
|
||||||
if char >= node.start && char < node.end {
|
if char >= node.start && char < node.end {
|
|
@ -1,6 +0,0 @@
|
||||||
package router
|
|
||||||
|
|
||||||
type Parameter struct {
|
|
||||||
Key string
|
|
||||||
Value string
|
|
||||||
}
|
|
13
router.go
13
router.go
|
@ -1,5 +1,18 @@
|
||||||
package router
|
package router
|
||||||
|
|
||||||
|
type Flow int
|
||||||
|
|
||||||
|
const (
|
||||||
|
flowStop Flow = iota
|
||||||
|
flowBegin
|
||||||
|
flowNext
|
||||||
|
)
|
||||||
|
|
||||||
|
type Parameter struct {
|
||||||
|
Key string
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
type Router[T any] struct {
|
type Router[T any] struct {
|
||||||
get Tree[T]
|
get Tree[T]
|
||||||
post Tree[T]
|
post Tree[T]
|
||||||
|
|
6
tree.go
6
tree.go
|
@ -2,7 +2,7 @@ package router
|
||||||
|
|
||||||
// Super-fancy radix tree
|
// Super-fancy radix tree
|
||||||
type Tree[T any] struct {
|
type Tree[T any] struct {
|
||||||
root TreeNode[T]
|
root Node[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a new element to the tree
|
// Adds a new element to the tree
|
||||||
|
@ -94,7 +94,7 @@ func (tree *Tree[T]) LookupNoAlloc(path string, addParameter func(key string, va
|
||||||
var (
|
var (
|
||||||
i uint
|
i uint
|
||||||
wildcardPath string
|
wildcardPath string
|
||||||
wildcard *TreeNode[T]
|
wildcard *Node[T]
|
||||||
node = &tree.root
|
node = &tree.root
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -189,7 +189,7 @@ notFound:
|
||||||
|
|
||||||
// Binds all handlers to a new one provided by the callback.
|
// Binds all handlers to a new one provided by the callback.
|
||||||
func (tree *Tree[T]) Map(transform func(T) T) {
|
func (tree *Tree[T]) Map(transform func(T) T) {
|
||||||
tree.root.each(func(node *TreeNode[T]) {
|
tree.root.each(func(node *Node[T]) {
|
||||||
node.data = transform(node.data)
|
node.data = transform(node.data)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user