mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-18 04:54:54 +00:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user