Skip to content

Commit 48c201e

Browse files
authored
improve wordifyString function to avoid splitting acronyms (#4938)
1 parent 9ce881e commit 48c201e

File tree

1 file changed

+26
-6
lines changed
  • tools/generator-go-sdk/internal/generator

1 file changed

+26
-6
lines changed

tools/generator-go-sdk/internal/generator/helpers.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"strings"
10+
"unicode"
1011

1112
"github.com/hashicorp/pandora/tools/data-api-sdk/v1/models"
1213
)
@@ -149,7 +150,7 @@ func golangTypeNameForConstantType(input models.SDKConstantType) (*string, error
149150
}
150151
segmentType, ok := segmentTypes[input]
151152
if !ok {
152-
return nil, fmt.Errorf("constant type %q has no segmentTypes mapping", string(input))
153+
return nil, fmt.Errorf("constant type %q has no segmentTypes mapping", input)
153154
}
154155
return &segmentType, nil
155156
}
@@ -173,12 +174,31 @@ func wordifyString(input string) string {
173174
val = strings.TrimSuffix(val, "Id")
174175
output := ""
175176

176-
for _, c := range val {
177-
character := string(c)
178-
if strings.ToUpper(character) == character {
179-
output += " "
177+
valLength := len(val)
178+
for i, c := range val {
179+
isUpper := unicode.IsUpper(c)
180+
isLower := unicode.IsLower(c)
181+
isNum := unicode.IsDigit(c)
182+
183+
if (i + 1) < valLength {
184+
next := rune(val[i+1])
185+
nextIsUpper := unicode.IsUpper(next)
186+
nextIsLower := unicode.IsLower(next)
187+
nextIsNum := unicode.IsDigit(next)
188+
189+
if i > 0 {
190+
if prevIsUpper := unicode.IsUpper(rune(val[i-1])); prevIsUpper && isUpper && nextIsLower {
191+
output += " "
192+
}
193+
}
194+
195+
if (isUpper && nextIsNum) || (isLower && (nextIsUpper || nextIsNum) || (isNum && (nextIsUpper || nextIsLower))) {
196+
output += string(c)
197+
output += " "
198+
continue
199+
}
180200
}
181-
output += character
201+
output += string(c)
182202
}
183203

184204
return strings.TrimPrefix(output, " ")

0 commit comments

Comments
 (0)