Skip to content

Commit

Permalink
Handle common initialisms as the non-first word (#3302)
Browse files Browse the repository at this point in the history
* Handle common initialisms as the non-first word

Co-authored-by: Sid Feiner <sidfeiner@gmail.com>

* Add wordWalker test

---------

Co-authored-by: Sid Feiner <sidfeiner@gmail.com>
  • Loading branch information
noamcohen97 and sidfeiner authored Sep 26, 2024
1 parent a711ecb commit cd6f0b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,18 +495,18 @@ func wordWalker(str string, f func(*wordInfo)) {
if initialisms[upperWord] {
// If the uppercase word (string(runes[w:i]) is "ID" or "IP"
// AND
// the word is the first two characters of the str
// the word is the first two characters of the current word
// AND
// that is not the end of the word
// AND
// the length of the string is greater than 3
// the length of the remaining string is greater than 3
// AND
// the third rune is an uppercase one
// THEN
// do NOT count this as an initialism.
switch upperWord {
case "ID", "IP":
if word == str[:2] && !eow && len(str) > 3 && unicode.IsUpper(runes[3]) {
if remainingRunes := runes[w:]; word == string(remainingRunes[:2]) && !eow && len(remainingRunes) > 3 && unicode.IsUpper(remainingRunes[3]) {
continue
}
}
Expand Down
7 changes: 7 additions & 0 deletions codegen/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func TestToGo(t *testing.T) {
require.Equal(t, "Identities", ToGo("identities"))
require.Equal(t, "Iphone", ToGo("IPHONE"))
require.Equal(t, "IPhone", ToGo("iPHONE"))
require.Equal(t, "UserIdentity", ToGo("USER_IDENTITY"))
require.Equal(t, "UserIdentity", ToGo("UserIdentity"))
require.Equal(t, "UserIdentity", ToGo("userIdentity"))
}

func TestToGoPrivate(t *testing.T) {
Expand Down Expand Up @@ -300,6 +303,10 @@ func Test_wordWalker(t *testing.T) {
input: makeInput("RelatedUrls"),
expected: []*wordInfo{{Word: "Related"}, {WordOffset: 1, Word: "Urls"}},
},
{
input: makeInput("USER_IDENTITY"),
expected: []*wordInfo{{Word: "USER"}, {WordOffset: 1, Word: "IDENTITY"}},
},
{
input: makeInput("ITicket"),
expected: []*wordInfo{{Word: "ITicket"}},
Expand Down

0 comments on commit cd6f0b9

Please sign in to comment.