tree

package
v9.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	// Methods 支持的请求方法
	Methods = []string{
		http.MethodGet,
		http.MethodPost,
		http.MethodDelete,
		http.MethodPut,
		http.MethodPatch,
		http.MethodConnect,
		http.MethodTrace,
		http.MethodHead,
		http.MethodOptions,
	}

	AnyMethods = Methods[:len(Methods)-3] // 添加请求方法时,所采用的默认值。

)

Functions

func ApplyMiddleware

func ApplyMiddleware[T any](h T, method, pattern, router string, f ...types.Middleware[T]) T

func BuildTestMiddleware

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

func BuildTestNodeHandlerFunc

func BuildTestNodeHandlerFunc(status int) types.BuildNodeHandler[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](
	name string,
	lock bool,
	i *syntax.Interceptors,
	notFound T,
	trace any,
	methodNotAllowedBuilder, optionsBuilder types.BuildNodeHandler[T],
) *Tree[T]

func NewTestTree

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

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

func (*Tree[T]) Add

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

Add 添加路由项

methods 可以为空,表示采用 AnyMethods 中的值。

func (*Tree[T]) ApplyMiddleware

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

ApplyMiddleware 为已有的路由项添加中间件

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(ctx *types.Context, method string) (types.Node, T, bool)

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

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

func (*Tree[T]) Name

func (tree *Tree[T]) Name() string

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