Skip to content

Commit 3aa32df

Browse files
committed
Merge branch 'mattn-fix-camel-case'
2 parents f712621 + a2173d9 commit 3aa32df

File tree

4 files changed

+151
-6
lines changed

4 files changed

+151
-6
lines changed

internal/constructor/all_args_constructor_generator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func (cg *AllArgsConstructorGenerator) Generate() g.Statement {
2323
if field.ShouldIgnore {
2424
continue
2525
}
26-
funcSignature = funcSignature.AddParameters(g.NewFuncParameter(strcase.ToLowerCamel(field.FieldName), field.FieldType))
27-
retStructureKeyValues = append(retStructureKeyValues, fmt.Sprintf("%s: %s", field.FieldName, strcase.ToLowerCamel(field.FieldName)))
26+
funcSignature = funcSignature.AddParameters(g.NewFuncParameter(toLowerCamel(field.FieldName), field.FieldType))
27+
retStructureKeyValues = append(retStructureKeyValues, fmt.Sprintf("%s: %s", field.FieldName, toLowerCamel(field.FieldName)))
2828
}
2929

3030
funcSignature = funcSignature.AddReturnTypes("*" + cg.TypeName)

internal/constructor/builder_constructor_generator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ func (cg *BuilderGenerator) Generate() g.Statement {
3535
}
3636

3737
builderStruct = builderStruct.AddField(
38-
strcase.ToLowerCamel(field.FieldName),
38+
toLowerCamel(field.FieldName),
3939
field.FieldType,
4040
)
4141

4242
fieldRegistererFunctions = append(fieldRegistererFunctions, g.NewFunc(
4343
g.NewFuncReceiver("b", "*"+builderType),
4444
g.NewFuncSignature(strcase.ToCamel(field.FieldName)).
45-
AddParameters(g.NewFuncParameter(strcase.ToLowerCamel(field.FieldName), field.FieldType)).
45+
AddParameters(g.NewFuncParameter(toLowerCamel(field.FieldName), field.FieldType)).
4646
AddReturnTypes("*"+builderType),
47-
g.NewRawStatement(fmt.Sprintf("b.%s = %s", strcase.ToLowerCamel(field.FieldName), strcase.ToLowerCamel(field.FieldName))),
47+
g.NewRawStatement(fmt.Sprintf("b.%s = %s", toLowerCamel(field.FieldName), strcase.ToLowerCamel(field.FieldName))),
4848
g.NewReturnStatement("b"),
4949
))
5050

51-
retStructureKeyValues = append(retStructureKeyValues, fmt.Sprintf("%s: b.%s", field.FieldName, strcase.ToLowerCamel(field.FieldName)))
51+
retStructureKeyValues = append(retStructureKeyValues, fmt.Sprintf("%s: b.%s", field.FieldName, toLowerCamel(field.FieldName)))
5252
}
5353

5454
buildFunc := g.NewFunc(

internal/constructor/strcase.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package constructor
2+
3+
import (
4+
"strings"
5+
"unicode"
6+
)
7+
8+
// https://github.com/golang/lint/blob/206c0f020eba0f7fbcfbc467a5eb808037df2ed6/lint.go#L731
9+
var commonInitialisms = map[string]bool{
10+
"ACL": true,
11+
"API": true,
12+
"ASCII": true,
13+
"CPU": true,
14+
"CSS": true,
15+
"DNS": true,
16+
"EOF": true,
17+
"GUID": true,
18+
"HTML": true,
19+
"HTTP": true,
20+
"HTTPS": true,
21+
"ID": true,
22+
"IP": true,
23+
"JSON": true,
24+
"LHS": true,
25+
"QPS": true,
26+
"RAM": true,
27+
"RHS": true,
28+
"RPC": true,
29+
"SLA": true,
30+
"SMTP": true,
31+
"SQL": true,
32+
"SSH": true,
33+
"TCP": true,
34+
"TLS": true,
35+
"TTL": true,
36+
"UDP": true,
37+
"UI": true,
38+
"UID": true,
39+
"UUID": true,
40+
"URI": true,
41+
"URL": true,
42+
"UTF8": true,
43+
"VM": true,
44+
"XML": true,
45+
"XMPP": true,
46+
"XSRF": true,
47+
"XSS": true,
48+
}
49+
50+
func toLowerCamel(name string) string {
51+
// Fast path for simple cases: "_" and all lowercase.
52+
if name == "_" {
53+
return name
54+
}
55+
allLower := true
56+
for _, r := range name {
57+
if !unicode.IsLower(r) {
58+
allLower = false
59+
break
60+
}
61+
}
62+
if allLower {
63+
return name
64+
}
65+
66+
// Split camelCase at any lower->upper transition, and split on underscores.
67+
// Check each word for common initialisms.
68+
runes := []rune(name)
69+
w, i := 0, 0 // index of start of word, scan
70+
for i+1 <= len(runes) {
71+
eow := false // whether we hit the end of a word
72+
if i+1 == len(runes) {
73+
eow = true
74+
} else if runes[i+1] == '_' {
75+
// underscore; shift the remainder forward over any run of underscores
76+
eow = true
77+
n := 1
78+
for i+n+1 < len(runes) && runes[i+n+1] == '_' {
79+
n++
80+
}
81+
82+
// Leave at most one underscore if the underscore is between two digits
83+
if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
84+
n--
85+
}
86+
87+
copy(runes[i+1:], runes[i+n+1:])
88+
runes = runes[:len(runes)-n]
89+
} else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
90+
// lower->non-lower
91+
eow = true
92+
}
93+
i++
94+
if !eow {
95+
continue
96+
}
97+
98+
// [w,i) is a word.
99+
word := string(runes[w:i])
100+
if u := strings.ToUpper(word); commonInitialisms[u] {
101+
if w == 0 {
102+
u = strings.ToLower(u)
103+
}
104+
copy(runes[w:], []rune(u))
105+
} else if w == 0 {
106+
copy(runes[w:], []rune(strings.ToLower(word)))
107+
}
108+
w = i
109+
}
110+
return string(runes)
111+
}

internal/constructor/strcase_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package constructor
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestToLowerCase(t *testing.T) {
8+
tests := []struct {
9+
input string
10+
want string
11+
}{
12+
{input: "", want: ""},
13+
{input: "ID", want: "id"},
14+
{input: "utf8", want: "utf8"},
15+
{input: "Utf8", want: "utf8"},
16+
{input: "UTF8", want: "utf8"},
17+
{input: "utf8name", want: "utf8name"},
18+
{input: "utf8_name", want: "utf8name"},
19+
{input: "Name", want: "name"},
20+
{input: "name", want: "name"},
21+
{input: "NAME", want: "name"},
22+
{input: "UserName", want: "userName"},
23+
{input: "userName", want: "userName"},
24+
{input: "user_Name", want: "userName"},
25+
{input: "MoZnIoN", want: "moZnIoN"},
26+
}
27+
28+
for _, test := range tests {
29+
got := toLowerCamel(test.input)
30+
if got != test.want {
31+
t.Errorf("toLowerCamel: want %v, but %v for %v:", test.want, got, test.input)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)