tree

package
v7.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package tree 提供了以树形结构保存路由项的相关操作

Index

Constants

This section is empty.

Variables

Functions

func ApplyMiddleware

func ApplyMiddleware[T any](h T, f ...types.MiddlewareOf[T]) T

func BuildTestMiddleware

func BuildTestMiddleware(a *assert.Assertion, text string) types.MiddlewareOf[http.Handler]

func BuildTestNodeHandlerFunc

func BuildTestNodeHandlerFunc(status int) types.BuildNodeHandleOf[http.Handler]

Types

type Tree

type Tree[T any] struct {
	// contains filtered or unexported fields
}

Tree 以树节点的形式保存的路由

多段路由项,会提取其中的相同的内容组成树状结构的节点。比如以下路由项:

/posts/{id}/author
/posts/{id}/author/emails
/posts/{id}/author/profile
/posts/1/author

会被转换成以下结构

/posts
   |
   +---- 1/author
   |
   +---- {id}/author/
             |
             +---- profile
             |
             +---- emails

func New

func New[T any](lock bool, i *syntax.Interceptors, notFound T, methodNotAllowedBuilder, optionsBuilder types.BuildNodeHandleOf[T]) *Tree[T]

func NewTestTree

func NewTestTree(a *assert.Assertion, lock bool, i *syntax.Interceptors) *Tree[http.Handler]

NewTestTree 返回以 http.Handler 作为参数实例化的 Tree

func (*Tree[T]) Add

func (tree *Tree[T]) Add(pattern string, h T, methods ...string) error

Add 添加路由项

methods 可以为空,表示添所有支持的请求方法,其中的 HEAD 和 OPTIONS 不受控。

func (*Tree[T]) ApplyMiddleware

func (tree *Tree[T]) ApplyMiddleware(ms ...types.MiddlewareOf[T])

func (*Tree[T]) Clean

func (tree *Tree[T]) Clean(prefix string)

Clean 清除路由项

func (*Tree[T]) Find

func (tree *Tree[T]) Find(pattern string) *node[T]

Find 查找匹配的节点

func (*Tree[T]) Handler

func (tree *Tree[T]) Handler(p *types.Context, method string) (types.Node, T, bool)

Handler 查找与参数匹配的处理对象

如果未找到,也会返回相应在的处理对象,比如 tree.notFound 或是相应的 methodNotAllowed 方法。

func (*Tree[T]) Print

func (tree *Tree[T]) Print(w io.Writer)

Print 向 w 输出树状结构

func (*Tree[T]) Remove

func (tree *Tree[T]) Remove(pattern string, methods ...string)

Remove 移除路由项

methods 可以为空,表示删除所有内容。单独删除 OPTIONS,将不会发生任何事情。

func (*Tree[T]) Routes

func (tree *Tree[T]) Routes() map[string][]string

Routes 获取当前的所有路由项以及对应的请求方法

func (*Tree[T]) URL

func (tree *Tree[T]) URL(buf *errwrap.StringBuilder, pattern string, ps map[string]string) error

URL 将 ps 填入 pattern 生成 URL

NOTE: 会检测 pattern 是否存在于 tree 中。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL