syntax

package
v5.0.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package syntax 负责处理路由语法

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func URL

func URL(pattern string, params map[string]string) (string, error)

URL 将 params 中的参数填入 pattern

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

Types

type Segment

type Segment struct {
	Value string
	Type  Type

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

	// 当前节点的参数名称,比如 "{id}/author",
	// 则此值为 "id",非字符串节点有用。
	Name string

	// 保存参数名之后的字符串,比如 "{id}/author" 此值为 "/author",
	// 仅对非字符串节点有效果,若 endpoint 为 false,则此值也不空。
	Suffix string
	// contains filtered or unexported fields
}

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

func NewSegment

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

NewSegment 声明新的 Segment 变量

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

func Split

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

Split 将字符串解析成字符串数组

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

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

func (*Segment) Match

func (seg *Segment) Match(path string, params params.Params) int

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

如果正确匹配,则返回 path 剩余部分的起始位置。 params 表示匹配完成之后,从地址中获取的参数值。

func (*Segment) Similarity

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

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

func (*Segment) Split

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

Split 从 pos 位置拆分为两个

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

type Type

type Type int8

Type 路由项的类型

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

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

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

	// Named 命名参数,相对于正则,其效率更高,当然也没有正则灵活。比如:
	//  /users/{id}
	// 可以匹配 /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