-
Notifications
You must be signed in to change notification settings - Fork 8
/
symbol.go
269 lines (250 loc) · 5.56 KB
/
symbol.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package thriftlint
import (
"bytes"
"fmt"
"go/doc"
"reflect"
"strings"
"unicode"
)
var (
// BuiltinThriftTypes is a map of the basic builtin Thrift types. Useful in templates.
BuiltinThriftTypes = map[string]bool{
"bool": true,
"byte": true,
"i16": true,
"i32": true,
"i64": true,
"double": true,
"string": true,
}
// BuiltinThriftCollections is the set of builtin collection types in Thrift.
BuiltinThriftCollections = map[string]bool{
"map": true,
"list": true,
"set": true,
"binary": true,
}
)
// Scanner over a sequence of runes.
type scanner struct {
runes []rune
cursor int
}
func (s *scanner) peek() rune {
if s.cursor >= len(s.runes) {
return -1
}
return s.runes[s.cursor]
}
func (s *scanner) next() rune {
r := s.peek()
if r != -1 {
s.cursor++
}
return r
}
func (s *scanner) reverse() {
s.cursor--
}
func consumeLower(scan *scanner) string {
out := ""
for unicode.IsLower(scan.peek()) || unicode.IsNumber(scan.peek()) {
out += string(scan.next())
}
return out
}
// ([A-Z]+)(?:[A-Z][a-z]|$)
func consumeMostUpper(scan *scanner) string {
out := ""
for unicode.IsUpper(scan.peek()) || unicode.IsNumber(scan.peek()) {
r := scan.next()
if unicode.IsLower(scan.peek()) && !commonInitialisms[out+string(r)] {
scan.reverse()
break
}
out += string(r)
}
return out
}
func title(s string) string {
return strings.ToUpper(s[0:1]) + strings.ToLower(s[1:])
}
// From https://github.com/golang/lint/blob/master/lint.go
var commonInitialisms = map[string]bool{
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DB": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"MD5": true,
"MLS": true,
"OK": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SHA": true,
"SLA": true,
"SMTP": true,
"SQL": true,
"SSH": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"URI": true,
"URL": true,
"UTC": true,
"UTF8": true,
"UUID": true,
"VM": true,
"XML": true,
"XSRF": true,
"XSS": true,
}
func Comment(v interface{}) []string {
comment := reflect.Indirect(reflect.ValueOf(v)).FieldByName("Comment").Interface().(string)
if comment == "" {
return nil
}
w := bytes.NewBuffer(nil)
doc.ToText(w, comment, "", "", 80)
return strings.Split(strings.TrimSpace(w.String()), "\n")
}
func IsInitialism(s string) bool {
return commonInitialisms[strings.ToUpper(s)]
}
// UpperCamelCase converts a symbol to CamelCase
func UpperCamelCase(s string) string {
parts := []string{}
for _, part := range SplitSymbol(s) {
if part == "" {
parts = append(parts, "_")
continue
}
if part == "s" && len(parts) > 0 {
parts[len(parts)-1] += part
} else {
if commonInitialisms[strings.ToUpper(part)] {
part = strings.ToUpper(part)
} else {
part = title(part)
}
parts = append(parts, part)
}
}
return strings.Join(parts, "")
}
// LowerCamelCase converts a symbol to lowerCamelCase
func LowerCamelCase(s string) string {
first := true
parts := []string{}
for _, part := range SplitSymbol(s) {
if part == "" {
parts = append(parts, "_")
continue
}
if first {
parts = append(parts, strings.ToLower(part))
first = false
} else {
// Merge trailing s
if part == "s" && len(parts) > 0 {
parts[len(parts)-1] += part
} else {
if commonInitialisms[strings.ToUpper(part)] {
part = strings.ToUpper(part)
} else {
part = title(part)
}
parts = append(parts, part)
}
}
}
return strings.Join(parts, "")
}
// LowerSnakeCase converts a symbol to snake_case
func LowerSnakeCase(s string) string {
parts := []string{}
for _, part := range SplitSymbol(s) {
if part == "" {
parts = append(parts, "_")
continue
}
parts = append(parts, strings.ToLower(part))
}
return strings.Join(parts, "_")
}
// UpperSnakeCase converts a symbol to UPPER_SNAKE_CASE
func UpperSnakeCase(s string) string {
parts := []string{}
for _, part := range SplitSymbol(s) {
if part == "" {
parts = append(parts, "_")
continue
}
parts = append(parts, strings.ToUpper(part))
}
return strings.Join(parts, "_")
}
// SplitSymbol splits an arbitrary symbol into parts. It accepts symbols in snake case and camel
// case, and correctly supports all-caps substrings.
//
// eg. "some_snake_case_symbol" would become ["some", "snake", "case", "symbol"]
// and "someCamelCaseSymbol" would become ["some", "Camel", "Case", "Symbol"]
func SplitSymbol(s string) []string {
// This is painful. See TestSplitSymbol for examples of what this does.
out := []string{}
scan := &scanner{runes: []rune(s)}
for scan.peek() != -1 {
part := ""
r := scan.peek()
switch {
case unicode.IsLower(r):
part = consumeLower(scan)
case unicode.IsUpper(r):
scan.next()
// [A-Z][a-z]+
if unicode.IsLower(scan.peek()) {
part += string(r)
part += consumeLower(scan)
} else {
scan.reverse()
part += consumeMostUpper(scan)
}
case unicode.IsNumber(r):
for unicode.IsNumber(scan.peek()) {
part += string(scan.next())
}
case r == '_':
scan.next()
if len(out) == 0 {
break
}
continue
default:
panic(fmt.Sprintf("unsupported character %q in %q", r, s))
}
out = append(out, part)
}
return out
}
// Extract the suffix from a . separated string (ie. namespace).
// Useful for getting the package reference from a files namespace.
func DotSuffix(pkg string) string {
parts := strings.Split(pkg, ".")
return parts[len(parts)-1]
}