Documentation ¶
Index ¶
- Constants
- Variables
- func Format(prefix string, relativePath string, schema RoutePathSchema) string
- func LowercaseFirstLetter(s string) string
- func SplitWords(s string) []string
- func UppercaseFirstLetter(s string) string
- type AddPrefix
- type AddSuffix
- type Backslash
- type Composition
- type Dash
- type LowerCamelCase
- type LowerCase
- type Original
- type RoutePathSchema
- type Underline
- type UnixDash
Constants ¶
const ( PathParamPrefix = ":" // 路径参数起始字符 PathSeparator = "/" // 路径分隔符 OptionalQueryParamPrefix = "?" // 查询参数起始字符,也是路径参数结束字符 )
Variables ¶
var LowerCaseBackslash = NewComposition(&LowerCase{}, &Backslash{})
LowerCaseBackslash 全小写段路由
var LowerCaseDash = NewComposition(&LowerCase{}, &UnixDash{})
LowerCaseDash 全小写-短横线
Functions ¶
func Format ¶
func Format(prefix string, relativePath string, schema RoutePathSchema) string
Format 按照方案格式化并组合路由
func LowercaseFirstLetter ¶
LowercaseFirstLetter 将一个字符串的首字母转换为小写形式
func SplitWords ¶
SplitWords 将字符串s按照单词进行切分, 判断单词的依据为:是否首字母大写 如果输入s无法切分,则返回只有s构成的一个元素的数组 如果输入s包含数字,下划线等,则其包含在前面的单词结尾
func UppercaseFirstLetter ¶
UppercaseFirstLetter 将一个字符串的首字母转换为大写形式
Types ¶
type AddPrefix ¶
type AddPrefix struct {
Prefix string
}
AddPrefix 用于在分段路由基础上添加一个前缀字符,作用于每一段路径,通常与其他方案组合使用
type AddSuffix ¶
type AddSuffix struct {
Suffix string
}
AddSuffix 用于在分段路由基础上添加一个后缀字符,作用于每一段路径,通常与其他方案组合使用
type Backslash ¶
type Backslash struct{}
Backslash 反斜杠 按单词分段,每一个单词都作为一个路由段
# example GetClipboardContent() => /Clipboard/Content ClipSettingsPost() => /Clip/Settings QueryTextHistoryGet() => /Query/Text/History
type Composition ¶
type Composition struct {
// contains filtered or unexported fields
}
Composition 组合式路由格式化方案, 通过按顺序执行多个 RoutePathSchema 获得最终路由 此方案会将多个 RoutePathSchema.Connector 拼接成一个唯一的 Connector 在执行 Split 时, 具体步骤为:
将 relativePath 代入第一个 RoutePathSchema,得到 Split 后的字符串数组 spans,
将 spans 的元素作为相对路由代入之后的 RoutePathSchema.Split, 得到临时字符串数组 ss, 将 ss 内部的元素拼接起来(直接+连接)代入下一个 RoutePathSchema.Split, 直到执行完全部的 RoutePathSchema.Split, 把最后的字符串数组拼接起来得到 S1
继续迭代 spans 的元素, 重复步骤2得到 Sx
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 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
type Original ¶
type Original struct{}
Original 原始不变,保持结构体方法名(不含HTTP方法名),只拼接成合法的路由 由于结构体非导出方法不会作为路由函数处理,因此此方案等同于大驼峰形式
# example GetClipboardContent() => /ClipboardContent ClipSettingsPost() => /ClipSettings QueryTextHistoryGet() => /QueryTextHistory
type RoutePathSchema ¶
type RoutePathSchema interface { Name() string // 路由名称 Connector() string // 路径连接符,仅对分组后的相对路由有效,用于将分段后的相对路由组合为一个完成的路由 Split(relativePath string) []string // 将相对路由分段,relativePath 为去除 Http.Method 后的方法名 }
RoutePathSchema 路由格式化方案
type Underline ¶
type Underline struct{}
Underline 下划线 将方法名按单词分割后用"_"相连接
# example GetClipboardContent() => /Clipboard-Content ClipSettingsPost() => /Clip-Settings QueryTextHistoryGet() => /Query-Text-History