From cd6f0b9da4ca46636baff82785e4a35ce21b452e Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Thu, 26 Sep 2024 15:15:15 +0300 Subject: [PATCH] Handle common initialisms as the non-first word (#3302) * Handle common initialisms as the non-first word Co-authored-by: Sid Feiner * Add wordWalker test --------- Co-authored-by: Sid Feiner --- codegen/templates/templates.go | 6 +++--- codegen/templates/templates_test.go | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/codegen/templates/templates.go b/codegen/templates/templates.go index 5a4374f02ff..a9d4e0217f6 100644 --- a/codegen/templates/templates.go +++ b/codegen/templates/templates.go @@ -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 } } diff --git a/codegen/templates/templates_test.go b/codegen/templates/templates_test.go index 6ca3d824842..c282f01ec53 100644 --- a/codegen/templates/templates_test.go +++ b/codegen/templates/templates_test.go @@ -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) { @@ -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"}},