tree

package
v5.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

Functions

This section is empty.

Types

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node 表示路由中的节点

func (*Node) Handler

func (n *Node) Handler(method string) http.Handler

Handler 获取指定方法对应的处理函数

func (*Node) Methods

func (n *Node) Methods() []string

Methods 当前节点支持的请求方法

func (*Node) Options

func (n *Node) Options() string

Options 获取当前支持的请求方法列表字符串

type Tree

type Tree 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(lock bool, i *syntax.Interceptors) *Tree

New 声明一个 Tree 实例

func (*Tree) Add

func (tree *Tree) Add(pattern string, h http.Handler, methods ...string) error

Add 添加路由项

methods 可以为空,表示添加除 OPTIONS 和 HEAD 之外所有支持的请求方法。

func (*Tree) Clean

func (tree *Tree) Clean(prefix string)

Clean 清除路由项

func (*Tree) Print

func (tree *Tree) Print(w io.Writer)

Print 向 w 输出树状结构

func (*Tree) Remove

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

Remove 移除路由项

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

func (*Tree) Route

func (tree *Tree) Route(path string) (*Node, *syntax.Params)

Route 找到与当前内容匹配的 Node 实例

NOTE: 调用方需要调用 syntax.Params.Destroy 销毁对象

func (*Tree) Routes

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

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

func (*Tree) URL

func (tree *Tree) 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