Use generic Contains rather than deprecated ContainsString

This commit is contained in:
Arda Güçlü 2024-11-25 11:30:04 +03:00
parent c3f15fd707
commit 8312c3ec24
2 changed files with 19 additions and 4 deletions

View File

@ -317,7 +317,7 @@ func printUnstructuredContent(w PrefixWriter, level int, content map[string]inte
switch typedValue := value.(type) {
case map[string]interface{}:
skipExpr := fmt.Sprintf("%s.%s", skipPrefix, field)
if slice.ContainsString(skip, skipExpr, nil) {
if slice.Contains[string](skip, skipExpr, nil) {
continue
}
w.Write(level, "%s:\n", smartLabelFor(field))
@ -325,7 +325,7 @@ func printUnstructuredContent(w PrefixWriter, level int, content map[string]inte
case []interface{}:
skipExpr := fmt.Sprintf("%s.%s", skipPrefix, field)
if slice.ContainsString(skip, skipExpr, nil) {
if slice.Contains[string](skip, skipExpr, nil) {
continue
}
w.Write(level, "%s:\n", smartLabelFor(field))
@ -340,7 +340,7 @@ func printUnstructuredContent(w PrefixWriter, level int, content map[string]inte
default:
skipExpr := fmt.Sprintf("%s.%s", skipPrefix, field)
if slice.ContainsString(skip, skipExpr, nil) {
if slice.Contains[string](skip, skipExpr, nil) {
continue
}
w.Write(level, "%s:\t%v\n", smartLabelFor(field), typedValue)
@ -365,7 +365,7 @@ func smartLabelFor(field string) string {
continue
}
if slice.ContainsString(commonAcronyms, strings.ToUpper(part), nil) {
if slice.Contains[string](commonAcronyms, strings.ToUpper(part), nil) {
part = strings.ToUpper(part)
} else {
part = strings.Title(part)

View File

@ -23,8 +23,23 @@ import (
// SortInts64 sorts []int64 in increasing order
func SortInts64(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
// Contains checks if a given slice of type T contains the provided item.
// If a modifier func is provided, it is called with the slice item before the comparation.
func Contains[T comparable](slice []T, s T, modifier func(s T) T) bool {
for _, item := range slice {
if item == s {
return true
}
if modifier != nil && modifier(item) == s {
return true
}
}
return false
}
// ContainsString checks if a given slice of strings contains the provided string.
// If a modifier func is provided, it is called with the slice item before the comparation.
// Deprecated: Use Contains[T] instead
func ContainsString(slice []string, s string, modifier func(s string) string) bool {
for _, item := range slice {
if item == s {