-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathfuncs.go
320 lines (288 loc) Β· 7.36 KB
/
funcs.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
package codegen
import (
"bytes"
"os"
"strings"
"unicode"
)
// TemplateFuncs lists common template helper functions.
func TemplateFuncs() map[string]interface{} {
return map[string]interface{}{
"commandLine": CommandLine,
"comment": Comment,
}
}
// CommandLine return the command used to run this process.
func CommandLine() string {
cmdl := "$ goa"
for _, arg := range os.Args {
if strings.HasPrefix(arg, "--cmd=") {
cmdl = arg[6:]
break
}
}
return cmdl
}
// Comment produces line comments by concatenating the given strings and
// producing 80 characters long lines starting with "//".
func Comment(elems ...string) string {
var lines []string
for _, e := range elems {
lines = append(lines, strings.Split(e, "\n")...)
}
var trimmed = make([]string, len(lines))
for i, l := range lines {
trimmed[i] = strings.TrimLeft(l, " \t")
}
t := strings.Join(trimmed, "\n")
return Indent(WrapText(t, 77), "// ")
}
// Indent inserts prefix at the beginning of each non-empty line of s. The
// end-of-line marker is NL.
func Indent(s, prefix string) string {
var (
res []byte
b = []byte(s)
p = []byte(prefix)
bol = true
)
for _, c := range b {
if bol && c != '\n' {
res = append(res, p...)
}
res = append(res, c)
bol = c == '\n'
}
return string(res)
}
// Casing exceptions
var toLower = map[string]string{"OAuth": "oauth"}
// CamelCase produces the CamelCase version of the given string. It removes any
// non letter and non digit character.
//
// If firstUpper is true the first letter of the string is capitalized else
// the first letter is in lowercase.
//
// If acronym is true and a part of the string is a common acronym
// then it keeps the part capitalized (firstUpper = true)
// (e.g. APIVersion) or lowercase (firstUpper = false) (e.g. apiVersion).
func CamelCase(name string, firstUpper bool, acronym bool) string {
if name == "" {
return ""
}
runes := []rune(name)
// remove trailing invalid identifiers (makes code below simpler)
runes = removeTrailingInvalid(runes)
// all characters are invalid
if len(runes) == 0 {
return ""
}
w, i := 0, 0 // index of start of word, scan
for i+1 <= len(runes) {
eow := false // whether we hit the end of a word
// remove leading invalid identifiers
runes = removeInvalidAtIndex(i, runes)
if i+1 == len(runes) {
eow = true
} else if !validIdentifier(runes[i]) {
// get rid of it
runes = append(runes[:i], runes[i+1:]...)
} else if runes[i+1] == '_' {
// underscore; shift the remainder forward over any run of underscores
eow = true
n := 1
for i+n+1 < len(runes) && runes[i+n+1] == '_' {
n++
}
copy(runes[i+1:], runes[i+n+1:])
runes = runes[:len(runes)-n]
} else if isLower(runes[i]) && !isLower(runes[i+1]) {
// lower->non-lower
eow = true
}
i++
if !eow {
continue
}
// [w,i] is a word.
word := string(runes[w:i])
// is it one of our initialisms?
if u := strings.ToUpper(word); acronym && commonInitialisms[u] {
if firstUpper {
u = strings.ToUpper(u)
} else if w == 0 {
u = strings.ToLower(u)
}
// All the common initialisms are ASCII,
// so we can replace the bytes exactly.
copy(runes[w:], []rune(u))
} else if w > 0 && strings.ToLower(word) == word {
// already all lowercase, and not the first word, so uppercase the first character.
runes[w] = unicode.ToUpper(runes[w])
} else if w == 0 && strings.ToLower(word) == word && firstUpper {
runes[w] = unicode.ToUpper(runes[w])
}
if w == 0 && !firstUpper {
runes[w] = unicode.ToLower(runes[w])
}
//advance to next word
w = i
}
return string(runes)
}
// SnakeCase produces the snake_case version of the given CamelCase string.
// News => news
// OldNews => old_news
// CNNNews => cnn_news
func SnakeCase(name string) string {
// Special handling for single "words" starting with multiple upper case letters
for u, l := range toLower {
name = strings.Replace(name, u, l, -1)
}
// Special handling for dashes to convert them into underscores
name = strings.Replace(name, "-", "_", -1)
var b bytes.Buffer
ln := len(name)
if ln == 0 {
return ""
}
n := rune(name[0])
b.WriteRune(unicode.ToLower(n))
lastLower, isLower, lastUnder, isUnder := false, true, false, false
for i := 1; i < ln; i++ {
r := rune(name[i])
isLower = unicode.IsLower(r) && unicode.IsLetter(r) || unicode.IsDigit(r)
isUnder = r == '_'
if !isLower && !isUnder {
if lastLower && !lastUnder {
b.WriteRune('_')
} else if ln > i+1 {
rn := rune(name[i+1])
if unicode.IsLower(rn) && rn != '_' && !lastUnder {
b.WriteRune('_')
}
}
}
b.WriteRune(unicode.ToLower(r))
lastLower = isLower
lastUnder = isUnder
}
return b.String()
}
// KebabCase produces the kebab-case version of the given CamelCase string.
func KebabCase(name string) string {
name = SnakeCase(name)
ln := len(name)
if name[ln-1] == '_' {
name = name[:ln-1]
}
return strings.Replace(name, "_", "-", -1)
}
// WrapText produces lines with text capped at maxChars
// it will keep words intact and respects newlines.
func WrapText(text string, maxChars int) string {
res := ""
lines := strings.Split(text, "\n")
for _, v := range lines {
runes := []rune(strings.TrimSpace(v))
for l := len(runes); l >= 0; l = len(runes) {
if maxChars >= l {
res = res + string(runes) + "\n"
break
}
i := runeSpacePosRev(runes[:maxChars])
if i == 0 {
i = runeSpacePos(runes)
}
res = res + string(runes[:i]) + "\n"
if l == i {
break
}
runes = runes[i+1:]
}
}
return res[:len(res)-1]
}
func runeSpacePosRev(r []rune) int {
for i := len(r) - 1; i > 0; i-- {
if unicode.IsSpace(r[i]) {
return i
}
}
return 0
}
func runeSpacePos(r []rune) int {
for i := 0; i < len(r); i++ {
if unicode.IsSpace(r[i]) {
return i
}
}
return len(r)
}
// isLower returns true if the character is considered a lower case character
// when transforming word into CamelCase.
func isLower(r rune) bool {
return unicode.IsDigit(r) || unicode.IsLower(r)
}
// validIdentifier returns true if the rune is a letter or number
func validIdentifier(r rune) bool {
return unicode.IsLetter(r) || unicode.IsDigit(r)
}
// removeTrailingInvalid removes trailing invalid identifiers from runes.
func removeTrailingInvalid(runes []rune) []rune {
valid := len(runes) - 1
for ; valid >= 0 && !validIdentifier(runes[valid]); valid-- {
}
return runes[0 : valid+1]
}
// removeInvalidAtIndex removes consecutive invalid identifiers from runes starting at index i.
func removeInvalidAtIndex(i int, runes []rune) []rune {
valid := i
for ; valid < len(runes) && !validIdentifier(runes[valid]); valid++ {
}
return append(runes[:i], runes[valid:]...)
}
var (
// common words who need to keep their
commonInitialisms = map[string]bool{
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JMES": true,
"JSON": true,
"JWT": true,
"LHS": true,
"OK": 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,
"XSRF": true,
"XSS": true,
}
)