pathschema

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PathParamPrefix          = ":" // 路径参数起始字符
	PathSeparator            = "/" // 路径分隔符
	OptionalQueryParamPrefix = "?" // 查询参数起始字符,也是路径参数结束字符
)

Variables

View Source
var LowerCaseBackslash = NewComposition(&LowerCase{}, &Backslash{})

LowerCaseBackslash 全小写段路由

View Source
var LowerCaseDash = NewComposition(&LowerCase{}, &UnixDash{})

LowerCaseDash 全小写-短横线

Functions

func Format

func Format(prefix string, relativePath string, schema RoutePathSchema) string

Format 按照方案格式化并组合路由

func LowercaseFirstLetter

func LowercaseFirstLetter(s string) string

LowercaseFirstLetter 将一个字符串的首字母转换为小写形式

func SplitWords

func SplitWords(s string) []string

SplitWords 将字符串s按照单词进行切分, 判断单词的依据为:是否首字母大写 如果输入s无法切分,则返回只有s构成的一个元素的数组 如果输入s包含数字,下划线等,则其包含在前面的单词结尾

func UppercaseFirstLetter

func UppercaseFirstLetter(s string) string

UppercaseFirstLetter 将一个字符串的首字母转换为大写形式

Types

type AddPrefix

type AddPrefix struct {
	Prefix string
}

AddPrefix 用于在分段路由基础上添加一个前缀字符,作用于每一段路径,通常与其他方案组合使用

func (AddPrefix) Connector

func (s AddPrefix) Connector() string

func (AddPrefix) Name

func (s AddPrefix) Name() string

func (AddPrefix) Split

func (s AddPrefix) Split(relativePath string) []string

type AddSuffix

type AddSuffix struct {
	Suffix string
}

AddSuffix 用于在分段路由基础上添加一个后缀字符,作用于每一段路径,通常与其他方案组合使用

func (AddSuffix) Connector

func (s AddSuffix) Connector() string

func (AddSuffix) Name

func (s AddSuffix) Name() string

func (AddSuffix) Split

func (s AddSuffix) Split(relativePath string) []string

type Backslash

type Backslash struct{}

Backslash 反斜杠 按单词分段,每一个单词都作为一个路由段

# example

GetClipboardContent()	=> /Clipboard/Content
ClipSettingsPost()		=> /Clip/Settings
QueryTextHistoryGet()	=> /Query/Text/History

func (Backslash) Connector

func (s Backslash) Connector() string

func (Backslash) Name

func (s Backslash) Name() string

func (Backslash) Split

func (s Backslash) Split(relativePath string) []string

type Composition

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

Composition 组合式路由格式化方案, 通过按顺序执行多个 RoutePathSchema 获得最终路由 此方案会将多个 RoutePathSchema.Connector 拼接成一个唯一的 Connector 在执行 Split 时, 具体步骤为:

  1. 将 relativePath 代入第一个 RoutePathSchema,得到 Split 后的字符串数组 spans,

  2. 将 spans 的元素作为相对路由代入之后的 RoutePathSchema.Split, 得到临时字符串数组 ss, 将 ss 内部的元素拼接起来(直接+连接)代入下一个 RoutePathSchema.Split, 直到执行完全部的 RoutePathSchema.Split, 把最后的字符串数组拼接起来得到 S1

  3. 继续迭代 spans 的元素, 重复步骤2得到 Sx

  4. Format 方法会将 S1 ~ Sx 以 Connector 为连接符组合起来得到最终的路由

如果格式化方案为空,则效果等同于 Original

func NewComposition

func NewComposition(schemas ...RoutePathSchema) *Composition

func (*Composition) Connector

func (s *Composition) Connector() string

func (*Composition) Name

func (s *Composition) Name() string

func (*Composition) Split

func (s *Composition) Split(relativePath string) []string

type Dash

type Dash UnixDash

Dash 短横线

type LowerCamelCase

type LowerCamelCase struct{}

LowerCamelCase 小驼峰 将结构体名称按单词分割后转换为小驼峰的形式后作为相对路由

# example

GetClipboardContent()	=> /clipboardContent
ClipSettingsPost()		=> /clipSettings
QueryTextHistoryGet()	=> /queryTextHistory

func (LowerCamelCase) Connector

func (s LowerCamelCase) Connector() string

func (LowerCamelCase) Name

func (s LowerCamelCase) Name() string

func (LowerCamelCase) Split

func (s LowerCamelCase) Split(relativePath string) []string

type LowerCase

type LowerCase struct{}

LowerCase 全小写字符 将方法名按单词分割后全部转换为小写字符再直接拼接起来

# example

GetClipboardContent()	=> /clipboardcontent
ClipSettingsPost()		=> /clipsettings
QueryTextHistoryGet()	=> /querytexthistory

func (LowerCase) Connector

func (s LowerCase) Connector() string

func (LowerCase) Name

func (s LowerCase) Name() string

func (LowerCase) Split

func (s LowerCase) Split(relativePath string) []string

type Original

type Original struct{}

Original 原始不变,保持结构体方法名(不含HTTP方法名),只拼接成合法的路由 由于结构体非导出方法不会作为路由函数处理,因此此方案等同于大驼峰形式

# example

GetClipboardContent()	=> /ClipboardContent
ClipSettingsPost()		=> /ClipSettings
QueryTextHistoryGet()	=> /QueryTextHistory

func (Original) Connector

func (s Original) Connector() string

func (Original) Name

func (s Original) Name() string

func (Original) Split

func (s Original) Split(relativePath string) []string

type RoutePathSchema

type RoutePathSchema interface {
	Name() string                       // 路由名称
	Connector() string                  // 路径连接符,仅对分组后的相对路由有效,用于将分段后的相对路由组合为一个完成的路由
	Split(relativePath string) []string // 将相对路由分段,relativePath 为去除 Http.Method 后的方法名
}

RoutePathSchema 路由格式化方案

func Default

func Default() RoutePathSchema

Default 默认路由解析方法, 全小写-短横线

type Underline

type Underline struct{}

Underline 下划线 将方法名按单词分割后用"_"相连接

# example

GetClipboardContent()	=> /Clipboard-Content
ClipSettingsPost()		=> /Clip-Settings
QueryTextHistoryGet()	=> /Query-Text-History

func (Underline) Connector

func (s Underline) Connector() string

func (Underline) Name

func (s Underline) Name() string

func (Underline) Split

func (s Underline) Split(relativePath string) []string

type UnixDash

type UnixDash struct{}

UnixDash 短横线 将方法名按单词分割后用"-"相连接

# example

GetClipboardContent()	=> /Clipboard-Content
ClipSettingsPost()		=> /Clip-Settings
QueryTextHistoryGet()	=> /Query-Text-History

func (UnixDash) Connector

func (s UnixDash) Connector() string

func (UnixDash) Name

func (s UnixDash) Name() string

func (UnixDash) Split

func (s UnixDash) Split(relativePath string) []string

Jump to

Keyboard shortcuts

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