-
Notifications
You must be signed in to change notification settings - Fork 1
/
snippet_ident.go
327 lines (284 loc) · 5.83 KB
/
snippet_ident.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package codegen
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
func Id(n string) *SnippetIdent {
values := strings.Split(n, ".")
if !IsValidIdent(values[0]) {
panic(fmt.Errorf("`%s` is not a valid identifier", values[0]))
}
if len(values) == 2 {
if !IsValidIdent(values[1]) {
panic(fmt.Errorf("`%s` is not a valid identifier", values[1]))
}
}
ident := SnippetIdent(n)
return &ident
}
func IdsFromNames(names ...string) []*SnippetIdent {
ids := make([]*SnippetIdent, 0)
for _, name := range names {
ids = append(ids, Id(name))
}
return ids
}
type SnippetIdent string
func (SnippetIdent) snippetCanAddr() {}
func (id SnippetIdent) Bytes() []byte {
return []byte(id)
}
func IsValidIdent(s string) bool {
if len(s) == 0 {
return false
}
if isReservedWord(s) {
return false
}
for _, r := range s {
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '_' {
return false
}
}
return true
}
func isBuiltInFunc(alias string) bool {
for _, name := range builtInFuncs {
if alias == name {
return true
}
}
return false
}
func isReservedWord(alias string) bool {
for _, name := range reserved {
if alias == name {
return true
}
}
return false
}
var builtInFuncs = []string{
"append",
"complex",
"cap",
"close",
"copy",
"delete",
"imag",
"len",
"make",
"new",
"panic",
"print",
"println",
"real",
"recover",
}
var reserved = append([]string{
Stringify(Break),
Stringify(Fallthrough),
Stringify(Continue),
"default",
"func",
"interface",
"select",
"case",
"defer",
"go",
"map",
"struct",
"chan",
"else",
"goto",
"package",
"switch",
"const",
"if",
"range",
"type",
"for",
"import",
"return",
"var",
Stringify(Bool),
Stringify(Int),
Stringify(Int8),
Stringify(Int16),
Stringify(Int32),
Stringify(Int64),
Stringify(Uint),
Stringify(Uint8),
Stringify(Uint16),
Stringify(Uint32),
Stringify(Uint64),
Stringify(Uintptr),
Stringify(Float32),
Stringify(Float64),
Stringify(Complex64),
Stringify(Complex128),
Stringify(String),
Stringify(Byte),
Stringify(Rune),
string(Error),
}, builtInFuncs...)
func (id SnippetIdent) UpperCamelCase() *SnippetIdent {
return Id(UpperCamelCase(string(id)))
}
func (id SnippetIdent) LowerCamelCase() *SnippetIdent {
return Id(LowerCamelCase(string(id)))
}
func (id SnippetIdent) UpperSnakeCase() *SnippetIdent {
return Id(UpperSnakeCase(string(id)))
}
func (id SnippetIdent) LowerSnakeCase() *SnippetIdent {
return Id(LowerSnakeCase(string(id)))
}
func UpperSnakeCase(s string) string {
return rewords(s, func(result string, word string, idx int) string {
newWord := strings.ToUpper(word)
if idx == 0 || (len(newWord) == 1 && unicode.IsDigit(rune(newWord[0]))) {
return result + newWord
}
return result + "_" + newWord
})
}
func LowerSnakeCase(s string) string {
return rewords(s, func(result string, word string, idx int) string {
newWord := strings.ToLower(word)
if idx == 0 || (len(newWord) == 1 && unicode.IsDigit(rune(newWord[0]))) {
return result + newWord
}
return result + "_" + newWord
})
}
func UpperCamelCase(s string) string {
return rewords(s, func(result string, word string, idx int) string {
return result + camelCase(word)
})
}
func LowerCamelCase(s string) string {
return rewords(s, func(result string, word string, idx int) string {
if idx == 0 {
return result + strings.ToLower(word)
}
return result + camelCase(word)
})
}
func upperFirst(s string) string {
runes := []rune(s)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
func camelCase(s string) string {
upperString := strings.ToUpper(s)
if commonInitialisms[upperString] {
return upperString
}
return upperFirst(strings.ToLower(s))
}
func rewords(s string, reducer func(result string, word string, index int) string) string {
words := splitToWords(string(s))
var result = ""
for idx, word := range words {
result = reducer(result, word, idx)
}
return result
}
func splitToWords(s string) (entries []string) {
if !utf8.ValidString(s) {
return []string{s}
}
entries = []string{}
var runes [][]rune
lastClass := 0
class := 0
// split into fields based on class of unicode character
for _, r := range s {
switch true {
case unicode.IsSpace(r):
class = 1
case unicode.IsLower(r):
class = 2
case unicode.IsUpper(r):
class = 3
case unicode.IsDigit(r):
class = 4
default:
class = 5
}
if class == lastClass {
runes[len(runes)-1] = append(runes[len(runes)-1], r)
} else {
runes = append(runes, []rune{r})
}
lastClass = class
}
// handle upper case -> lower case sequences, e.g.
// "PDFL", "oader" -> "PDF", "Loader"
for i := 0; i < len(runes)-1; i++ {
if unicode.IsUpper(runes[i][0]) && unicode.IsLower(runes[i+1][0]) {
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
runes[i] = runes[i][:len(runes[i])-1]
}
}
// construct []string from results
for i, s := range runes {
if len(s) > 0 {
if unicode.IsDigit(s[0]) {
if i > 0 {
entries[len(entries)-1] += string(s)
} else {
entries = append(entries, string(s))
}
}
if unicode.IsLetter(s[0]) {
entries = append(entries, string(s))
}
}
}
return
}
// https://github.com/golang/lint/blob/master/lint.go#L749-L788
var commonInitialisms = map[string]bool{
"ACL": true,
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SQL": true,
"SSH": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"XMPP": true,
"XSRF": true,
"XSS": true,
}