syntax

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: 11 Imported by: 0

Documentation

Overview

Package syntax 负责处理路由语法

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchAny

func MatchAny(path string) bool

MatchAny 匹配任意非空内容

func MatchDigit

func MatchDigit(path string) bool

MatchDigit 匹配数值字符

与正则表达式中的 [0-9]+ 是相同的。

func MatchWord

func MatchWord(path string) bool

MatchWord 匹配单词

与正则表达式中的 [a-zA-Z0-9]+ 是相同的。

func WithValue

func WithValue(r *http.Request, ps *Params) *http.Request

WithValue 将参数 ps 附加在 r 上

与 context.WithValue 功能相同,但是考虑了在同一个 r 上调用多次 WithValue 的情况。

Types

type InterceptorFunc

type InterceptorFunc func(string) bool

InterceptorFunc 拦截器的处理函数

type Interceptors

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

func NewInterceptors

func NewInterceptors() *Interceptors

func (*Interceptors) Add

func (i *Interceptors) Add(f InterceptorFunc, name ...string)

func (*Interceptors) NewSegment

func (i *Interceptors) NewSegment(val string) (*Segment, error)

NewSegment 声明新的 Segment 变量

如果为非字符串类型的内容,应该是以 { 符号开头才是合法的。

func (*Interceptors) Split

func (i *Interceptors) Split(str string) ([]*Segment, error)

Split 将字符串解析成 Segment 数组

以 { 为分界线进行分割。比如

/posts/{id}/email ==> /posts/, {id}/email
/posts/\{{id}/email ==> /posts/{, {id}/email
/posts/{year}/{id}.html ==> /posts/, {year}/, {id}.html

func (*Interceptors) URL

func (i *Interceptors) URL(buf *errwrap.StringBuilder, pattern string, ps map[string]string) error

URL 将 ps 中的参数填入 pattern

如果 pattern 中存在,但是不存在于 ps,将出错, 但是如果只存在于 ps,但是不存在于 pattern 是可以的。

不能将 URL 作为判断 pattern 是否合规的方法,在 ps 为空时, 将直接返回 pattern。

type Param

type Param struct {
	K, V string // 如果 K 为空,则表示该参数已经被删除。
}

type Params

type Params struct {
	Path   string  // 这是在 Segment.Match 中用到的路径信息。
	Params []Param // 实际需要传递的参数
}

Params 路由参数

实现了 params.Params 接口

func GetParams

func GetParams(r *http.Request) *Params

GetParams 获取当前请求实例上的参数列表

func NewParams

func NewParams(path string) *Params

func (*Params) Bool

func (p *Params) Bool(key string) (bool, error)

func (*Params) Clone

func (p *Params) Clone() params.Params

func (*Params) Count

func (p *Params) Count() (cnt int)

func (*Params) Delete

func (p *Params) Delete(k string)

func (*Params) Destroy

func (p *Params) Destroy()

func (*Params) Exists

func (p *Params) Exists(key string) bool

func (*Params) Float

func (p *Params) Float(key string) (float64, error)

func (*Params) Get

func (p *Params) Get(key string) (string, bool)

func (*Params) Int

func (p *Params) Int(key string) (int64, error)

func (*Params) Map

func (p *Params) Map() map[string]string

func (*Params) MustBool

func (p *Params) MustBool(key string, def bool) bool

func (*Params) MustFloat

func (p *Params) MustFloat(key string, def float64) float64

func (*Params) MustInt

func (p *Params) MustInt(key string, def int64) int64

func (*Params) MustString

func (p *Params) MustString(key, def string) string

func (*Params) MustUint

func (p *Params) MustUint(key string, def uint64) uint64

func (*Params) Set

func (p *Params) Set(k, v string)

func (*Params) String

func (p *Params) String(key string) (string, error)

func (*Params) Uint

func (p *Params) Uint(key string) (uint64, error)

type Segment

type Segment struct {
	// 节点实际内容被拆分成以下几个部分,其组成方式如下:
	//  Value = {Name:rule}Suffix
	// 其中 Name、rule 和 Suffix 可能为空,但是 Name 和 rule 不能同时为空。
	Value string // 节点上的原始内容
	Name  string // 当前节点的参数名称,适用非字符节点。

	Suffix string // 保存参数名之后的字符串,比如 "{id}/author" 此值为 "/author",非 endpoint 的拦截器和命名节点不能为空。。

	// 节点类型
	Type Type

	// 是否为最终结点
	//
	// 在命名和拦截类型的路由项中,如果以 {path} 等结尾,则表示可以匹配任意剩余的字符。
	// 此值表示当前节点是否为此种类型。该类型的节点在匹配时,优先级可能会比较低。
	Endpoint bool
	// contains filtered or unexported fields
}

Segment 路由项被拆分之后的分段内容

func (*Segment) AmbiguousLen

func (seg *Segment) AmbiguousLen() int16

func (*Segment) IsAmbiguous

func (seg *Segment) IsAmbiguous(s2 *Segment) bool

IsAmbiguous 判断两个节点是否存在歧义

即除了名称,其它都相同,如果两个节点符合此条件, 在相同路径下是无法判断到底要选哪一条路径的,应该避免此类节节点出现在同一路径上。

func (*Segment) Match

func (seg *Segment) Match(p *Params) bool

Match 路径是否与当前节点匹配

如果正确匹配,则将剩余的未匹配字符串写入到 p.Path 并返回 true。

func (*Segment) Similarity

func (seg *Segment) Similarity(s1 *Segment) int

Similarity 与 s1 的相似度,-1 表示完全相同, 其它大于等于零的值,越大,表示相似度越高。

func (*Segment) Split

func (seg *Segment) Split(i *Interceptors, pos int) ([]*Segment, error)

Split 从 pos 位置拆分为两个

pos 位置的字符归属于后一个元素。

func (*Segment) Valid added in v5.1.0

func (seg *Segment) Valid(pattern string) bool

Valid 验证 pattern 是否与当前节点匹配

type Type

type Type int8

Type 路由项节点的类型

const (
	// String 普通的字符串类型,逐字匹配,比如
	//  /users/1
	// 只能匹配 /users/1,不能匹配 /users/2
	String Type = iota

	// Interceptor 拦截器
	//
	// 这是正则和命名参数的特例,其优先级比两都都要高。
	Interceptor

	// Regexp 正则表达式,比如:
	//  {id:\\d+}/abc
	// 可以匹配 /users/1、/users/2 等任意数值。
	Regexp

	// Named 命名参数,相对于正则,其效率更高,当然也没有正则灵活。比如:
	//  {id}/abc
	// 可以匹配 /users/1、/users/2 和 /users/username 等非数值类型
	Named
)

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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