diff --git a/flow.go b/flow.go deleted file mode 100644 index a45d140..0000000 --- a/flow.go +++ /dev/null @@ -1,9 +0,0 @@ -package router - -type Flow int - -const ( - flowStop Flow = iota - flowBegin - flowNext -) diff --git a/treeNode.go b/node.go similarity index 88% rename from treeNode.go rename to node.go index 1eb269b..5aab30a 100644 --- a/treeNode.go +++ b/node.go @@ -10,12 +10,12 @@ const ( ) // A node on our radix tree -type TreeNode[T any] struct { +type Node[T any] struct { prefix string data T - children []*TreeNode[T] - parameter *TreeNode[T] - wildcard *TreeNode[T] + children []*Node[T] + parameter *Node[T] + wildcard *Node[T] indexes []uint8 start 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 // not create another child node and instead assign the data // 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 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 -func (node *TreeNode[T]) clone(prefix string) *TreeNode[T] { - return &TreeNode[T]{ +func (node *Node[T]) clone(prefix string) *Node[T] { + return &Node[T]{ prefix: prefix, data: node.data, children: node.children, @@ -63,7 +63,7 @@ func (node *TreeNode[T]) clone(prefix string) *TreeNode[T] { } // Reset the node, set the prefix -func (node *TreeNode[T]) reset(prefix string) { +func (node *Node[T]) reset(prefix string) { var empty T node.prefix = prefix node.data = empty @@ -77,7 +77,7 @@ func (node *TreeNode[T]) reset(prefix string) { } // 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 // in the remaining string we have parameters. // node: /user| @@ -106,7 +106,7 @@ func (node *TreeNode[T]) append(path string, data T) { return } - child := &TreeNode[T]{ + child := &Node[T]{ prefix: path, data: data, } @@ -125,7 +125,7 @@ func (node *TreeNode[T]) append(path string, data T) { paramEnd = len(path) } - child := &TreeNode[T]{ + child := &Node[T]{ prefix: path[1:paramEnd], 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. - child := &TreeNode[T]{ + child := &Node[T]{ prefix: path[:paramStart], } @@ -173,7 +173,7 @@ func (node *TreeNode[T]) append(path string, data T) { } // 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 { node.children = append(node.children, nil) } @@ -213,19 +213,19 @@ func (node *TreeNode[T]) addChild(child *TreeNode[T]) { 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) { return } - node.addChild(&TreeNode[T]{ + node.addChild(&Node[T]{ prefix: "/", data: data, }) } // 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) 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. // 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] if char >= node.start && char < node.end { diff --git a/parameter.go b/parameter.go deleted file mode 100644 index 2de46d6..0000000 --- a/parameter.go +++ /dev/null @@ -1,6 +0,0 @@ -package router - -type Parameter struct { - Key string - Value string -} diff --git a/router.go b/router.go index 839fade..c834fe4 100644 --- a/router.go +++ b/router.go @@ -1,5 +1,18 @@ package router +type Flow int + +const ( + flowStop Flow = iota + flowBegin + flowNext +) + +type Parameter struct { + Key string + Value string +} + type Router[T any] struct { get Tree[T] post Tree[T] diff --git a/tree.go b/tree.go index bd1d438..cb017d8 100644 --- a/tree.go +++ b/tree.go @@ -2,7 +2,7 @@ package router // Super-fancy radix tree type Tree[T any] struct { - root TreeNode[T] + root Node[T] } // Adds a new element to the tree @@ -94,7 +94,7 @@ func (tree *Tree[T]) LookupNoAlloc(path string, addParameter func(key string, va var ( i uint wildcardPath string - wildcard *TreeNode[T] + wildcard *Node[T] node = &tree.root ) @@ -189,7 +189,7 @@ notFound: // Binds all handlers to a new one provided by the callback. 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) }) }