Enhance smartLabelFor function (#135683)

* Enhance smartLabelFor function to support additional acronym "PDB" and add unit tests

* Remove "PDB" acronym from commonAcronyms in smartLabelFor function
This commit is contained in:
Ümüt Özalp
2025-12-18 11:09:41 +01:00
committed by GitHub
parent 06ff302857
commit bbd992d496
2 changed files with 52 additions and 4 deletions

View File

@@ -358,17 +358,41 @@ func smartLabelFor(field string) string {
commonAcronyms := []string{"API", "URL", "UID", "OSB", "GUID"}
parts := camelcase.Split(field)
result := make([]string, 0, len(parts))
for _, part := range parts {
mergedParts := make([]string, 0, len(parts))
for i := 0; i < len(parts); i++ {
part := parts[i]
if i < len(parts)-1 {
nextPart := parts[i+1]
// Merge if current part is 2+ uppercase letters and next starts with uppercase
allUpper := true
for _, r := range part {
if unicode.IsLetter(r) && !unicode.IsUpper(r) {
allUpper = false
break
}
}
if len(part) >= 2 && allUpper && len(nextPart) > 0 && unicode.IsUpper(rune(nextPart[0])) {
mergedParts = append(mergedParts, part+nextPart)
i++
continue
}
}
mergedParts = append(mergedParts, part)
}
result := make([]string, 0, len(mergedParts))
for _, part := range mergedParts {
if part == "_" {
continue
}
if slice.Contains[string](commonAcronyms, strings.ToUpper(part), nil) {
if slice.Contains(commonAcronyms, strings.ToUpper(part), nil) {
part = strings.ToUpper(part)
} else {
} else if strings.ToLower(part) == part {
part = cases.Title(language.English).String(part)
}
result = append(result, part)
}

View File

@@ -7128,3 +7128,27 @@ func TestDescribeProjectedVolumesOptionalSecret(t *testing.T) {
t.Errorf("expected to find %q in output: %q", expectedOut, out)
}
}
func TestSmartLabelFor(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"simpleField", "Simple Field"},
{"respectPDBs", "Respect PDBs"},
{"respectPDB", "Respect PDB"},
{"apiURL", "API URL"},
{"kubeAPI", "Kube API"},
{"userUID", "User UID"},
{"HTTPSProxy", "HTTPSProxy"}, // Multiple capitals stay together
{"customCRDs", "Custom CRDs"}, // Another pluralized acronym
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
if got := smartLabelFor(tt.input); got != tt.expected {
t.Errorf("smartLabelFor(%q) = %q, want %q", tt.input, got, tt.expected)
}
})
}
}