-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
98 lines (84 loc) · 1.68 KB
/
utils.go
File metadata and controls
98 lines (84 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"sort"
"strings"
)
type gonicKey []string
func (s gonicKey) Less(i, j int) bool {
return len(s[i]) > len(s[j])
}
func (s gonicKey) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s gonicKey) Len() int {
return len(s)
}
func isASCIIUpper(r rune) bool {
return 'A' <= r && r <= 'Z'
}
//LintGonicKeys Gonic转换时候的关键词,init函数里会进行排序,确保有序性,避免因关键词类似而带来的问题
var LintGonicKeys = gonicKey{
"API",
"ASCII",
"CPU",
"CSS",
"DNS",
"EOF",
"GUID",
"HTML",
"HTTP",
"HTTPS",
"ID",
"IP",
"JSON",
"LHS",
"QPS",
"RAM",
"RHS",
"RPC",
"SLA",
"SMTP",
"SSH",
"TLS",
"TTL",
"UI",
"UID",
"UUID",
"URI",
"URL",
"UTF8",
"VM",
"XML",
"XSRF",
"XSS",
}
func init() {
sort.Sort(LintGonicKeys)
}
//SnakeCasedName 驼峰式命名命名转换,比如 UserName 转为user_name
func SnakeCasedName(name string) string {
var newstr []rune
newstr = make([]rune, 0)
for idx, chr := range name {
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
if idx > 0 {
newstr = append(newstr, '_')
}
chr -= ('A' - 'a')
}
newstr = append(newstr, chr)
}
return string(newstr)
}
//GonicCasedName 类似驼峰式命名命名转换,但是排除一些特殊词,如ID、GUID、URL等,比如 UserID 转为user_id
func GonicCasedName(name string) string {
for _, v := range LintGonicKeys {
if strings.Contains(name, v) {
name = strings.Replace(name, v, "_"+strings.ToLower(v), -1)
}
}
if strings.HasPrefix(name, "_") {
name = strings.TrimLeft(name, "_")
}
return SnakeCasedName(name)
}