Skip to content

Commit c65e588

Browse files
committed
fix(mangler): name mangler panics on trailing pluralized initialisms
This fix correctly detects this situation and avoid an out of range check. * fixes #158 Signed-off-by: Frederic BIDON <[email protected]>
1 parent 47cac45 commit c65e588

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

mangling/initialism_index.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ const (
179179
simplePlural
180180
)
181181

182+
func (f pluralForm) String() string {
183+
switch f {
184+
case notPlural:
185+
return "notPlural"
186+
case invariantPlural:
187+
return "invariantPlural"
188+
case simplePlural:
189+
return "simplePlural"
190+
default:
191+
return "<unknown>"
192+
}
193+
}
194+
182195
// pluralForm indicates how we want to pluralize a given initialism.
183196
//
184197
// Besides configured invariant forms (like HTTP and HTTPS),

mangling/name_mangler_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/go-openapi/testify/v2/assert"
10+
"github.com/go-openapi/testify/v2/require"
1011
)
1112

1213
const (
@@ -24,6 +25,16 @@ const (
2425
sampleUscore = "sample_"
2526
)
2627

28+
func TestManglerToGoName_Issue158(t *testing.T) {
29+
m := NewNameMangler()
30+
31+
t.Run("should detect trailing pluralized initialisms", func(t *testing.T) {
32+
require.Equal(t, "LinkLocalIPs", m.ToGoName("LinkLocalIPs"))
33+
require.Equal(t, "NativeBaseURLs", m.ToGoName("nativeBaseURLs"))
34+
require.Equal(t, "SiteURLs", m.ToGoName("siteURLs"))
35+
})
36+
}
37+
2738
func TestManglerToGoName(t *testing.T) {
2839
m := NewNameMangler()
2940

mangling/split.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ func (s splitter) gatherInitialismMatches(nameRunes []rune) *initialismMatches {
9090
continue
9191
}
9292

93+
if currentRunePosition-match.start == len(match.body) {
94+
// unmatched: skip
95+
continue
96+
}
97+
9398
currentMatchRune := match.body[currentRunePosition-match.start]
9499
if currentMatchRune != currentRune {
95100
// failed match, move on to next rune
@@ -160,7 +165,7 @@ func (s splitter) gatherInitialismMatches(nameRunes []rune) *initialismMatches {
160165
}
161166
}
162167

163-
// check for new initialism matches
168+
// check for new initialism matches, based on the first character
164169
for i, r := range s.initialismsRunes {
165170
if r[0] == currentRune {
166171
*newMatches = append(*newMatches, initialismMatch{

0 commit comments

Comments
 (0)