Skip to content

Commit

Permalink
Fix mixed case name handling in ToGo, ToGoPrivate
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Mar 7, 2019
1 parent d872af6 commit bc386d7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
14 changes: 12 additions & 2 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ func ToGo(name string) string {
if info.MatchCommonInitial {
word = strings.ToUpper(word)
} else if !info.HasCommonInitial {
word = ucFirst(strings.ToLower(word))
if strings.ToUpper(word) == word || strings.ToLower(word) == word {
// FOO or foo → Foo
// FOo → FOo
word = ucFirst(strings.ToLower(word))
}
}
runes = append(runes, []rune(word)...)
})
Expand All @@ -250,7 +254,13 @@ func ToGoPrivate(name string) string {
wordWalker(name, func(info *wordInfo) {
word := info.Word
if first {
word = strings.ToLower(info.Word)
if strings.ToUpper(word) == word || strings.ToLower(word) == word {
// ID → id, CAMEL → camel
word = strings.ToLower(info.Word)
} else {
// ITicket → iTicket
word = lcFirst(info.Word)
}
first = false
} else if info.MatchCommonInitial {
word = strings.ToUpper(word)
Expand Down
6 changes: 6 additions & 0 deletions codegen/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestToGo(t *testing.T) {
require.Equal(t, "", ToGo(""))

require.Equal(t, "RelatedUrls", ToGo("RelatedUrls"))
require.Equal(t, "ITicket", ToGo("ITicket"))
require.Equal(t, "FooTicket", ToGo("fooTicket"))
}

func TestToGoPrivate(t *testing.T) {
Expand All @@ -48,6 +50,9 @@ func TestToGoPrivate(t *testing.T) {
require.Equal(t, "utf8Foo", ToGoPrivate("UTF8Foo"))
require.Equal(t, "jsonEncoding", ToGoPrivate("JSONEncoding"))

require.Equal(t, "relatedUrls", ToGoPrivate("RelatedUrls"))
require.Equal(t, "iTicket", ToGoPrivate("ITicket"))

require.Equal(t, "rangeArg", ToGoPrivate("Range"))

require.Equal(t, "a", ToGoPrivate("A"))
Expand Down Expand Up @@ -87,6 +92,7 @@ func Test_wordWalker(t *testing.T) {
require.Equal(t, []*wordInfo{}, helper(""))

require.Equal(t, []*wordInfo{{Word: "Related"}, {Word: "Urls"}}, helper("RelatedUrls"))
require.Equal(t, []*wordInfo{{Word: "ITicket"}}, helper("ITicket"))
}

func TestCenter(t *testing.T) {
Expand Down
20 changes: 10 additions & 10 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions codegen/testserver/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bc386d7

Please sign in to comment.