/
string.go
174 lines (154 loc) · 4.74 KB
/
string.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
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package gen
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
"go.uber.org/thriftrw/compile"
)
// isAllCaps checks if a string contains all capital letters only. Non-letters
// are not considered.
func isAllCaps(s string) bool {
for _, r := range s {
if unicode.IsLetter(r) && !unicode.IsUpper(r) {
return false
}
}
return true
}
// pascalCase combines the given words using PascalCase.
//
// If allowAllCaps is true, when an all-caps word that is not a known
// abbreviation is encountered, it is left unchanged. Otherwise, it is
// Titlecased.
func pascalCase(allowAllCaps bool, words ...string) string {
for i, chunk := range words {
if len(chunk) == 0 {
// foo__bar
continue
}
// known initalism
init := strings.ToUpper(chunk)
if _, ok := commonInitialisms[init]; ok {
words[i] = init
continue
}
// Was SCREAMING_SNAKE_CASE and not a known initialism so Titlecase it.
if isAllCaps(chunk) && !allowAllCaps {
// A single ALLCAPS word does not count as SCREAMING_SNAKE_CASE.
// There must be at least one underscore.
words[i] = strings.Title(strings.ToLower(chunk))
continue
}
// Just another word, but could already be camelCased somehow, so just
// change the first letter.
head, headIndex := utf8.DecodeRuneInString(chunk)
words[i] = string(unicode.ToUpper(head)) + string(chunk[headIndex:])
}
return strings.Join(words, "")
}
func constantName(s string) string {
return pascalCase(false /* all caps */, strings.Split(s, "_")...)
}
// goCase converts strings into PascalCase.
func goCase(s string) string {
if len(s) == 0 {
panic(fmt.Sprintf("%q is not a valid identifier", s))
}
words := strings.Split(s, "_")
return pascalCase(len(words) == 1 /* all caps */, words...)
// goCase allows all caps only if the string is a single all caps word.
// That is, "FOO" is allowed but "FOO_BAR" is changed to "FooBar".
}
// goNameAnnotation returns ("", nil) if there is no "go.name" annotation.
func goNameAnnotation(e compile.NamedEntity) (string, error) {
name, ok := e.ThriftAnnotations()["go.name"]
if !ok {
return "", nil
}
c, _ := utf8.DecodeRuneInString(name)
capitalized := unicode.IsLetter(c) && unicode.IsUpper(c)
underscore := strings.Contains(name, "_")
if !capitalized || underscore {
var emsg []string
if underscore {
emsg = append(emsg, "contains underscores")
}
if !capitalized {
emsg = append(emsg, "is not capitalized")
}
return "", fmt.Errorf("%q (from go.name annotation) is not a Go style public identifier (%s), suggestion: %q)", name, strings.Join(emsg, ", "), goCase(name))
}
return name, nil
}
func goNameForNamedEntity(e compile.NamedEntity) (name string, fromAnnotation bool, err error) {
fromAnnotation = true
name, err = goNameAnnotation(e)
if err == nil && name == "" {
name = goCase(e.ThriftName())
fromAnnotation = false
}
return name, fromAnnotation, err
}
func goName(e compile.NamedEntity) (string, error) {
name, _, err := goNameForNamedEntity(e)
return name, err
}
// This set is taken from https://github.com/golang/lint/blob/master/lint.go#L692
var 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,
"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,
"XSRF": true,
"XSS": true,
}